Class: OmekaClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/omeka_client/client.rb

Overview

A class to create clients that interact with the Omeka API

Author:

  • Lincoln Mullen

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, api_key = nil) ⇒ OmekaClient

Sets up a new client to interact with an Omeka site

Parameters:

  • endpoint (String)

    the URL of the Omeka API endpoint, without a trailing slash. For example: “http://localhost/omeka/api

  • api_key (String) (defaults to: nil)

    The API key of the Omeka user. This can be null for GET requests, but is required for POST, PUT, and DELETE requests.

Since:

  • 0.0.1



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_keyObject

Since:

  • 0.0.1



10
11
12
# File 'lib/omeka_client/client.rb', line 10

def api_key
  @api_key
end

#connectionObject

Since:

  • 0.0.1



10
11
12
# File 'lib/omeka_client/client.rb', line 10

def connection
  @connection
end

#endpointObject

Since:

  • 0.0.1



10
11
12
# File 'lib/omeka_client/client.rb', line 10

def endpoint
  @endpoint
end

Instance Method Details

#collectionsArray

Get a list of the Omeka collections

TODO: Check that items are available in the resources

Returns:

  • (Array)

    Returns an array of collection hashes

Since:

  • 0.0.1



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

Parameters:

  • resource (String)

    The type of Omeka resource to delete, e.g. “items”, “collections”

  • id (Integer)

    The id number of the Omeka resource to delete

  • query (defaults to: {})

    {} [Hash] Additional query parameters

Returns:

  • (NetHttpPersistentResponseWrapper)

    A wrapper around the object

Since:

  • 0.0.3



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

Parameters:

  • omeka_item (OmekaItem)

    An instance of OmekaItem

Since:

  • 0.0.4



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

Parameters:

  • resource (String)

    The Omeka resource to request, e.g. “items”, “collections”

  • id (Integer) (defaults to: nil)

    The id of the specific resource to request. Include an id to get just one item; do not include it to get all the items.

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

    Additional query parameters

Returns:

  • (NetHttpPersistentResponseWrapper)

    A wrapper around the object

Since:

  • 0.0.1



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

Parameters:

  • resource (String)

    The Omeka resource to request, e.g. “items”, “collections”

  • id (Integer) (defaults to: nil)

    The id of the specific resource to request. Include an id to get just one item; do not include it to get all the items.

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

    Additional query parameters

Returns:

  • (Array or Hash)

    A hash of the representation of the object, or an array of hashes.

Since:

  • 0.0.1



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

#itemsArray

Get a list of the Omeka items

TODO: Check that items are available in the resources

Returns:

  • (Array)

    Returns an array of item hashes

Since:

  • 0.0.1



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

Parameters:

  • id (Integer) (defaults to: nil)

    The ID of the item to return. No value gets an

  • query (defaults to: {})

    {} [Hash] Additional query parameters

Returns:

  • (OmekaItem)

    An OmekaItem representation of the desired item,

Since:

  • 0.0.2



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

Parameters:

  • resource (String)

    The Omeka resource to request, e.g. “items”, “collections”

  • body (String) (defaults to: nil)

    A string containing a JSON representation of the body of the item

  • query (defaults to: {})

    {} [Hash] Additional query parameters (optional)

Returns:

  • (NetHttpPersistentResponseWrapper)

    A wrapper around the object

Since:

  • 0.0.3



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

Parameters:

  • omeka_item (OmekaItem)

    An instance of OmekaItem

Since:

  • 0.0.4



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

Parameters:

  • resource (String)

    The type of Omeka resource to update, e.g. “items”, “collections”

  • id (Integer)

    The id number of the Omeka resource to update

  • query (defaults to: {})

    {} [Hash] Additional query parameters

Returns:

  • (NetHttpPersistentResponseWrapper)

    A wrapper around the object

Since:

  • 0.0.3



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

Parameters:

  • omeka_item (OmekaItem)

    An instance of OmekaItem

Since:

  • 0.0.4



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

#resourcesHash

Get a list of the resources available from the Omeka API

Returns:

  • (Hash)

    Returns a hash of the resources available via the API

Since:

  • 0.0.1



178
179
180
# File 'lib/omeka_client/client.rb', line 178

def resources
  self.get_hash('resources')
end

#siteHash

Get the description of the Omeka site

Returns:

  • (Hash)

    A hash of the description of the Omeka site

Since:

  • 0.0.1



170
171
172
# File 'lib/omeka_client/client.rb', line 170

def site
  self.get_hash('site')
end