Class: Benchwarmer::API

Inherits:
Object
  • Object
show all
Defined in:
lib/benchwarmer/api.rb

Constant Summary collapse

BENCHMARK_API_VERSION =

Benchmark Email API Documentation: www.benchmarkemail.com/API/Library

"1.0"
DEFAULTS =
{
  :api_version => BENCHMARK_API_VERSION,
  :secure => false,
  :timeout => nil
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token, config = {}) ⇒ API

Initialize with an API token and config options



17
18
19
20
21
# File 'lib/benchwarmer/api.rb', line 17

def initialize(api_token, config = {})
  opts = DEFAULTS.merge(config).freeze
  @api_client = XMLRPC::Client.new2("#{opts[:secure] ? 'https' : 'http'}://api.benchmarkemail.com/#{opts[:api_version]}/", nil, opts[:timeout])
  @api_token = api_token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(api_method, *args) ⇒ Object

:nodoc:



23
24
25
26
27
28
# File 'lib/benchwarmer/api.rb', line 23

def method_missing(api_method, *args) # :nodoc:
  @api_client.call(camelize_api_method_name(api_method.to_s), @api_token, *args)
rescue XMLRPC::FaultException => error
  super if error.message.include?("unsupported method called:")
  raise APIError.new(error)
end

Class Method Details

.login(username, password, config = {}) ⇒ Object

Token management methods

Each time you “login”, a new token is generated with access to the B.E. API. This means that you should store a single token and use it over and over, preventing a build-up of tokens.

Our hope is that in the future B.E. will make the following changes with regard to API access tokens:

  • Provide an interface for account holders to manage their API tokens.

  • Remove the ability to login with username, password.

  • Add OAuth2 so that the “access_token” can be used in the same way that “API tokens” are currently used.

In other words, make it more like Mailchimp.



52
53
54
55
# File 'lib/benchwarmer/api.rb', line 52

def self.(username, password, config = {})
  api_client(config)
  @api_client.call('login', username, password)
end

.token_add(username, password, config = {}) ⇒ Object

Add a new token (generated randomly)



64
65
66
67
# File 'lib/benchwarmer/api.rb', line 64

def self.token_add(username, password, config = {})
  api_client(config)
  @api_client.call("tokenAdd", username, password, SecureRandom.urlsafe_base64(20))
end

.token_delete(username, password, token, config = {}) ⇒ Object

Delete an existing token



70
71
72
73
# File 'lib/benchwarmer/api.rb', line 70

def self.token_delete(username, password, token, config = {})
  api_client(config)
  @api_client.call("tokenDelete", username, password, token)
end

.token_get(username, password, config = {}) ⇒ Object

Get a list of the tokens



58
59
60
61
# File 'lib/benchwarmer/api.rb', line 58

def self.token_get(username, password, config = {})
  api_client(config)
  @api_client.call("tokenGet", username, password)
end

Instance Method Details

#respond_to?(api_method) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/benchwarmer/api.rb', line 30

def respond_to?(api_method) # :nodoc:
  @api_client.call(api_method, @api_token)
rescue XMLRPC::FaultException => error
  error.message.include?("unsupported method called:") ? false : true
end