Class: Hominid::API

Inherits:
Object
  • Object
show all
Includes:
Campaign, List, Security
Defined in:
lib/hominid/api.rb

Direct Known Subclasses

STS

Constant Summary collapse

MAILCHIMP_API_VERSION =

MailChimp API Documentation: apidocs.mailchimp.com/api/1.3/

"1.3"

Instance Method Summary collapse

Methods included from Security

#apikey_add, #apikey_expire, #apikeys

Methods included from List

#find_list_by_id, #find_list_by_name, #find_list_by_web_id, #find_list_id_by_name, #find_list_id_by_web_id

Methods included from Campaign

#find_campaign_by_id, #find_campaign_by_web_id, #find_campaigns_by_list_id, #find_campaigns_by_list_name, #find_campaigns_by_title, #find_campaigns_by_type

Constructor Details

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

Initialize with an API key and config options

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hominid/api.rb', line 16

def initialize(api_key, config = {})
  raise ArgumentError.new('Your Mailchimp API key appears to be malformed.') unless api_key.include?('-')
  dc = api_key.split('-').last
  defaults = {
    :api_version        => MAILCHIMP_API_VERSION,
    :domain             => 'api.mailchimp.com',
    :secure             => false,
    :timeout            => nil
  }
  @config = defaults.merge(config).freeze
  protocol = @config[:secure] ? 'https' : 'http'
  @api_key = api_key
  @chimpApi = XMLRPC::Client.new2("#{protocol}://#{dc}.#{@config[:domain]}/#{@config[:api_version]}/", nil, @config[:timeout])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(api_method, *args) ⇒ Object

:nodoc:



31
32
33
34
35
36
# File 'lib/hominid/api.rb', line 31

def method_missing(api_method, *args) # :nodoc:
  @chimpApi.call(camelize_api_method_name(api_method.to_s), @api_key, *args)
rescue XMLRPC::FaultException => error
  super if error.faultCode == -32601
  raise APIError.new(error)
end

Instance Method Details

#respond_to?(api_method) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/hominid/api.rb', line 38

def respond_to?(api_method) # :nodoc:
  @chimpApi.call(api_method, @api_key)
rescue XMLRPC::FaultException => error
  error.faultCode == -32601 ? false : true 
end