Skip to content

Commit

Permalink
more query options for repositories. #292
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 16, 2019
1 parent 5d767de commit 028965a
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 11 deletions.
10 changes: 8 additions & 2 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,17 @@ def organization(id:)

field :repositories, RepositoryConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :open, Boolean, required: false
argument :pid, Boolean, required: false
argument :certified, Boolean, required: false
argument :disciplinary, Boolean, required: false
argument :software, String, required: false
argument :subject, String, required: false
argument :first, Int, required: false, default_value: 25
end

def repositories(query: nil, first: nil)
Repository.query(query, limit: first).fetch(:data, [])
def repositories(query: nil, open: nil, pid: nil, certified: nil, disciplinary: nil, software: nil, subject: nil, first: nil)
Repository.query(query, open: open, pid: pid, certified: certified, disciplinary: disciplinary, software: software, subject: subject, limit: first).fetch(:data, [])
end

field :repository, RepositoryType, null: false do
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/repository_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class RepositoryConnectionWithMetaType < BaseConnection
def total_count
args = object.arguments

Repository.query(args[:query], limit: 0).dig(:meta, "total").to_i
Repository.query(args[:query], open: args[:open], pid: args[:pid], certified: args[:certified], subject: args[:subject], software: args[:software], disciplinary: args[:disciplinary], limit: 0).dig(:meta, "total").to_i
end
end
3 changes: 1 addition & 2 deletions app/graphql/types/repository_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class RepositoryType < BaseObject
field :description, String, null: true, description: "Repository description"
field :certificates, [TextType], null: true, description: "Repository certificates"
field :subjects, [SchemeType], null: true, description: "Subjects"
field :types, [TextType], null: true, description: "Repository types"
field :content_types, [SchemeType], null: true, description: "Content types"
field :provider_types, [TextType], null: true, description: "Provider types"
field :keywords, [TextType], null: true, description: "Keywords"
Expand All @@ -37,8 +38,6 @@ class RepositoryType < BaseObject
end

def datasets(**args)
logger = Logger.new(STDOUT)
logger.info doi_from_url(object[:id])
Doi.query(args[:query], repository_id: doi_from_url(object[:id]), resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
end

Expand Down
20 changes: 14 additions & 6 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ def self.find_by_id(id)
end

def self.query(query, options={})
# rows = options[:limit] || 25
limit ||= 25
page ||= 1

if query.present?
url = "https://api.datacite.org/repositories?query=#{query}"
else
url = "https://api.datacite.org/repositories"
end
params = {
query: query,
subject: options[:subject],
open: options[:open],
certified: options[:certified],
pid: options[:pid],
software: options[:software],
disciplinary: options[:disciplinary],
"page[size]" => limit,
"page[number]" => page }.compact

url = "https://api.datacite.org/repositories?" + URI.encode_www_form(params)

response = Maremma.get(url, host: true)

Expand Down
47 changes: 47 additions & 0 deletions spec/fixtures/vcr_cassettes/Repository/query/certified.yml

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions spec/fixtures/vcr_cassettes/Repository/query/open.yml

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions spec/fixtures/vcr_cassettes/Repository/query/pid.yml

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions spec/models/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@
expect(repository[:certificates]).to eq([])
end

it "pid" do
repositories = Repository.query(nil, pid: true)
expect(repositories.dig(:meta, "total")).to eq(651)
expect(repositories[:data].size).to eq(25)
repository = repositories[:data].first
expect(repository[:id]).to eq("https://doi.org/10.17616/r3vg6n")
expect(repository[:re3data_id]).to eq("r3d100010216")
expect(repository[:name]).to eq("4TU.Centre for Research Data")
expect(repository[:url]).to eq("https://researchdata.4tu.nl/en/home/")
expect(repository[:pid_systems]).to eq([{"text"=>"DOI"}])
end

it "certified" do
repositories = Repository.query(nil, certified: true)
expect(repositories.dig(:meta, "total")).to eq(154)
expect(repositories[:data].size).to eq(25)
repository = repositories[:data].first
expect(repository[:id]).to eq("https://doi.org/10.17616/r3vg6n")
expect(repository[:re3data_id]).to eq("r3d100010216")
expect(repository[:name]).to eq("4TU.Centre for Research Data")
expect(repository[:url]).to eq("https://researchdata.4tu.nl/en/home/")
expect(repository[:certificates]).to eq([{"text"=>"DSA"}])
end

it "open" do
repositories = Repository.query(nil, open: true)
expect(repositories.dig(:meta, "total")).to eq(1374)
expect(repositories[:data].size).to eq(25)
repository = repositories[:data].first
expect(repository[:id]).to eq("https://doi.org/10.17616/r3w05r")
expect(repository[:re3data_id]).to eq("r3d100011565")
expect(repository[:name]).to eq("1000 Functional Connectomes Project")
expect(repository[:url]).to eq("http://fcon_1000.projects.nitrc.org/fcpClassic/FcpTable.html")
expect(repository[:data_accesses]).to eq([{"restrictions"=>[], "type"=>"open"}])
end

it "not found" do
query = "xxx"
repositories = Repository.query(query)
Expand Down

0 comments on commit 028965a

Please sign in to comment.