Class: Hominid::Base
- Inherits:
-
Object
- Object
- Hominid::Base
- Defined in:
- lib/hominid/base.rb
Constant Summary collapse
- MAILCHIMP_API_VERSION =
MailChimp API Documentation: www.mailchimp.com/api/1.2/
"1.2"
Instance Method Summary collapse
-
#add_api_key ⇒ Object
Security related methods ——————————–.
- #api_keys(include_expired = false) ⇒ Object
- #apply_defaults_to(options) ⇒ Object
- #call(method, *args) ⇒ Object
-
#clean_merge_tags(merge_tags) ⇒ Object
handle common cases for which the Mailchimp API would raise Exceptions.
- #expire_api_key ⇒ Object
-
#initialize(config = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(config = {}) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hominid/base.rb', line 7 def initialize(config = {}) if defined?(Rails.root) && (!config || config.empty?) config = YAML.load(File.open("#{Rails.root}/config/hominid.yml"))[Rails.env].symbolize_keys end api_endpoint = config[:api_key].split('-').last config.merge(:username => config[:username].to_s, :password => config[:password].to_s) defaults = {:send_welcome => false, :double_opt_in => false, :update_existing => true, :replace_interests => true, :merge_tags => {}, :secure => false} @config = defaults.merge(config).freeze if config[:secure] @chimpApi = XMLRPC::Client.new2("https://#{api_endpoint}.api.mailchimp.com/#{MAILCHIMP_API_VERSION}/") else @chimpApi = XMLRPC::Client.new2("http://#{api_endpoint}.api.mailchimp.com/#{MAILCHIMP_API_VERSION}/") end end |
Instance Method Details
#add_api_key ⇒ Object
Security related methods
30 31 32 |
# File 'lib/hominid/base.rb', line 30 def add_api_key @chimpApi.call("apikeyAdd", *@config.values_at(:username, :password, :api_key)) end |
#api_keys(include_expired = false) ⇒ Object
38 39 40 41 |
# File 'lib/hominid/base.rb', line 38 def api_keys(include_expired = false) username, password = *@config.values_at(:username, :password) @chimpApi.call("apikeys", username, password, include_expired) end |
#apply_defaults_to(options) ⇒ Object
58 59 60 |
# File 'lib/hominid/base.rb', line 58 def apply_defaults_to() @config.merge() end |
#call(method, *args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hominid/base.rb', line 62 def call(method, *args) @chimpApi.call(method, @config[:api_key], *args) rescue XMLRPC::FaultException => error case error.faultCode when 230 raise AlreadySubscribed.new(error) when 231 raise AlreadyUnsubscribed.new(error) when 232 raise NotExists.new(error) when 233, 215 raise NotSubscribed.new(error) else raise APIError.new(error) end rescue RuntimeError => error if error. =~ /Wrong type NilClass\. Not allowed!/ hashes = args.select{|a| a.is_a? Hash} errors = hashes.select{|k, v| v.nil? }.collect{ |k, v| "#{k} is Nil." }.join(' ') raise CommunicationError.new(errors) else raise error end rescue Exception => error raise CommunicationError.new(error.) end |
#clean_merge_tags(merge_tags) ⇒ Object
handle common cases for which the Mailchimp API would raise Exceptions
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hominid/base.rb', line 47 def () return {} unless .is_a? Hash .each do |key, value| if [key].is_a? String [key] = value.gsub("\v", '') elsif [key].nil? [key] = '' end end end |
#expire_api_key ⇒ Object
34 35 36 |
# File 'lib/hominid/base.rb', line 34 def expire_api_key @chimpApi.call("apikeyExpire", *@config.values_at(:username, :password, :api_key)) end |