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, :status]
- @@count_only_param =
'rows=0'
- @@default_service_url =
'http://127.0.0.1:3000'
- @@counts_key =
'counts'
Instance Attribute Summary collapse
-
#service_url ⇒ Object
readonly
Base URL this instance will run RESTful API calls against.
Instance Method Summary collapse
-
#add_params(input_params) ⇒ String
Transform a parameter hash into a RESTful API parameter format.
-
#druid_array(response, params = {}) ⇒ Array{String}
Parses full Hash into an array containing only the druids.
-
#get_apo(apo, params = {}) ⇒ Hash
Get the APO and all objects governed by the APO.
-
#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.
-
#is_alive? ⇒ Boolean
Check to see if the dor-fetcher-service is responding to requests, this is a basic heartbeat checker.
-
#list_all_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository that are accessioned.
-
#list_all_collections ⇒ Hash
Get a Hash of all the collections in the digital repository that are accessioned.
-
#list_registered_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository that are registered.
-
#list_registered_collections ⇒ Hash
Get a Hash of all the collections in the digital repository.
-
#query_api(base, druid, params) ⇒ Hash, Integer
Query a RESTful API and return the JSON result as a Hash.
-
#query_url(base, druid, params) ⇒ String
Synthesize URL from base, druid and params.
-
#service_info ⇒ hash
Return service info (rails env, version deployed, last restart and last deploy).
-
#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
20 21 22 23 24 25 |
# File 'lib/dor-fetcher.rb', line 20 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 @site = RestClient::Resource.new(@service_url) raise "DorFetcher::Client Error! No response from #{@service_url}" unless [:skip_heartbeat] || is_alive? end |
Instance Attribute Details
#service_url ⇒ Object (readonly)
Base URL this instance will run RESTful API calls against
12 13 14 |
# File 'lib/dor-fetcher.rb', line 12 def service_url @service_url end |
Instance Method Details
#add_params(input_params) ⇒ String
Transform a parameter hash into a RESTful API parameter format
164 165 166 167 168 |
# File 'lib/dor-fetcher.rb', line 164 def add_params(input_params) uri = Addressable::URI.new uri.query_values = input_params.select { |key, _val| @@supported_params.include?(key) } '?' + uri.query.gsub('count_only=true', @@count_only_param) end |
#druid_array(response, params = {}) ⇒ Array{String}
Parses full Hash into an array containing only the druids
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dor-fetcher.rb', line 116 def druid_array(response, params = {}) return_list = [] response.each do |key, items| next if key == @@counts_key items.each do |item| next if item['druid'].nil? druid = item['druid'].downcase return_list << (params[:no_prefix] ? druid.gsub('druid:', '') : druid) end end return_list end |
#get_apo(apo, params = {}) ⇒ Hash
Get the APO and all objects governed by the APO
80 81 82 |
# File 'lib/dor-fetcher.rb', line 80 def get_apo(apo, params = {}) query_api('apos', apo, params) end |
#get_collection(collection, params = {}) ⇒ Hash
Get a hash of all members of a collection and the collection itself
46 47 48 |
# File 'lib/dor-fetcher.rb', line 46 def get_collection(collection, params = {}) 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
89 90 91 |
# File 'lib/dor-fetcher.rb', line 89 def get_count_for_apo(apo, params = {}) query_api('apos', apo, params.merge!(:count_only => true)) end |
#get_count_for_collection(collection, params = {}) ⇒ Integer
Get the count of the number of items in a collection, including the collection object itself
54 55 56 |
# File 'lib/dor-fetcher.rb', line 54 def get_count_for_collection(collection, params = {}) query_api('collections', collection, params.merge!(:count_only => true)) end |
#is_alive? ⇒ Boolean
Check to see if the dor-fetcher-service is responding to requests, this is a basic heartbeat checker
36 37 38 39 |
# File 'lib/dor-fetcher.rb', line 36 def is_alive? resp = @site.get 200.eql?(resp.code) && /PASSED/.match?(resp.body) end |
#list_all_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository that are accessioned
95 96 97 |
# File 'lib/dor-fetcher.rb', line 95 def list_all_apos query_api('apos', '', {}) end |
#list_all_collections ⇒ Hash
Get a Hash of all the collections in the digital repository that are accessioned
60 61 62 |
# File 'lib/dor-fetcher.rb', line 60 def list_all_collections query_api('collections', '', {}) end |
#list_registered_apos ⇒ Hash
Get a Hash of all the APOs in the digital repository that are registered
101 102 103 |
# File 'lib/dor-fetcher.rb', line 101 def list_registered_apos query_api('apos', '', :status => 'registered') end |
#list_registered_collections ⇒ Hash
Get a Hash of all the collections in the digital repository
66 67 68 |
# File 'lib/dor-fetcher.rb', line 66 def list_registered_collections query_api('collections', '', :status => 'registered') end |
#query_api(base, druid, params) ⇒ Hash, Integer
Query a RESTful API and return the JSON result as a Hash
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/dor-fetcher.rb', line 145 def query_api(base, druid, params) url = query_url(base, druid, params) begin # We use RestClient::Request.execute here for the longer timeout option resp = RestClient::Request.execute(:method => :get, :url => url, :timeout => 180) rescue RestClient::Exception => e warn "Connection Error with url #{url}: #{e.}" raise e end # RestClient monkey patches its response so it looks like a string, but really isn't. # If you just dd resp.to_i, you'll get the HTML Code, normally 200, not the actually body text you want return resp[0..resp.size].to_i if params[:count_only] == true JSON[resp] # Convert the response JSON to a Ruby Hash end |
#query_url(base, druid, params) ⇒ String
Synthesize URL from base, druid and params
132 133 134 135 136 137 |
# File 'lib/dor-fetcher.rb', line 132 def query_url(base, druid, params) url = "#{@site}/#{base}" url += "/#{druid}" unless druid.nil? || druid.empty? url += add_params(params).to_s unless params.nil? || params.empty? url end |
#service_info ⇒ hash
Return service info (rails env, version deployed, last restart and last deploy)
29 30 31 32 |
# File 'lib/dor-fetcher.rb', line 29 def service_info resp = @site['about/version.json'].get JSON[resp] end |
#total_apo_count ⇒ Integer
Get a Count of all the APOs in the digital repository
107 108 109 |
# File 'lib/dor-fetcher.rb', line 107 def total_apo_count query_api('apos', '', :count_only => true) end |
#total_collection_count ⇒ Integer
Get a Count of all the collections in the digital repository
72 73 74 |
# File 'lib/dor-fetcher.rb', line 72 def total_collection_count query_api('collections', '', :count_only => true) end |