Class: OmekaClient::Client
- Inherits:
-
Object
- Object
- OmekaClient::Client
- Defined in:
- lib/omeka_client/client.rb
Overview
A class to create clients that interact with the Omeka API
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#collections ⇒ Array
Get a list of the Omeka collections.
-
#delete(resource, id, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic DELETE request to the Omeka site.
-
#delete_item(omeka_item) ⇒ Object
Delete the item represented by an OmekaItem instance.
-
#get(resource, id = nil, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic GET request to the Omeka site.
-
#get_hash(resource, id = nil, query = {}) ⇒ Array or Hash
Parse a GET request into a useable format.
-
#initialize(endpoint, api_key = nil) ⇒ OmekaClient
constructor
Sets up a new client to interact with an Omeka site.
-
#items ⇒ Array
Get a list of the Omeka items.
-
#omeka_items(id = nil, query = {}) ⇒ OmekaItem
Get an array or a single Omeka item represented as an OmekaItem class array of all the items.
-
#post(resource, body = nil, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic POST request to the Omeka site.
-
#post_item(omeka_item) ⇒ Object
Create a new item from an OmekaItem instance.
-
#put(resource, id, body, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic DELETE request to the Omeka site.
-
#put_item(omeka_item) ⇒ Object
Update an item using an OmekaItem instance.
-
#resources ⇒ Hash
Get a list of the resources available from the Omeka API.
-
#site ⇒ Hash
Get the description of the Omeka site.
Constructor Details
#initialize(endpoint, api_key = nil) ⇒ OmekaClient
Sets up a new client to interact with an Omeka site
21 22 23 24 25 |
# File 'lib/omeka_client/client.rb', line 21 def initialize(endpoint, api_key = nil ) @endpoint = endpoint @api_key = api_key @connection = Rest::Client.new end |
Instance Attribute Details
#api_key ⇒ Object
10 11 12 |
# File 'lib/omeka_client/client.rb', line 10 def api_key @api_key end |
#connection ⇒ Object
10 11 12 |
# File 'lib/omeka_client/client.rb', line 10 def connection @connection end |
#endpoint ⇒ Object
10 11 12 |
# File 'lib/omeka_client/client.rb', line 10 def endpoint @endpoint end |
Instance Method Details
#collections ⇒ Array
Get a list of the Omeka collections
TODO: Check that items are available in the resources
198 199 200 |
# File 'lib/omeka_client/client.rb', line 198 def collections self.get_hash('collections') end |
#delete(resource, id, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic DELETE request to the Omeka site
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/omeka_client/client.rb', line 85 def delete(resource, id, query = {} ) url = self.endpoint + "/" + resource url += "/" + id.to_s unless id.nil? query[:key] = self.api_key unless self.api_key.nil? # The rest gem that provides out functionality has a bug. The Omeka API # returns 204 No Content on DELETE, indicating that the item has been # successfully deleted but that there is no body to return. The rest # gem assumes there will be a body, so it throws a type error. Until # this is fixed, we just rescue the error and don't worry about it. begin self.connection.delete(url, :params => query) rescue TypeError # Not putting the error to stdout end end |
#delete_item(omeka_item) ⇒ Object
Delete the item represented by an OmekaItem instance
160 161 162 |
# File 'lib/omeka_client/client.rb', line 160 def delete_item(omeka_item) self.delete("items", omeka_item.data.id) end |
#get(resource, id = nil, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic GET request to the Omeka site
39 40 41 42 43 44 |
# File 'lib/omeka_client/client.rb', line 39 def get(resource, id = nil, query = {} ) url = self.endpoint + "/" + resource url += "/" + id.to_s unless id.nil? query[:key] = self.api_key unless self.api_key.nil? self.connection.get(url, :params => query) end |
#get_hash(resource, id = nil, query = {}) ⇒ Array or Hash
Parse a GET request into a useable format
56 57 58 59 60 61 |
# File 'lib/omeka_client/client.rb', line 56 def get_hash(resource, id = nil, query = {} ) response = self.get(resource, id, query) if response.code == 200 JSON.parse(response.body) end end |
#items ⇒ Array
Get a list of the Omeka items
TODO: Check that items are available in the resources
188 189 190 |
# File 'lib/omeka_client/client.rb', line 188 def items self.get_hash('items') end |
#omeka_items(id = nil, query = {}) ⇒ OmekaItem
Get an array or a single Omeka item represented as an OmekaItem class array of all the items. or an array of OmekaItems
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/omeka_client/client.rb', line 130 def omeka_items(id = nil, query = {} ) response = self.get_hash('items', id = id, query = query) if id.nil? items = Array.new response.each do |item_hash| items.push OmekaItem.new(item_hash) end return items else OmekaItem.new(response) end end |
#post(resource, body = nil, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic POST request to the Omeka site
71 72 73 74 75 |
# File 'lib/omeka_client/client.rb', line 71 def post(resource, body = nil, query = {} ) url = self.endpoint + "/" + resource query['key'] = self.api_key unless self.api_key.nil? self.connection.post(url, :body => body, :params => query) end |
#post_item(omeka_item) ⇒ Object
Create a new item from an OmekaItem instance
146 147 148 |
# File 'lib/omeka_client/client.rb', line 146 def post_item(omeka_item) self.post("items", omeka_item.data.to_h.to_json) end |
#put(resource, id, body, query = {}) ⇒ NetHttpPersistentResponseWrapper
Generic DELETE request to the Omeka site
111 112 113 114 115 116 |
# File 'lib/omeka_client/client.rb', line 111 def put(resource, id, body, query = {} ) url = self.endpoint + "/" + resource url += "/" + id.to_s unless id.nil? query[:key] = self.api_key unless self.api_key.nil? self.connection.put(url, :body => body, :params => query) end |
#put_item(omeka_item) ⇒ Object
Update an item using an OmekaItem instance
153 154 155 |
# File 'lib/omeka_client/client.rb', line 153 def put_item(omeka_item) self.put("items", omeka_item.data.id, omeka_item.data.to_h.to_json) end |
#resources ⇒ Hash
Get a list of the resources available from the Omeka API
178 179 180 |
# File 'lib/omeka_client/client.rb', line 178 def resources self.get_hash('resources') end |
#site ⇒ Hash
Get the description of the Omeka site
170 171 172 |
# File 'lib/omeka_client/client.rb', line 170 def site self.get_hash('site') end |