Skip to content

Commit

Permalink
correctly parse cursor when a DOI contains a comma. datacite/datacite…
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 13, 2019
1 parent 4380684 commit 5f26597
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def index

render json: ActivitySerializer.new(results, options).serialized_json, status: :ok
else
results = response.results

options = {}
options[:meta] = {
total: total,
Expand All @@ -60,15 +62,15 @@ def index
self: request.original_url,
next: response.results.size < page[:size] ? nil : request.base_url + "/activities?" + {
query: params[:query],
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(@activities.to_a.last[:sort]).join(","), padding: false) : nil,
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true

render json: ActivitySerializer.new(response.results, options).serialized_json, status: :ok
render json: ActivitySerializer.new(results, options).serialized_json, status: :ok
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/concerns/paginatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def page_from_params(params)
begin
# When we decode and split, we'll always end up with an array
# use urlsafe_decode to not worry about url-unsafe characters + and /
page[:cursor] = Base64.urlsafe_decode64(page[:cursor].to_s).split(",")
# split into two strings so that DOIs with comma in them are left intact
page[:cursor] = Base64.urlsafe_decode64(page[:cursor].to_s).split(",", 2)
rescue ArgumentError
# If we fail to decode we'll just default back to an empty cursor
page[:cursor] = []
Expand All @@ -40,5 +41,10 @@ def page_from_params(params)

page
end

def make_cursor(results)
# Base64-encode cursor
Base64.urlsafe_encode64(results.to_a.last[:sort], padding: false)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def index
"client-id" => params[:client_id],
certificate: params[:certificate],
# The cursor link should be an array of values, but we want to encode it into a single string for the URL
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(results.to_a.last[:sort]).join(','), padding: false) : nil,
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def index
"registrant-id" => params[:registrant_id],
"publication-year" => params[:publication_year],
"year-month" => params[:year_month],
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(results.to_a.last[:sort]).join(","), padding: false) : nil,
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand All @@ -216,7 +216,7 @@ def index
if @include.include?(:dois)
options[:include] = []
doi_names = (results.map { |event| event.doi}).uniq().join(",")
events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), {is_collection: true}).serializable_hash.dig(:data)
events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), is_collection: true).serializable_hash.dig(:data)
end

render json: events_serialized, status: :ok
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/old_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def index
"registrant-id" => params[:registrant_id],
"publication-year" => params[:publication_year],
"year-month" => params[:year_month],
"page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(results.to_a.last[:sort]).join(',')) : nil,
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def self.update_datacite_orcid_auto_update(options = {})
break unless response.results.results.length > 0

logger.info "[Update] Updating #{response.results.results.length} datacite-orcid-auto-update events starting with _id #{response.results.to_a.first[:_id]}."
cursor = response.results.to_a.last[:sort].first.to_i
cursor = response.results.to_a.last[:sort]

ids = response.results.results.map(&:obj_id).uniq
OrcidAutoUpdateJob.perform_later(ids, options)
Expand Down

0 comments on commit 5f26597

Please sign in to comment.