Skip to content

Commit

Permalink
Merge pull request #596 from datacite/organization-query
Browse files Browse the repository at this point in the history
Adding support for filtering organizations by types or country
  • Loading branch information
Martin Fenner authored Aug 5, 2020
2 parents d73dca4 + f84d678 commit 7646067
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5351,6 +5351,11 @@ type Organization implements ActorItem {
"""
type: String!

"""
The type of organization.
"""
types: [String!]

"""
URL of the organization.
"""
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class OrganizationType < BaseObject

field :identifiers, [IdentifierType], null: true, description: "The identifier(s) for the organization."
field :url, [Url], null: true, hash_key: "links", description: "URL of the organization."
field :types, [String], null: true, description: "The type of organization."
field :address, AddressType, null: true, description: "Physical address of the organization."
field :view_count, Integer, null: true, description: "The number of views according to the Counter Code of Practice."
field :download_count, Integer, null: true, description: "The number of downloads according to the Counter Code of Practice."
Expand Down
4 changes: 3 additions & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ def data_catalogs(**args)
field :organizations, OrganizationConnectionWithTotalType, null: false do
argument :query, String, required: false
argument :after, String, required: false
argument :types, String, required: false
argument :country, String, required: false
end

def organizations(**args)
response = Organization.query(args[:query], offset: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil)
response = Organization.query(args[:query], types: args[:types], country: args[:country], offset: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil)
HashConnection.new(response, context: self.context, after: args[:after])
end

Expand Down
15 changes: 11 additions & 4 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ def self.find_by_id(id)
def self.query(query, options={})
# rows = options[:limit] || 20
page = options[:offset] || 1
types = options[:types]
country = options[:country]

if query.present?
url = "https://api.ror.org/organizations?query=#{query}&page=#{page}"
else
url = "https://api.ror.org/organizations?page=#{page}"
url = "https://api.ror.org/organizations?page=#{page}"
url += "&query=#{query}" if query.present?
if types.present? && country.present?
url += "&filter=types:#{types.upcase_first},country.country_code:#{country.upcase}"
elsif types.present?
url += "&filter=types:#{types.upcase_first}"
elsif country.present?
url += "&filter=country.country_code:#{country.upcase}"
end

response = Maremma.get(url, host: true)
Expand Down Expand Up @@ -63,6 +69,7 @@ def self.parse_message(id: nil, message: nil)
Hashie::Mash.new({
id: id,
type: "Organization",
types: message["types"],
name: message["name"],
aliases: message["aliases"],
acronyms: message["acronyms"],
Expand Down
114 changes: 114 additions & 0 deletions spec/fixtures/vcr_cassettes/Organization/query/found_by_country_gb.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 7646067

Please sign in to comment.