Class: Mailchimp::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mailchimp/base.rb

Direct Known Subclasses

Export, Mandrill, STS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, default_parameters = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
# File 'lib/mailchimp/base.rb', line 11

def initialize(api_key = nil, default_parameters = {})
  @api_key = api_key || ENV['MAILCHIMP_API_KEY'] || nil
  @timeout = default_parameters.delete(:timeout) 
  @default_params = default_parameters
  @throws_exceptions = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



48
49
50
51
52
# File 'lib/mailchimp/base.rb', line 48

def method_missing(method, *args)
  method = method.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } #Thanks for the gsub, Rails
  method = method[0].chr.downcase + method[1..-1].gsub(/aim$/i, 'AIM')
  call(method, *args)
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



27
28
29
# File 'lib/mailchimp/base.rb', line 27

def api_key
  @api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/mailchimp/base.rb', line 9

def api_key
  @api_key
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/mailchimp/base.rb', line 9

def options
  @options
end

#throws_exceptionsObject

Returns the value of attribute throws_exceptions.



9
10
11
# File 'lib/mailchimp/base.rb', line 9

def throws_exceptions
  @throws_exceptions
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/mailchimp/base.rb', line 9

def timeout
  @timeout
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



29
30
31
32
# File 'lib/mailchimp/base.rb', line 29

def method_missing(sym, *args, &block)
  self.api_key = args[0] if sym == :valid_api_key?
  new(self.api_key).send(sym, *args, &block)
end

Instance Method Details

#dc_from_api_keyObject



18
19
20
# File 'lib/mailchimp/base.rb', line 18

def dc_from_api_key
  (@api_key.nil? || @api_key.length == 0 || @api_key !~ /-/) ? '' : "#{@api_key.split("-").last}."
end

#valid_api_key?Boolean

Returns:



22
23
24
# File 'lib/mailchimp/base.rb', line 22

def valid_api_key?
  %q{"Everything's Chimpy!"} == self.class.post(_base_api_url+"ping", :body => {apikey: @api_key}, :timeout => 30).body
end