Class: Replication::ArchiveCatalog
- Inherits:
-
Object
- Object
- Replication::ArchiveCatalog
- 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
-
.root_uri ⇒ String
The base or home URL of the Archive Catalog web service.
-
.timeout ⇒ Integer
Seconds to wait for a response or to open a connection.
Class Method Summary collapse
-
.add_or_update_item(table, hash) ⇒ Hash
update an existing database record or add a new one using the data provided in the hash.
-
.get_item(table, id) ⇒ Hash
Get the item record from the specified table for the specified primary key.
-
.root_resource ⇒ Object
The base RestClient resource to be used for requests.
-
.update_item(table, id, hash) ⇒ Boolean
Update the database columns for the specified item using the hash data.
Class Attribute Details
.root_uri ⇒ String
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 |
.timeout ⇒ Integer
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.
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.
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_resource ⇒ Object
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.
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 |