Class: PopIt

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/popit.rb,
lib/popit/version.rb

Overview

A Ruby wrapper for the PopIt API.

Instead of writing the path to an API endpoint, you can use method chaining. For example:

require 'popit'
api = PopIt.new :instance_name => 'demo'
api.get 'persons/john-doe'

can be written as:

api.persons('john-doe').get

All methods and arguments between api and the HTTP method - in this case, get - become parts of the path.

Defined Under Namespace

Classes: Chain, Error, NotAuthenticated, PageNotFound, ServiceUnavailable

Constant Summary collapse

VERSION =
"0.0.8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PopIt

Initializes a PopIt API client.

Parameters:

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

    the API client's configuration

Options Hash (opts):

  • :instance_name (String)

    the instance name

  • :host_name (String)

    the PopIt API's host name, eg "popit.mysociety.org"

  • :post (String)

    the PopIt API's port, eg 80

  • :version (String)

    the PopIt API version, eg "v1"

  • :apikey (String)

    an API key

  • :max_retries (String)

    the maximum number of retries in case of HTTP 503 Service Unavailable errors



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

def initialize(opts = {})
  unless opts.has_key?(:instance_name)
    raise ArgumentError, 'Missing key :instance_name'
  end

  @instance_name = opts[:instance_name]
  @host_name     = opts[:host_name]   || 'popit.mysociety.org'
  @port          = opts[:port]        || 80
  @version       = opts[:version]     || 'v0.1'
  @apikey        = opts[:apikey]
  @max_retries   = opts[:max_retries] || 0

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)



155
156
157
# File 'lib/popit.rb', line 155

def method_missing(*args)
  Chain.new(self, args)
end

Instance Attribute Details

#apikeyObject (readonly)

An API key.



39
40
41
# File 'lib/popit.rb', line 39

def apikey
  @apikey
end

#host_nameObject (readonly)

The PopIt API's host name, eg "popit.mysociety.org".



33
34
35
# File 'lib/popit.rb', line 33

def host_name
  @host_name
end

#instance_nameObject (readonly)

The instance name.



31
32
33
# File 'lib/popit.rb', line 31

def instance_name
  @instance_name
end

#max_retriesObject (readonly)

The maximum number of retries in case of HTTP 503 Service Unavailable errors.



41
42
43
# File 'lib/popit.rb', line 41

def max_retries
  @max_retries
end

#portObject (readonly)

The PopIt API's port, eg 80



35
36
37
# File 'lib/popit.rb', line 35

def port
  @port
end

#versionObject (readonly)

The PopIt API version, eg "v0.1"



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

def version
  @version
end

Instance Method Details

#delete(path, opts = {}) ⇒ Hash

Sends a DELETE request.

Parameters:

  • path (String)

    a path with no leading slash

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

    key-value pairs for the query string

Returns:

  • (Hash)

    an empty hash



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

def delete(path, opts = {})
  request(:delete, path, opts)
end

#get(path, opts = {}) ⇒ Object

Sends a GET request.

Parameters:

  • path (String)

    a path with no leading slash

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

    key-value pairs for the query string

Returns:

  • the JSON response from the server



72
73
74
# File 'lib/popit.rb', line 72

def get(path, opts = {})
  request(:get, path, opts)
end

#post(path, opts = {}) ⇒ Object

Sends a POST request.

Parameters:

  • path (String)

    a path with no leading slash

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

    key-value pairs for the message body

Returns:

  • the JSON response from the server



81
82
83
# File 'lib/popit.rb', line 81

def post(path, opts = {})
  request(:post, path, opts)
end

#put(path, opts = {}) ⇒ nil

Sends a PUT request.

Parameters:

  • path (String)

    a path with no leading slash

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

    key-value pairs for the message body

Returns:

  • (nil)

    nothing



90
91
92
# File 'lib/popit.rb', line 90

def put(path, opts = {})
  request(:put, path, opts)
end