Class: WebbyNode::APIObject

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/webbynode-api.rb

Overview

Author:

  • Shane Sveller

Since:

  • 0.0.1

Version:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ APIObject

Creates a new API object secured by e-mail address and API token

Examples:

Instantiates a new API access object

WebbyNode::APIObject.new(:email => "[email protected]", :token => "123456abcdef")

Options Hash (options):

  • :email (String)

    e-mail address with API access

  • :token (String)

    API token that matches the included e-mail address

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



28
29
30
31
32
# File 'lib/webbynode-api.rb', line 28

def initialize(options = {})
  raise ArgumentError, ":email and :token are required arguments" unless options[:email] && options[:token]
  @email = options[:email]
  @token = options[:token]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object?

Catches simple requests for specific API data returned via a hash

Since:

  • 0.0.1



78
79
80
81
# File 'lib/webbynode-api.rb', line 78

def method_missing(method)
  key = @data[method.to_s] if @data
  key
end

Instance Attribute Details

#dataObject

Stores the results of the HTTParty API interactions

Since:

  • 0.0.1



20
21
22
# File 'lib/webbynode-api.rb', line 20

def data
  @data
end

#emailObject

E-mail address used to access the WebbyNode API

Since:

  • 0.0.1



16
17
18
# File 'lib/webbynode-api.rb', line 16

def email
  @email
end

#tokenObject

API token used to access the WebbyNode API, visible at manager.webbynode.com/account

Since:

  • 0.0.1



18
19
20
# File 'lib/webbynode-api.rb', line 18

def token
  @token
end

Class Method Details

.auth_post(url, options = {}) ⇒ Hash

Uses HTTParty to submit a secure API POST request via email address and token. API access information should be included via the :email and :token hash keys, in a hash on the :query key of the options hash passed as the second argument in the method call.

Examples:

Queries the API data for the client account

WebbyNode::APIObject.auth_post("/api/xml/client", :query => {:email => ""example@email.com",
:token => "1234567abcde", :posted_data => "test"})

Options Hash (options):

  • :query (String)

    query hash used to build the final URL Should include :email and :token keys with the e-mail address and API token with access rights

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



64
65
66
67
68
69
70
# File 'lib/webbynode-api.rb', line 64

def self.auth_post(url, options = {})
  options[:query] ||= {}
  raise ArgumentError, "API information is missing or incomplete" unless options[:query][:email] && options[:query][:token]
  results = self.post(url, options)
  raise ArgumentError, "Probable bad API information given" if results == {}
  return results
end

Instance Method Details

#auth_get(url, options = {}) ⇒ Object

Uses HTTParty to submit a secure API request via email address and token

Options Hash (options):

  • :query (Hash)

    query hash used to build the final URL

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



38
39
40
41
42
43
44
45
# File 'lib/webbynode-api.rb', line 38

def auth_get(url, options = {})
  raise ArgumentError, "API information is missing or incomplete" unless @email && @token
  options[:query] ||= {}
  options[:query].merge!(:email => @email, :token => @token)
  results = self.class.get(url, options)
  raise ArgumentError, "Probable bad API information given" if results == {}
  return results
end

#auth_post(url, options = {}) ⇒ Object

Since:

  • 0.0.1



47
48
49
# File 'lib/webbynode-api.rb', line 47

def auth_post(url, options = {})
  self.class.auth_post(url, options)
end