Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to v2 of api #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You can create a query object using a parameter hash:

```ruby
DPLA::Query.new :title => 'fruit'
DPLA::Query.new :title => 'fruit', :facets => ['sourceResource.spatial.coordinates:42.3:-71:20mi', 'sourceResource.spatial.state']
```

The actual records themselves are accessible via the `#results` instance method, which returns an array of results. At the moment, those are returned in pages of 100. To access the next page, use the `#next_page` instance method, or `#fetch_page` to fetch an arbitrary page. You can also `#rewind` to go back to the first page.
Expand Down
6 changes: 6 additions & 0 deletions lib/ruby-dpla.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
require "ruby-dpla/version"

require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/object/to_query'
require 'active_support/json'

require "ruby-dpla/query"
require "ruby-dpla/parameters"
2 changes: 1 addition & 1 deletion lib/ruby-dpla/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DPLA
class ParameterError < StandardError; end
class ParameterError < StandardError; end
end
69 changes: 69 additions & 0 deletions lib/ruby-dpla/parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'ruby-dpla/exceptions'


module DPLA
module Parameters


# Here are the default paramters from the DPLA api ( https://github.com/dpla/platform/ )
PARAMETERS = %w[
api_key
facets
q
@id
id
sourceResource
sourceResource.contributor
sourceResource.creator
sourceResource.date.displayDate
sourceResource.date.begin
sourceResource.date.end
sourceResource.description
sourceResource.extent
sourceResource.language.name
sourceResource.language.iso639
sourceResource.physicalMedium
sourceResource.publisher
sourceResource.rights
sourceResource.relation
sourceResource.stateLocatedIn.name
sourceResource.stateLocatedIn.iso3166-2
sourceResource.spatial.name
sourceResource.spatial.country
sourceResource.spatial.region
sourceResource.spatial.county
sourceResource.spatial.state
sourceResource.spatial.city
sourceResource.spatial.iso3166-2
sourceResource.spatial.coordinates
sourceResource.spatial.distance
sourceResource.subject.@id
sourceResource.subject.@type
sourceResource.subject.name
sourceResource.temporal.begin
sourceResource.temporal.end
sourceResource.title
sourceResource.type
dataProvider
hasView
hasView.@id
hasView.format
hasView.rights
isPartOf
isPartOf.@id
isPartOf.name
isShownAt
isShownAt.@id
isShownAt.format
isShownAt.rights
object
object.@id
object.format
object.rights
provider
provider.@id
provider.name
]

end
end
49 changes: 33 additions & 16 deletions lib/ruby-dpla/query.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require 'ruby-dpla/exceptions'
require 'ruby-dpla/parameters'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/object/to_query'
require 'active_support/json'


require 'httparty'
require 'json'
Expand All @@ -7,29 +12,40 @@ module DPLA
class Query
include Enumerable
include HTTParty
base_uri 'http://api.dp.la/v1'
include DPLA::Parameters
base_uri 'http://api.dp.la/v2'
attr_reader :parameters, :response, :page

PARAMETERS = %w[
title description creator type publisher format rights
contributor created spatial temporal source id q page_size
]

def self.parameters_correct? params
params.keys.all? {|key| PARAMETERS.include? key.to_s.split('.').first}
def self.format_parameters params
formatted_params = {}
params.inject(formatted_params) do |h, (k,v)|
if PARAMETERS.include? k.to_s.split('.').first
h[k] = Array.wrap(v).join(",")
elsif PARAMETERS.include? "sourceResource.#{k}"
h["sourceResource.#{k}"] = Array.wrap(v).join(",")
else
raise DPLA::ParameterError, "unrecognized parameter"
end
h
end
formatted_params
end

def initialize parameters
@page = 1

raise DPLA::ParameterError, "unrecognized parameter" unless Query.parameters_correct? parameters
@parameters = parameters
@parameters[:page_size] ||= 100

json = self.class.get('items', :body => parameters).body
parameters[:api_key] ||= ENV["DPLA_API_KEY"]
raise DPLA::ParameterError, "please provide a vaild DPLA API key" unless parameters[:api_key]

@parameters = parameters.except(:page, :page_size)
parameters = self.class.format_parameters(parameters)
@page = parameters[:page] ? parameters[:page].to_i : 1
json = self.class.get('/items', :query => parameters).body
@response = JSON.parse(json)
end

def url
"#{DPLA::Query.base_uri}/items?#{ @parameters.to_query }"
end

def each
return to_enum unless block_given?

Expand Down Expand Up @@ -59,11 +75,12 @@ def fetch_page number
params = @parameters.dup
params[:page] = @page

json = self.class.get('items', :body => params).body
json = self.class.get('/items', :query => params).body
@response = JSON.parse(json)
end

def results; @response['docs']; end
def facets; @response["facets"]; end
def count; @response['count']; end
def start; @response['start']; end
def limit; @response['limit']; end
Expand Down
10 changes: 10 additions & 0 deletions ruby-dpla.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_dependency 'httparty', '>= 0.9.0'
gem.add_dependency "activesupport"

gem.add_development_dependency "bundler", "~> 1.0"
gem.add_development_dependency "shoulda"
gem.add_development_dependency "mocha", "~> 0.13"
gem.add_development_dependency "minitest", "~> 2.12"
gem.add_development_dependency "simplecov"
gem.add_development_dependency "turn", "~> 0.9"
gem.add_development_dependency "webmock"

end
Loading