Class: DorFetcher::Client
- Inherits:
-
Object
- Object
- DorFetcher::Client
- Defined in:
- lib/dor-fetcher.rb
Constant Summary collapse
- @@supported_params =
[:first_modified, :last_modified]
- @@count_only_param =
"?rows=0"- @@default_service_url =
'http://127.0.0.1:3000'
Instance Method Summary collapse
-
#add_count_only_param(params) ⇒ Hash
Add the parameter so query_api knows only to get a count of the documents in solr.
-
#add_params(input_params) ⇒ String
Transform a parameter hash into a RESTful API parameter format.
-
#druid_array(response) ⇒ Array
Method to parse full Hash into an array containing only the druids.
-
#get_apo(apo, params = {}) ⇒ Hash
Get the APO and all objects governed by the APO pid/druid, title, date last modified, and count.
-
#get_collection(collection, params = {}) ⇒ Hash
Get a hash of all members of a collection and the collection itself.
-
#get_count_for_apo(apo, params = {}) ⇒ Integer
Get the count of the number of objects in an APO, including the APO object itself.
-
#get_count_for_collection(collection, params = {}) ⇒ Integer
Get the count of the number of items in a collection, including the collection object itself.
-
#initialize(options = {}) ⇒ Client
constructor
Create a new instance of DorFetcher::Client url for API queries.
-
#list_all_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository date last modified, and count.
-
#list_all_collections ⇒ Hash
Get a Hash of all the collections in the digital repository date last modified, and count.
-
#query_api(base, druid, params) ⇒ Hash
Query a RESTful API and return the JSON result as a Hash route to or empty string for no specific druid pid/druid, title, date last modified, and count.
-
#service_url ⇒ String
Get the Base URL this instance will run RESTful API calls against.
-
#total_apo_count ⇒ Integer
Get a Count of all the APOs in the digital repository.
-
#total_collection_count ⇒ Integer
Get a Count of all the collections in the digital repository.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a new instance of DorFetcher::Client url for API queries. Defaults to 127.0.0.1:3000
17 18 19 20 |
# File 'lib/dor-fetcher.rb', line 17 def initialize = {} #TODO: Check for a well formed URL and a 200 from the destination before just accepting this @service_url = [:service_url] || @@default_service_url end |
Instance Method Details
#add_count_only_param(params) ⇒ Hash
Add the parameter so query_api knows only to get a count of the documents in solr
149 150 151 152 |
# File 'lib/dor-fetcher.rb', line 149 def add_count_only_param(params) params.store(:count_only, true) return params end |
#add_params(input_params) ⇒ String
Transform a parameter hash into a RESTful API parameter format
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dor-fetcher.rb', line 125 def add_params(input_params) args_string = "" #Handle Count Only args_string << @@count_only_param if input_params[:count_only] == true #If we did not add in a rows=0 param, args_string will have a size of #zero #If we did add in a rows=0 param, this will set count to greater than #zero count = args_string.size @@supported_params.each do |p| operator = "?" operator = "&" if count > 0 args_string << "#{operator}#{p.to_s}=#{input_params[p]}" if input_params[p] != nil count += 1 end return args_string end |
#druid_array(response) ⇒ Array
Method to parse full Hash into an array containing only the druids
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dor-fetcher.rb', line 90 def druid_array(response) return_list = [] j = response j.keys.each do |key| j[key].each do |item| return_list << item['druid'] if item['druid'] != nil end end return return_list end |
#get_apo(apo, params = {}) ⇒ Hash
Get the APO and all objects governed by the APO pid/druid, title, date last modified, and count
60 61 62 |
# File 'lib/dor-fetcher.rb', line 60 def get_apo(apo, params= {}) return query_api('apos', apo, params) end |
#get_collection(collection, params = {}) ⇒ Hash
Get a hash of all members of a collection and the collection itself
pid/druid, title, date last modified, and count
29 30 31 |
# File 'lib/dor-fetcher.rb', line 29 def get_collection(collection, params = {}) return query_api('collections', collection, params) end |
#get_count_for_apo(apo, params = {}) ⇒ Integer
Get the count of the number of objects in an APO, including the APO object itself
69 70 71 |
# File 'lib/dor-fetcher.rb', line 69 def get_count_for_apo(apo, params={}) return query_api('apos', apo, add_count_only_param(params)) end |
#get_count_for_collection(collection, params = {}) ⇒ Integer
Get the count of the number of items in a collection, including the collection object itself
38 39 40 |
# File 'lib/dor-fetcher.rb', line 38 def get_count_for_collection(collection, params = {}) return query_api('collections', collection, add_count_only_param(params)) end |
#list_all_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository date last modified, and count
76 77 78 |
# File 'lib/dor-fetcher.rb', line 76 def list_all_apos return query_api('apos', '', {}) end |
#list_all_collections ⇒ Hash
Get a Hash of all the collections in the digital repository date last modified, and count
45 46 47 |
# File 'lib/dor-fetcher.rb', line 45 def list_all_collections return query_api('collections', '', {}) end |
#query_api(base, druid, params) ⇒ Hash
Query a RESTful API and return the JSON result as a Hash route to or empty string for no specific druid pid/druid, title, date last modified, and count
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dor-fetcher.rb', line 108 def query_api(base, druid, params) url = "#{@service_url}/#{base}/#{druid}#{add_params(params)}" begin resp = Net::HTTP.get_response(URI.parse(url)) rescue raise "Connection Error with url #{url}" end return resp.body.to_i if params[:count_only] == true return JSON[resp.body] #Convert the response JSON to a Ruby Hash end |
#service_url ⇒ String
Get the Base URL this instance will run RESTful API calls against
156 157 158 |
# File 'lib/dor-fetcher.rb', line 156 def service_url return @service_url end |
#total_apo_count ⇒ Integer
Get a Count of all the APOs in the digital repository
82 83 84 |
# File 'lib/dor-fetcher.rb', line 82 def total_apo_count return query_api('apos', '', {:count_only=>true}) end |
#total_collection_count ⇒ Integer
Get a Count of all the collections in the digital repository
51 52 53 |
# File 'lib/dor-fetcher.rb', line 51 def total_collection_count return query_api('collections', '', {:count_only=>true}) end |