Class: DorFetcher::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new instance of DorFetcher::Client

Examples:

df = DorFetcher::Client.new(:service_url => 'http://SERVICEURL')

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :service_url (String)

    base url for API queries. Default: 127.0.0.1:3000

  • :skip_heartbeat (Boolean)

    skip querying :service_url to confirm API is responsive. Default: false



20
21
22
23
24
25
# File 'lib/dor-fetcher.rb', line 20

def initialize(options = {})
  # TODO: Check for a well formed URL and a 200 from the destination before just accepting this
  @service_url = options[:service_url] || @@default_service_url
  @site = RestClient::Resource.new(@service_url)
  raise "DorFetcher::Client Error! No response from #{@service_url}" unless options[:skip_heartbeat] || is_alive?
end

Instance Attribute Details

#service_urlObject (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

Parameters:

  • input_params (Hash{Symbol=>Object})

    The existing parameters, eg time and tag

Returns:

  • (String)

    parameters in the Hash now formatted into a RESTful parameter string



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

Parameters:

  • response (Hash)

    Hash as returned by query_api

  • params (Hash{Symbol=>Boolean}) (defaults to: {})

    options

Options Hash (params):

  • :no_prefix (Boolean)

    if true (default), remove the ‘druid:’ prefix on all druids

Returns:

  • (Array{String})

    all druids in the supplied Hash



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

Parameters:

  • apo (String)

    pid/druid of the APO

  • params (Hash) (defaults to: {})

    we expect :count_only or any of @@supported_params

Returns:

  • (Hash)

    All objects governed by the APO including: pid/druid, title, date last modified, and count



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

Parameters:

  • collection (String)

    we expect pid/druid

  • params (Hash) (defaults to: {})

    we expect :count_only or any of @@supported_params

Returns:

  • (Hash)

    Hash of all objects in the collection including: pid/druid, title, date last modified, and count



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

Parameters:

  • apo (String)

    we expect pid/druid

  • params (Hash) (defaults to: {})

    we expect :count_only or any of @@supported_params

Returns:

  • (Integer)

    Number found



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

Parameters:

  • collection (String)

    we expect pid/druid

  • params (Hash) (defaults to: {})

    any of @@supported_params

Returns:

  • (Integer)

    Number found



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

Returns:

  • (Boolean)

    True for a service that responds, False for a service that does not.



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_aposHash

Get a Hash of all the APOs in the digital repository that are accessioned

Returns:

  • (Hash)

    All APOs including: pid/druid, title, date last modified, and count



95
96
97
# File 'lib/dor-fetcher.rb', line 95

def list_all_apos
  query_api('apos', '', {})
end

#list_all_collectionsHash

Get a Hash of all the collections in the digital repository that are accessioned

Returns:

  • (Hash)

    All collections including: pid/druid, title, date last modified, and count



60
61
62
# File 'lib/dor-fetcher.rb', line 60

def list_all_collections
  query_api('collections', '', {})
end

#list_registered_aposHash

Get a Hash of all the APOs in the digital repository that are registered

Returns:

  • (Hash)

    All registered APOs including: pid/druid, title, date last modified, and count



101
102
103
# File 'lib/dor-fetcher.rb', line 101

def list_registered_apos
  query_api('apos', '', :status => 'registered')
end

#list_registered_collectionsHash

Get a Hash of all the collections in the digital repository

Returns:

  • (Hash)

    All registered collections including: pid/druid, title, date last modified, and count



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

Parameters:

  • base (String)

    The name of controller of the Rails App you wish to route to

  • druid (String)

    The druid/pid of the object you wish to query, or empty string for no specific druid

  • params (Hash)

    we expect :count_only or any of @@supported_params

Options Hash (params):

  • :count_only (Hash)

Returns:

  • (Hash, Integer)

    All objects governed by the APO including pid/druid, title, date last modified, and count – or just the count if :count_only



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.message}"
    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

Returns:

  • (String)

    URL

See Also:



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_infohash

Return service info (rails env, version deployed, last restart and last deploy)

Returns:

  • (hash)

    Hash containing service info



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_countInteger

Get a Count of all the APOs in the digital repository

Returns:

  • (Integer)

    Number of all APOs



107
108
109
# File 'lib/dor-fetcher.rb', line 107

def total_apo_count
  query_api('apos', '', :count_only => true)
end

#total_collection_countInteger

Get a Count of all the collections in the digital repository

Returns:

  • (Integer)

    Number of all collections



72
73
74
# File 'lib/dor-fetcher.rb', line 72

def total_collection_count
  query_api('collections', '', :count_only => true)
end