Class: PopIt
- Inherits:
-
Object
- Object
- PopIt
- 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
-
#apikey ⇒ Object
readonly
An API key.
-
#host_name ⇒ Object
readonly
The PopIt API's host name, eg "popit.mysociety.org".
-
#instance_name ⇒ Object
readonly
The instance name.
-
#max_retries ⇒ Object
readonly
The maximum number of retries in case of HTTP 503 Service Unavailable errors.
-
#port ⇒ Object
readonly
The PopIt API's port, eg 80.
-
#version ⇒ Object
readonly
The PopIt API version, eg "v0.1".
Instance Method Summary collapse
-
#delete(path, opts = {}) ⇒ Hash
Sends a DELETE request.
-
#get(path, opts = {}) ⇒ Object
Sends a GET request.
-
#initialize(opts = {}) ⇒ PopIt
constructor
Initializes a PopIt API client.
-
#post(path, opts = {}) ⇒ Object
Sends a POST request.
-
#put(path, opts = {}) ⇒ nil
Sends a PUT request.
Constructor Details
#initialize(opts = {}) ⇒ PopIt
Initializes a PopIt API client.
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
Instance Attribute Details
#apikey ⇒ Object (readonly)
An API key.
39 40 41 |
# File 'lib/popit.rb', line 39 def apikey @apikey end |
#host_name ⇒ Object (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_name ⇒ Object (readonly)
The instance name.
31 32 33 |
# File 'lib/popit.rb', line 31 def instance_name @instance_name end |
#max_retries ⇒ Object (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 |
#port ⇒ Object (readonly)
The PopIt API's port, eg 80
35 36 37 |
# File 'lib/popit.rb', line 35 def port @port end |
#version ⇒ Object (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.
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.
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.
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.
90 91 92 |
# File 'lib/popit.rb', line 90 def put(path, opts = {}) request(:put, path, opts) end |