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_param =
"?rows=0"
@@default_service_url =
'http://127.0.0.1:3000'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new instance of DorFetcher::Client url for API queries. Defaults to 127.0.0.1:3000

Examples:

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

Parameters:

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

    Currently supports only :service_url, the base



17
18
19
20
# File 'lib/dor-fetcher.rb', line 17

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
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

Parameters:

  • params (Hash)

    existing parameters, eg time and tag

Returns:

  • (Hash)

    the params Hash plus the key/value set :count_only=>true



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

Parameters:

  • input_params (Hash)

    existing parameters, eg time and tag

Returns:

  • (String)

    parameters in the Hash now formatted into a RESTful parameter string



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

Parameters:

  • response (Hash)

    Hash as returned by query_api

Returns:

  • (Array)

    the array listing all druids in the supplied Hash



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

Parameters:

  • apo (String)

    pid/druid of the APO

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

    we expect :count_only or any of @@supported_params

Returns:

  • (Hash)

    Hash of all objects governed by the APO including



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

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



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

Parameters:

  • apo (String)

    we expect pid/druid

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

    we expect :count_only or any of @@supported_params

Returns:

  • (Integer)

    Number found



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

Parameters:

  • collection (String)

    we expect pid/druid

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

    we expect :count_only or any of @@supported_params

Returns:

  • (Integer)

    Number found



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_aposHash

Get a Hash of all the APOs in the digital repository date last modified, and count

Returns:

  • (Hash)

    Hash of all APOs including pid/druid, title,



76
77
78
# File 'lib/dor-fetcher.rb', line 76

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

#list_all_collectionsHash

Get a Hash of all the collections in the digital repository date last modified, and count

Returns:

  • (Hash)

    Hash of all collections including pid/druid, title,



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

Parameters:

  • base (String)

    The name of controller of the Rails App you wish to

  • druid (String)

    The druid/pid of the object you wish to query,

  • params (Hash)

    we expect :count_only or any of @@supported_params

Returns:

  • (Hash)

    Hash of all objects governed by the APO including



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_urlString

Get the Base URL this instance will run RESTful API calls against

Returns:

  • (String)

    the url



156
157
158
# File 'lib/dor-fetcher.rb', line 156

def service_url
  return @service_url
end

#total_apo_countInteger

Get a Count of all the APOs in the digital repository

Returns:

  • (Integer)

    Number of all APOs



82
83
84
# File 'lib/dor-fetcher.rb', line 82

def total_apo_count
  return 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



51
52
53
# File 'lib/dor-fetcher.rb', line 51

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