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



127
128
129
# File 'lib/omeka_client/client.rb', line 127

def collections
  self.get_hash('collections')
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



117
118
119
# File 'lib/omeka_client/client.rb', line 117

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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omeka_client/client.rb', line 80

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

#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



107
108
109
# File 'lib/omeka_client/client.rb', line 107

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



99
100
101
# File 'lib/omeka_client/client.rb', line 99

def site
  self.get_hash('site')
end