From 912142d74dfe220397d1d9d0b7c4a4a29deef5db Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 13 May 2019 06:52:43 +0200 Subject: [PATCH] handle orcid id as http. #258 --- app/graphql/types/client_type.rb | 2 +- app/graphql/types/doi_item.rb | 16 ++++++++-------- app/graphql/types/event_data_edge.rb | 11 ++++++++++- ...searcher_dataset_connection_with_meta_type.rb | 8 +++++++- ...cher_publication_connection_with_meta_type.rb | 8 +++++++- ...earcher_software_connection_with_meta_type.rb | 8 +++++++- app/graphql/types/researcher_type.rb | 12 +++++++++--- app/models/concerns/helpable.rb | 6 ++++++ spec/concerns/helpable_spec.rb | 12 ++++++++++++ 9 files changed, 67 insertions(+), 16 deletions(-) diff --git a/app/graphql/types/client_type.rb b/app/graphql/types/client_type.rb index 63edfbc87..3004a9409 100644 --- a/app/graphql/types/client_type.rb +++ b/app/graphql/types/client_type.rb @@ -17,7 +17,7 @@ class ClientType < GraphQL::Schema::Object argument :first, Int, required: false, default_value: 25 end - field :datasets, [Types::DatasetType], null: false, description: "Datasets managed by the client" do + field :datasets, [DatasetType], null: false, description: "Datasets managed by the client" do argument :query, String, required: false argument :first, Int, required: false, default_value: 25 end diff --git a/app/graphql/types/doi_item.rb b/app/graphql/types/doi_item.rb index 640626c2a..bda93f8fe 100644 --- a/app/graphql/types/doi_item.rb +++ b/app/graphql/types/doi_item.rb @@ -10,19 +10,19 @@ module DoiItem field :titles, [TitleType], null: true, description: "A name or title by which a resource is known" field :publication_year, Int, null: true, description: "The year when the data was or will be made publicly available" field :publisher, String, null: true, description: "The name of the entity that holds, archives, publishes prints, distributes, releases, issues, or produces the resource" - field :subjects, [Types::SubjectType], null: true, description: "Subject, keyword, classification code, or key phrase describing the resource" + field :subjects, [SubjectType], null: true, description: "Subject, keyword, classification code, or key phrase describing the resource" field :resource_type_general, String, null: true, hash_key: "resource_type_id", description: "The general type of a resource" - field :dates, [Types::DateType], null: true, description: "Different dates relevant to the work" + field :dates, [DateType], null: true, description: "Different dates relevant to the work" field :language, String, null: true, description: "The primary language of the resource" - field :identifiers, [Types::IdentifierType], null: true, description: "An identifier or identifiers applied to the resource being registered" - field :related_identifiers, [Types::RelatedIdentifierType], null: true, description: "Identifiers of related resources. These must be globally unique identifiers" - field :types, Types::ResourceTypeType, null: true, description: "The resource type" + field :identifiers, [IdentifierType], null: true, description: "An identifier or identifiers applied to the resource being registered" + field :related_identifiers, [RelatedIdentifierType], null: true, description: "Identifiers of related resources. These must be globally unique identifiers" + field :types, ResourceTypeType, null: true, description: "The resource type" field :formats, [String], null: true, description: "Technical format of the resource" field :sizes, [String], null: true, description: "Size (e.g. bytes, pages, inches, etc.) or duration (extent), e.g. hours, minutes, days, etc., of a resource" field :version, String, null: true, hash_key: "version_info", description: "The version number of the resource" - field :rights, [Types::RightsType], null: true, hash_key: "rights_list", description: "Any rights information for this resource" - field :descriptions, [Types::DescriptionType], null: true, description: "All additional information that does not fit in any of the other categories" - field :funding_references, [Types::FundingType], null: true, description: "Information about financial support (funding) for the resource being registered" + field :rights, [RightsType], null: true, hash_key: "rights_list", description: "Any rights information for this resource" + field :descriptions, [DescriptionType], null: true, description: "All additional information that does not fit in any of the other categories" + field :funding_references, [FundingType], null: true, description: "Information about financial support (funding) for the resource being registered" field :url, String, null: true, description: "The URL registered for the resource" field :client, ClientType, null: true, description: "The client account managing this resource" field :provider, ProviderType, null: true, description: "The provider account managing this resource" diff --git a/app/graphql/types/event_data_edge.rb b/app/graphql/types/event_data_edge.rb index 6ad672d67..313b05e0a 100644 --- a/app/graphql/types/event_data_edge.rb +++ b/app/graphql/types/event_data_edge.rb @@ -1,6 +1,13 @@ # frozen_string_literal: true class EventDataEdge < GraphQL::Relay::Edge + RELATION_TYPES = { + "funds" => "isFundedBy", + "isFundedBy" => "funds", + "authors" => "isAuthoredBy", + "isAuthoredBy" => "authors" + } + def event_data @event_data ||= begin Event.query(nil, subj_id: self.node[:id], obj_id: self.parent[:id])[:data].first @@ -11,8 +18,10 @@ def source event_data[:source_id].underscore.camelcase(:lower) end + # We are switching subj and obj, and thus need to change direction of relation type def relation_type - event_data[:relation_type_id].underscore.camelcase(:lower) + rt = event_data[:relation_type_id].underscore.camelcase(:lower) + RELATION_TYPES.fetch(rt, rt) end def total diff --git a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb index 38f4b61ad..9526b573a 100644 --- a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb @@ -6,6 +6,12 @@ class ResearcherDatasetConnectionWithMetaType < GraphQL::Types::Relay::BaseConne field :total_count, Integer, null: false def total_count - Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Person").fetch(:meta, "total") + Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Dataset-Person").fetch(:meta, "total") + end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" + uri.to_s end end diff --git a/app/graphql/types/researcher_publication_connection_with_meta_type.rb b/app/graphql/types/researcher_publication_connection_with_meta_type.rb index 6f0b00008..984efecaa 100644 --- a/app/graphql/types/researcher_publication_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_publication_connection_with_meta_type.rb @@ -6,6 +6,12 @@ class ResearcherPublicationConnectionWithMetaType < GraphQL::Types::Relay::BaseC field :total_count, Integer, null: false def total_count - Event.query(nil, obj_id: object[:id], citation_type: "JournalArticle-Person").fetch(:meta, "total") + Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "JournalArticle-Person").fetch(:meta, "total") + end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" + uri.to_s end end diff --git a/app/graphql/types/researcher_software_connection_with_meta_type.rb b/app/graphql/types/researcher_software_connection_with_meta_type.rb index 6774fa55d..500e8231f 100644 --- a/app/graphql/types/researcher_software_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_software_connection_with_meta_type.rb @@ -6,6 +6,12 @@ class ResearcherSoftwareConnectionWithMetaType < GraphQL::Types::Relay::BaseConn field :total_count, Integer, null: false def total_count - Event.query(nil, obj_id: object[:id], citation_type: "Person-SoftwareSourceCode").fetch(:meta, "total") + Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Person-SoftwareSourceCode").fetch(:meta, "total") + end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" + uri.to_s end end diff --git a/app/graphql/types/researcher_type.rb b/app/graphql/types/researcher_type.rb index 46a1c3db8..9709794a4 100644 --- a/app/graphql/types/researcher_type.rb +++ b/app/graphql/types/researcher_type.rb @@ -22,21 +22,21 @@ class ResearcherType < BaseObject end def datasets(**args) - ids = Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Person").fetch(:data, []).map do |e| + ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Dataset-Person").fetch(:data, []).map do |e| doi_from_url(e[:subj_id]) end.join(",") Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a end def publications(**args) - ids = Event.query(nil, obj_id: object[:id], citation_type: "Person-ScholarlyArticle").fetch(:data, []).map do |e| + ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Person-ScholarlyArticle").fetch(:data, []).map do |e| doi_from_url(e[:subj_id]) end.join(",") Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a end def softwares(**args) - ids = Event.query(nil, obj_id: object[:id], citation_type: "Person-SoftwareSourceCode").fetch(:data, []).map do |e| + ids = Event.query(nil, obj_id: https_to_http(object[:id]), citation_type: "Person-SoftwareSourceCode").fetch(:data, []).map do |e| doi_from_url(e[:subj_id]) end.join(",") Doi.find_by_ids(ids, page: { number: 1, size: args[:first] }).to_a @@ -48,4 +48,10 @@ def doi_from_url(url) uri.path.gsub(/^\//, "").downcase end end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" + uri.to_s + end end diff --git a/app/models/concerns/helpable.rb b/app/models/concerns/helpable.rb index a44760186..407b160e8 100644 --- a/app/models/concerns/helpable.rb +++ b/app/models/concerns/helpable.rb @@ -106,6 +106,12 @@ def encode_doi(prefix, options={}) def epoch_to_utc(epoch) Time.at(epoch).to_datetime.utc.iso8601 end + + def https_to_http(url) + uri = Addressable::URI.parse(url) + uri.scheme = "http" + uri.to_s + end end module ClassMethods diff --git a/spec/concerns/helpable_spec.rb b/spec/concerns/helpable_spec.rb index f01dcc545..3c615f619 100644 --- a/spec/concerns/helpable_spec.rb +++ b/spec/concerns/helpable_spec.rb @@ -77,6 +77,18 @@ expect(response.body.dig("data", "values")).to eq([{"index"=>1, "type"=>"URL", "data"=>{"format"=>"string", "value"=>"https://blog.datacite.org/"}, "ttl"=>86400, "timestamp"=>"2019-05-10T12:45:27Z"}]) end + context "https to http" do + it "should convert" do + url = "https://orcid.org/0000-0003-1419-2405" + expect(subject.https_to_http(url)).to eq("http://orcid.org/0000-0003-1419-2405") + end + + it "should ignore http" do + url = "http://orcid.org/0000-0003-1419-2405" + expect(subject.https_to_http(url)).to eq(url) + end + end + # it 'should register on save' do # url = "https://blog.datacite.org/" # subject = create(:doi, doi: "10.5438/hpc4-5t22", url: url, client: client, aasm_state: "findable")