Class: Replication::ArchiveCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/replication/archive_catalog.rb

Overview

A wrapper class based on RestClient used to interface with the Archive Catalog service.

The default RestClient behavior is:

  • for results code between 200 and 207 a RestClient::Response will be returned

  • for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head

  • for result code 303 the redirection will be followed and the request transformed into a get

  • for other cases a RestClient::Exception holding the Response will be raised

But we are using a technique that forces RestClient to always provide the response

RestClient::Response has these instance methods (some inherited from AbstractResponse):

  • args

  • body

  • code (e.g. 204)

  • description (e.g. “204 No Content | 0 bytes”)

  • headers

  • net_http_res

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.root_uriString

Returns The base or home URL of the Archive Catalog web service.

Returns:

  • The base or home URL of the Archive Catalog web service



37
38
39
# File 'lib/replication/archive_catalog.rb', line 37

def root_uri
  @root_uri
end

.timeoutInteger

Returns seconds to wait for a response or to open a connection. Value nil disables the timeout.

Returns:

  • seconds to wait for a response or to open a connection. Value nil disables the timeout.



40
41
42
# File 'lib/replication/archive_catalog.rb', line 40

def timeout
  @timeout
end

Class Method Details

.add_or_update_item(table, hash) ⇒ Hash

update an existing database record or add a new one using the data provided in the hash.

Parameters:

  • name of the database table

  • the item data to be added to the database table

Returns:

  • result containing the item data as if a GET were performed. The HTTP response code for success is 201 (Created).

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/replication/archive_catalog.rb', line 72

def add_or_update_item(table,hash)
  payload = hash.to_json
  headers = {:content_type => :json, :accept => :json}
  # Don't raise RestClient::Exception but return the response
  response = root_resource["#{table}.json"].post(payload, headers) {|response, request, result| response }
  case response.code.to_s
    when '201'
      JSON.parse(response.body)
    else
      raise response.description
  end
end

.get_item(table, id) ⇒ Hash

Get the item record from the specified table for the specified primary key.

Parameters:

  • name of the database table

  • primary key for the item in the database table

Returns:

  • the row (in key,value hash) from the specified table for the specified identifier. Response body contains the item data in JSON format, which is converted to a hash.

See Also:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/replication/archive_catalog.rb', line 53

def get_item(table,id)
  # Don't raise RestClient::Exception but return the response
  headers = {:accept => 'application/json'}
  response = root_resource["#{table}/#{id}.json"].get(headers) {|response, request, result| response }
  case response.code.to_s
    when '200'
      JSON.parse(response.body)
    else
      raise response.description
  end
end

.root_resourceObject

The base RestClient resource to be used for requests



43
44
45
# File 'lib/replication/archive_catalog.rb', line 43

def root_resource
  RestClient::Resource.new(@root_uri, {:open_timeout => @timeout, :timeout => @timeout})
end

.update_item(table, id, hash) ⇒ Boolean

Update the database columns for the specified item using the hash data.

Parameters:

  • name of the database table

  • primary key for the item in the database table

  • the item data to be updated in the database table

Returns:

  • true if the HTTP response code is 204, per specification for PATCH or PUT request types. Response body is empty, per same specification.

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/replication/archive_catalog.rb', line 93

def update_item(table,id,hash)
  payload = hash.to_json
  headers = {:content_type => :json}
  # Don't raise RestClient::Exception but return the response
  response = root_resource["#{table}/#{id}.json"].patch(payload, headers) {|response, request, result| response }
  case response.code.to_s
    when '204'
      true
    else
      raise response.description
  end
end