Skip to content

Commit

Permalink
standardize country and language codes for graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 15, 2019
1 parent 5cc01e5 commit 43e3fc5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/graphql/types/country_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class CountryType < BaseObject
description "Information about countries"

field :id, String, null: true, description: "Country code"
field :code, String, null: true, description: "Country ISO 3166-1 code"
field :name, String, null: true, description: "Country name"
end
2 changes: 1 addition & 1 deletion app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FunderType < BaseObject
field :id, ID, null: false, description: "Crossref Funder ID"
field :name, String, null: false, description: "Funder name"
field :alternate_name, [String], null: true, description: "Alternate funder names"
field :country, String, null: true, description: "Country where funder is located"
field :country, CountryType, null: true, description: "Country where funder is located"
field :date_modified, String, null: false, description: "Date information was last updated"
field :datasets, FunderDatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/label_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class LabelType < BaseObject
description "Information about labels"

field :iso639, ID, null: false, description: "Label language"
field :code, ID, null: false, description: "Label language ISO 639-1 code"
field :name, String, null: true, method: :label, description: "Label name"
end
30 changes: 19 additions & 11 deletions app/graphql/types/provider_type.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true

class ProviderType < BaseObject
description "Information about members"
description "Information about providers"

field :id, ID, null: false, hash_key: "uid", description: "Unique identifier for each member"
field :name, String, null: false, description: "Member name"
field :id, ID, null: false, hash_key: "uid", description: "Unique identifier for each provider"
field :name, String, null: false, description: "Provider name"
field :ror_id, String, null: false, description: "Research Organization Registry (ROR) identifier"
field :description, String, null: true, description: "Description of the member"
field :website, String, null: true, description: "Website of the member"
field :contact_name, String, null: true, description: "Member contact name"
field :contact_email, String, null: true, description: "Member contact email"
field :logo_url, String, null: true, description: "URL for the member logo"
field :region, String, null: true, description: "Geographic region where the member is located"
field :country, String, null: true, description: "Country where the member is located"
field :description, String, null: true, description: "Description of the provider"
field :website, String, null: true, description: "Website of the provider"
field :contact_name, String, null: true, description: "Provider contact name"
field :contact_email, String, null: true, description: "Provider contact email"
field :logo_url, String, null: true, description: "URL for the provider logo"
field :region, String, null: true, description: "Geographic region where the provider is located"
field :country, CountryType, null: true, description: "Country where the provider is located"
field :organization_type, String, null: true, description: "Type of organization"
field :focus_area, String, null: true, description: "Field of science covered by member"
field :focus_area, String, null: true, description: "Field of science covered by provider"
field :joined, String, null: true, description: "Date provider joined DataCite"
field :prefixes, PrefixConnectionWithMetaType, null: false, description: "Prefixes managed by the provider", connection: true, max_page_size: 100 do
argument :query, String, required: false
Expand All @@ -31,6 +31,14 @@ class ProviderType < BaseObject
argument :after, String, required: false
end

def country
return {} unless object.country_code.present?
{
code: object.country_code,
name: ISO3166::Country[object.country_code].name
}.compact
end

def prefixes(**args)
collection = object.provider_prefixes.joins(:prefix)
collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?
Expand Down
10 changes: 6 additions & 4 deletions app/models/funder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ def self.query(query, options={})

def self.parse_message(id: nil, message: nil)
if message["location"].present?
location = {
"country" => message["location"]
c = ISO3166::Country.find_country_by_name(message["location"])
country = {
"code" => c.alpha2,
"name" => message["location"]
}
else
location = nil
country = nil
end

{
id: id,
name: message["name"],
alternate_name: message["alt-names"],
country: message["location"],
country: country,
date_modified: "2019-04-18T00:00:00Z" }.compact
end
end
7 changes: 4 additions & 3 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ def self.query(query, options={})

def self.parse_message(id: nil, message: nil)
country = {
id: message.dig("country", "country_code"),
code: message.dig("country", "country_code"),
name: message.dig("country", "country_name") }.compact

labels = Array.wrap(message["labels"]).map do |label|
code = label["iso639"].present? ? label["iso639"].upcase : nil
{
iso639: label["iso639"],
label: label["label"] }.compact
code: code,
name: label["label"] }.compact
end

