Class: MailChimp

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

Constant Summary collapse

@@api_key =
''
@@api_version =
'1.2'
@@datacenter =
'us1'
@@default_options =
{:output => :json}
@@url =
"http://#{@@datacenter}.api.mailchimp.com/#{@@api_version}/"

Class Method Summary collapse

Class Method Details

.log=(logger) ⇒ Object



43
44
45
# File 'lib/chimp_mailer.rb', line 43

def self.log=(logger)
  RestClient.log = logger
end

.method_missing(name, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chimp_mailer.rb', line 25

def self.method_missing(name, *args)
  options = args.last.class == Hash ? args.last : {}
  options.merge!(@@default_options)
  options[:method] = name.to_s.camelize(:lower)

  raw_response = RestClient.get("#{@@url}?#{options.to_query}", :accept => :json)

  response = ActiveSupport::JSON.decode(raw_response) if response.class == String
  if response.class == Array
    response.each { |x| x.symbolize_keys }
  elsif response.class == Hash
    response.symbolize_keys!
    raise MailChimpError, "(#{response[:code] ? response[:code] : 'no code'}) #{response[:error]}" if response[:error]
  end

  return response
end

.settings=(settings) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/chimp_mailer.rb', line 14

def self.settings=(settings)
  settings.each do |key, value|
    class_variable_set("@@#{key}".to_sym, value)
    if key == 'api_key'
      @@datacenter = @@api_key.split('-').last
    end
  end
  @@url = "http://#{@@datacenter}.api.mailchimp.com/#{@@api_version}/"
  @@default_options = {:apikey => @@api_key, :output => :json}
end