Module: Rubydora::RestApiClient

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Benchmarkable, FedoraUrlHelpers
Included in:
Repository
Defined in:
lib/rubydora/rest_api_client.rb,
lib/rubydora/rest_api_client/v33.rb

Overview

Provide low-level access to the Fedora Commons REST API

Defined Under Namespace

Modules: V33

Constant Summary collapse

VALID_CLIENT_OPTIONS =
[:user, :password, :timeout, :open_timeout, :ssl_client_cert, :ssl_client_key]

Constants included from FedoraUrlHelpers

FedoraUrlHelpers::API_DOCUMENTATION

Instance Method Summary collapse

Methods included from FedoraUrlHelpers

#datastream_content_url, #datastream_history_url, #datastream_url, #dissemination_url, #export_object_url, #find_objects_url, #new_object_relationship_url, #next_pid_url, #object_relationship_url, #object_url, #object_versions_url, #object_xml_url, #url_for, #validate_object_url

Instance Method Details

#add_datastream(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


263
264
265
266
267
268
269
270
271
272
# File 'lib/rubydora/rest_api_client.rb', line 263

def add_datastream options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  file = options.delete(:content)
  content_type = options.delete(:content_type) || options[:mimeType] || (MIME::Types.type_for(file.path).first if file.respond_to? :path) || 'application/octet-stream'
  run_hook :before_add_datastream, :pid => pid, :dsid => dsid, :file => file, :options => options
  client[datastream_url(pid, dsid, options)].post file, :content_type => content_type.to_s, :multipart => true
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#add_relationship(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


329
330
331
332
333
334
335
# File 'lib/rubydora/rest_api_client.rb', line 329

def add_relationship options = {}
  pid = options.delete(:pid) || options[:subject]
  run_hook :before_add_relationship, :pid => pid, :options => options
  client[new_object_relationship_url(pid, options)].post nil
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#client(config = {}) ⇒ RestClient::Resource

Create an authorized HTTP client for the Fedora REST API TODO trap for these errors specifically: RestClient::Request::Unauthorized, Errno::ECONNREFUSED

Parameters:

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

Options Hash (config):

  • :url (String)
  • :user (String)
  • :password (String)

Returns:

  • (RestClient::Resource)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubydora/rest_api_client.rb', line 49

def client config = {}
  client_config = self.config.merge(config)
  client_config.symbolize_keys!
  if config.empty? or @config_hash.nil? or (client_config.hash == @config_hash)
    @config_hash = client_config.hash
    url = client_config[:url]
    client_config.delete_if { |k,v| not VALID_CLIENT_OPTIONS.include?(k) }
    client_config[:open_timeout] ||= client_config[:timeout]
    @client ||= RestClient::Resource.new(url, client_config)
  else
    raise ArgumentError, "Attemping to re-initialize #{self.class}#client with different configuration parameters"
  end
end

#datastream(options = {}) ⇒ String Also known as: datastreams

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)
  • :asOfDateTime (String)
  • :validateChecksum (String)

Returns:

  • (String)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rubydora/rest_api_client.rb', line 183

def datastream options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  options[:format] ||= 'xml'
  val = nil
  message = dsid.nil? ? "Loaded datastream list for #{pid}" : "Loaded datastream #{pid}/#{dsid}"
  benchmark message, :level=>:debug do
    val = client[datastream_url(pid, dsid, options)].get
  end

  val
rescue RestClient::Unauthorized => e
  logger.error "Unauthorized at #{client.url}/#{datastream_url(pid, dsid, options)}"
  raise e
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#datastream_dissemination(options = {}, &block_response) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rubydora/rest_api_client.rb', line 242

def datastream_dissemination options = {}, &block_response
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  method = options.delete(:method)
  method ||= :get
  raise self.class.name + "#datastream_dissemination requires a DSID" unless dsid
  if block_given?
    resource = safe_subresource(datastream_content_url(pid, dsid, options), :block_response => block_response)
  else
    resource = client[datastream_content_url(pid, dsid, options)]
  end
  resource.send(method)
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#datastream_versions(options = {}) ⇒ String Also known as: datastream_history

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rubydora/rest_api_client.rb', line 222

def datastream_versions options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  raise ArgumentError, "Must supply dsid" unless dsid
  options[:format] ||= 'xml'
  client[datastream_history_url(pid, dsid, options)].get
rescue RestClient::ResourceNotFound => e
  #404 Resource Not Found: No datastream history could be found. There is no datastream history for the digital object "changeme:1" with datastream ID of "descMetadata
  return nil
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#dissemination(options = {}, &block_response) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :sdef (String)
  • :method (String)

Returns:

  • (String)


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/rubydora/rest_api_client.rb', line 355

def dissemination options = {}, &block_response
  pid = options.delete(:pid)
  sdef = options.delete(:sdef)
  method = options.delete(:method)
  options[:format] ||= 'xml' unless pid and sdef and method
  if block_given?
    resource = safe_subresource(dissemination_url(pid,sdef,method,options), :block_response => block_response)
  else
    resource = client[dissemination_url(pid,sdef,method,options)]
  end
  resource.get

rescue Exception => exception
    rescue_with_handler(exception) || raise

end

#export(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


119
120
121
122
123
124
# File 'lib/rubydora/rest_api_client.rb', line 119