{
Expand Down
4 changes: 2 additions & 2 deletions spec/models/funder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
funders = Funder.query(query)
expect(funders.dig(:meta, "total")).to eq(19662)
expect(funders[:data].size).to eq(100)
expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: "United States", date_modified: "2019-04-18T00:00:00Z")
expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: {"code"=>"US", "name"=>"United States"}, date_modified: "2019-04-18T00:00:00Z")
end

it "limit" do
query = nil
funders = Funder.query(query, limit: 10)
expect(funders.dig(:meta, "total")).to eq(19662)
expect(funders[:data].size).to eq(10)
expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: "United States", date_modified: "2019-04-18T00:00:00Z")
expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: {"code"=>"US", "name"=>"United States"}, date_modified: "2019-04-18T00:00:00Z")
end

it "found" do
Expand Down
6 changes: 3 additions & 3 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
id = "https://ror.org/0521rfb23"
organizations = Organization.find_by_id(id)
expect(organizations[:data].size).to eq(1)
expect(organizations[:data].first).to eq(:id=>"https://ror.org/0521rfb23", :name=>"Lincoln University", :aliases=>["Ashmun Institute"], :acronyms=>["LU"], :labels=>[{:iso639=>"es", :label=>"Universidad Lincoln"}], :links=>["http://www.lincoln.edu/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(Pennsylvania)", :country=>{:id=>"US", :name=>"United States"})
expect(organizations[:data].first).to eq(:id=>"https://ror.org/0521rfb23", :name=>"Lincoln University", :aliases=>["Ashmun Institute"], :acronyms=>["LU"], :labels=>[{:code=>"ES", :name=>"Universidad Lincoln"}], :links=>["http://www.lincoln.edu/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(Pennsylvania)", :country=>{:code=>"US", :name=>"United States"})
end

it "not found" do
Expand All @@ -23,15 +23,15 @@
organizations = Organization.query(query)
expect(organizations.dig(:meta, "total")).to eq(91625)
expect(organizations[:data].size).to eq(20)
expect(organizations[:data].first).to eq(:id=>"https://ror.org/00xqf8t64", :name=>"Padjadjaran University", :aliases=>["Padjadjaran University"], :acronyms=>["UNPAD"], :labels=> [{:iso639=>"id", :label=>"Universitas Padjadjaran"}], :links=>["http://www.unpad.ac.id/en/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Padjadjaran_University", :country=>{:id=>"ID", :name=>"Indonesia"})
expect(organizations[:data].first).to eq(:id=>"https://ror.org/00xqf8t64", :name=>"Padjadjaran University", :aliases=>["Padjadjaran University"], :acronyms=>["UNPAD"], :labels=> [{:code=>"ID", :name=>"Universitas Padjadjaran"}], :links=>["http://www.unpad.ac.id/en/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Padjadjaran_University", :country=>{:code=>"ID", :name=>"Indonesia"})
end

it "found" do
query = "lincoln university"
organizations = Organization.query(query)
expect(organizations.dig(:meta, "total")).to eq(10475)
expect(organizations[:data].size).to eq(20)
expect(organizations[:data].first).to eq(:id=>"https://ror.org/04ps1r162", :name=>"Lincoln University", :aliases=>[], :acronyms=>[], :labels=>[{:iso639=>"mi", :label=>"Te Whare Wanaka o Aoraki"}], :links=>["http://www.lincoln.ac.nz/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(New_Zealand)", :country=>{:id=>"NZ", :name=>"New Zealand"})
expect(organizations[:data].first).to eq(:id=>"https://ror.org/04ps1r162", :name=>"Lincoln University", :aliases=>[], :acronyms=>[], :labels=>[{:code=>"MI", :name=>"Te Whare Wanaka o Aoraki"}], :links=>["http://www.lincoln.ac.nz/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(New_Zealand)", :country=>{:code=>"NZ", :name=>"New Zealand"})
end

it "not found" do
Expand Down

0 comments on commit 43e3fc5

Please sign in to comment.