Skip to content

Commit

Permalink
handle doi parameter for events. #262
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 7, 2019
1 parent a57710f commit 55f5c13
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
12 changes: 12 additions & 0 deletions app/models/concerns/modelable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Modelable
extend ActiveSupport::Concern

module ClassMethods
def doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, '').downcase
end
end
end
end
5 changes: 5 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Event
# include helper module for PORO models
include Modelable

def self.find_by_id(id)
return { errors: [{ "title" => "No id provided"} ] } unless id.present?

Expand All @@ -19,10 +22,12 @@ def self.find_by_id(id)

def self.query(query, options={})
size = options[:limit] || 100
doi = options[:doi].present? ? doi_from_url(options[:doi]) : nil

url = "https://api.datacite.org/events?page[size]=#{size}"
url += "&relation-type-id=#{options[:relation_type_id]}" if options[:relation_type_id].present?
url += "&source-id=#{options[:source_id]}" if options[:source_id].present?
url += "&doi=#{doi}" if doi.present?

response = Maremma.get(url, accept: "application/vnd.api+json; version=2")

Expand Down
10 changes: 3 additions & 7 deletions app/models/funder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Funder
# include helper module for PORO models
include Modelable

def self.find_by_id(id)
doi = doi_from_url(id)
return [] unless doi.present?
Expand Down Expand Up @@ -47,11 +50,4 @@ def self.parse_message(id: nil, message: nil)
country: message["location"],
date_modified: "2019-04-18T00:00:00Z" }.compact
end

def self.doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, '').downcase
end
end
end
Loading

0 comments on commit 55f5c13

Please sign in to comment.