def export options = {}
  pid = options.delete(:pid)
  client[export_object_url(pid, options)].get
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#find_objects(options = {}, &block_response) ⇒ String

Parameters:

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

Returns:

  • (String)


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubydora/rest_api_client.rb', line 76

def find_objects options = {}, &block_response
  raise ArgumentError,"Cannot have both :terms and :query parameters" if options[:terms] and options[:query]
  options[:resultFormat] ||= 'xml'

  resource = client[find_objects_url(options)]
  if block_given?
    resource.options[:block_response] = block_response
  end 
  return resource.get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#ingest(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


105
106
107
108
109
110
111
112
113
# File 'lib/rubydora/rest_api_client.rb', line 105

def ingest options = {}
  pid = options.delete(:pid) || 'new'
  file = options.delete(:file)
  assigned_pid = client[object_url(pid, options)].post file, :content_type => 'text/xml'
  run_hook :after_ingest, :pid => assigned_pid, :file => file, :options => options
  assigned_pid
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#modify_datastream(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rubydora/rest_api_client.rb', line 279

def modify_datastream options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  file = options.delete(:content)
  content_type = options.delete(:content_type) || options[:mimeType] || (MIME::Types.type_for(file.path).first if file.respond_to? :path) || 'application/octet-stream'

  rest_client_options = {}
  if file
    rest_client_options[:multipart] = true
    rest_client_options[:content_type] = content_type
  end

  run_hook :before_modify_datastream, :pid => pid, :dsid => dsid, :file => file, :content_type => content_type, :options => options
  client[datastream_url(pid, dsid, options)].put(file, rest_client_options)

rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#modify_object(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


130
131
132
133
134
135
136
# File 'lib/rubydora/rest_api_client.rb', line 130

def modify_object options = {}
  pid = options.delete(:pid)
  run_hook :before_modify_object, :pid => pid, :options => options
  client[object_url(pid, options)].put nil
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#next_pid(options = {}) ⇒ String

Parameters:

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

Returns:

  • (String)


66
67
68
69
70
71
# File 'lib/rubydora/rest_api_client.rb', line 66

def next_pid options = {}
  options[:format] ||= 'xml'
  client[next_pid_url(options)].post nil
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#object(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


93
94
95
96
97
98
99
# File 'lib/rubydora/rest_api_client.rb', line 93

def object options = {}
  pid = options.delete(:pid)
  options[:format] ||= 'xml'
  client[object_url(pid, options)].get
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#object_versions(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


154
155
156
157
158
159
160
161
# File 'lib/rubydora/rest_api_client.rb', line 154

def object_versions options = {}
  pid = options.delete(:pid)
  options[:format] ||= 'xml'
  raise ArgumentError, "Must have a pid" unless pid
  client[object_versions_url(pid, options)].get
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#object_xml(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


167
168
169
170
171
172
173
174
# File 'lib/rubydora/rest_api_client.rb', line 167

def object_xml options = {}
  pid = options.delete(:pid)
  raise ArgumentError, "Missing required parameter :pid" unless pid
  options[:format] ||= 'xml'
  client[object_xml_url(pid, options)].get
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#purge_datastream(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


303
304
305
306
307
308
309
310
# File 'lib/rubydora/rest_api_client.rb', line 303

def purge_datastream options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  run_hook :before_purge_datastream, :pid => pid, :dsid => dsid
  client[datastream_url(pid, dsid, options)].delete
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#purge_object(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


142
143
144
145
146
147
148
# File 'lib/rubydora/rest_api_client.rb', line 142

def purge_object options = {}
  pid = options.delete(:pid)
  run_hook :before_purge_object, :pid => pid, :options => options
  client[object_url(pid, options)].delete
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#purge_relationship(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


341
342
343
344
345
346
347
# File 'lib/rubydora/rest_api_client.rb', line 341

def purge_relationship options = {}
  pid = options.delete(:pid) || options[:subject]
  run_hook :before_purge_relationship, :pid => pid, :options => options
  client[object_relationship_url(pid, options)].delete
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#relationships(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


316
317
318
319
320
321
322
323
# File 'lib/rubydora/rest_api_client.rb', line 316

def relationships options = {}
  pid = options.delete(:pid) || options[:subject]
  raise ArgumentError, "Missing required parameter :pid" unless pid
  options[:format] ||= 'xml'
  client[object_relationship_url(pid, options)].get
rescue Exception => exception
    rescue_with_handler(exception) || raise
end

#safe_subresource(subresource, options = Hash.new) ⇒ Object



372
373
374
375
376
377
378
379
380
381
# File 'lib/rubydora/rest_api_client.rb', line 372

def safe_subresource(subresource, options=Hash.new)
  url = client.concat_urls(client.url, subresource)
  options = client.options.dup.merge! options
  block = client.block
  if block
    client.class.new(url, options, &block)
  else
    client.class.new(url, options)
  end
end

#set_datastream_options(options = {}) ⇒ String

Parameters:

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

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


208
209
210
211
212
213
214
215
# File 'lib/rubydora/rest_api_client.rb', line 208

def set_datastream_options options = {}
  pid = options.delete(:pid)
  dsid = options.delete(:dsid)
  run_hook :before_set_datastream_options, :pid => pid, :dsid => dsid, :options => options
  client[datastream_url(pid, dsid, options)].put nil
rescue Exception => exception
    rescue_with_handler(exception) || raise
end