lib/hominid.rb in bgetting-hominid-1.1.12 vs lib/hominid.rb in bgetting-hominid-1.2.0
- old
+ new
@@ -15,59 +15,40 @@
class Hominid
# MailChimp API Documentation: http://www.mailchimp.com/api/1.2/
MAILCHIMP_API = "http://api.mailchimp.com/1.2/"
- def initialize(config = nil)
- load_monkey_brains(config)
- @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
+ 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
+ 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, :user_info => {}}
+ @config = defaults.merge(config).freeze
+ @chimpApi = XMLRPC::Client.new2(MAILCHIMP_API)
end
- def load_monkey_brains(config)
- config = YAML.load(File.open("#{RAILS_ROOT}/config/hominid.yml"))[RAILS_ENV].symbolize_keys unless config
- @chimpUsername = config[:username].to_s
- @chimpPassword = config[:password].to_s
- @api_key = config[:api_key]
- @send_goodbye = config[:send_goodbye]
- @send_notify = config[:send_notify]
- @double_opt = config[:double_opt]
- end
-
## Security related methods
def add_api_key
- begin
- @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
- @chimpApi.call("apikeyAdd", @chimpUsername, @chimpPassword, @api_key)
- rescue
- false
- end
+ @chimpApi.call("apikeyAdd", *@config.values_at(:username, :password, :api_key))
end
def expire_api_key
- begin
- @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
- @chimpApi.call("apikeyExpire", @chimpUsername, @chimpPassword, @api_key)
- rescue
- false
- end
+ @chimpApi.call("apikeyExpire", *@config.values_at(:username, :password, :api_key))
end
def api_keys(include_expired = false)
- begin
- @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
- @api_keys = @chimpApi.call("apikeys", @chimpUsername, @chimpPassword, include_expired)
- rescue
- return nil
- end
+ username, password = *@config.values_at(:username, :password)
+ @chimpApi.call("apikeys", username, password, include_expired)
end
## Campaign related methods
def campaign_content(campaign_id)
# Get the content of a campaign
- @content = call("campaignContent", campaign_id)
+ call("campaignContent", campaign_id)
end
def campaigns(filters = {}, start = 0, limit = 50)
# Get the campaigns for this account
# API Version 1.2 requires that filters be sent as a hash
@@ -81,11 +62,11 @@
# :title = (string) Show only campaigns with this title.
# :subject = (string) Show only campaigns with this subject.
# :sedtime_start = (string) Show campaigns sent after YYYY-MM-DD HH:mm:ss.
# :sendtime_end = (string) Show campaigns sent before YYYY-MM-DD HH:mm:ss.
# :subject = (boolean) Filter by exact values, or search within content for filter values.
- @campaigns = call("campaigns", filters, start, limit)
+ call("campaigns", filters, start, limit)
end
# Attach Ecommerce Order Information to a Campaign.
# The order hash should be structured as follows:
#
@@ -106,26 +87,26 @@
# :category_id = (integer) internal id for the (main) category associated with product
# :category_name = (string) the category name for the category id
# :qty = (double) the quantity of items ordered
# :cost = (double) the cost of a single item (i.e., not the extended cost of the line)
def campaign_ecomm_add_order(order)
- @campaign = call("campaignEcommAddOrder", order)
+ call("campaignEcommAddOrder", order)
end
def create_campaign(type = 'regular', options = {}, content = {}, segment_options = {}, type_opts = {})
# Create a new campaign
- @campaign = call("campaignCreate", type, options, content, segment_options, type_opts)
+ call("campaignCreate", type, options, content, segment_options, type_opts)
end
def delete_campaign(campaign_id)
# Delete a campaign
- @campaign = call("campaignDelete", campaign_id)
+ call("campaignDelete", campaign_id)
end
def replicate_campaign(campaign_id)
# Replicate a campaign (returns ID of new campaign)
- @campaign = call("campaignReplicate", campaign_id)
+ call("campaignReplicate", campaign_id)
end
def schedule_campaign(campaign_id, time = "#{1.day.from_now}")
# Schedule a campaign
## TODO: Add support for A/B Split scheduling
@@ -142,11 +123,11 @@
call("campaignSendTest", campaign_id, emails)
end
def templates
# Get the templates
- @templates = call("campaignTemplates", @api_key)
+ call("campaignTemplates")
end
def update_campaign(campaign_id, name, value)
# Update a campaign
call("campaignUpdate", campaign_id, name, value)
@@ -159,23 +140,23 @@
## Helper methods
def html_to_text(content)
# Convert HTML content to text
- @html_to_text = call("generateText", 'html', content)
+ call("generateText", 'html', content)
end
def convert_css_to_inline(html, strip_css = false)
# Convert CSS styles to inline styles and (optionally) remove original styles
- @html_to_text = call("inlineCss", html, strip_css)
+ call("inlineCss", html, strip_css)
end
## List related methods
def lists
# Get all of the lists for this mailchimp account
- @lists = call("lists", @api_key)
+ call("lists")
end
def create_group(list_id, group)
# Add an interest group to a list
call("listInterestGroupAdd", list_id, group)
@@ -196,16 +177,16 @@
call("listMergeVarDel", list_id, tag)
end
def groups(list_id)
# Get the interest groups for a list
- @groups = call("listInterestGroups", list_id)
+ call("listInterestGroups", list_id)
end
def member(list_id, email)
# Get a member of a list
- @member = call("listMemberInfo", list_id, email)
+ call("listMemberInfo", list_id, email)
end
def members(list_id, status = "subscribed", since = "2000-01-01 00:00:00", start = 0, limit = 100)
# Get members of a list based on status
# Select members based on one of the following statuses:
@@ -215,47 +196,55 @@
# 'updated'
#
# Select members that have updated their status or profile by providing
# a "since" date in the format of YYYY-MM-DD HH:MM:SS
#
- @members = call("listMembers", list_id, status, since, start, limit)
+ call("listMembers", list_id, status, since, start, limit)
end
def merge_tags(list_id)
# Get the merge tags for a list
- @merge_tags = call("listMergeVars", list_id)
+ call("listMergeVars", list_id)
end
- def subscribe(list_id, email, user_info = {}, email_type = "html", update_existing = true, replace_interests = true, double_opt_in = nil)
+ def subscribe(list_id, email, options = {})
+ options = apply_defaults_to({:email_type => "html"}.merge(options))
# Subscribe a member
- call("listSubscribe", list_id, email, user_info, email_type, double_opt_in || @double_opt, update_existing, replace_interests)
+ call("listSubscribe", list_id, email, *options.values_at(:user_info, :email_type, :double_opt_in, :update_existing, :replace_interests, :send_welcome))
end
- def subscribe_many(list_id, subscribers)
+ def subscribe_many(list_id, subscribers, options = {})
+ options = apply_defaults_to({:update_existing => true}.merge(options))
# Subscribe a batch of members
# subscribers = {:EMAIL => 'example@email.com', :EMAIL_TYPE => 'html'}
- call("listBatchSubscribe", list_id, subscribers, @double_opt, true)
+ call("listBatchSubscribe", list_id, subscribers, *options.values_at(:double_opt_in, :update_existing, :replace_interests))
end
- def unsubscribe(list_id, current_email)
+ def unsubscribe(list_id, current_email, options = {})
+ options = apply_defaults_to({:delete_member => true}.merge(options))
# Unsubscribe a list member
- call("listUnsubscribe", list_id, current_email, true, @send_goodbye, @send_notify)
+ call("listUnsubscribe", list_id, current_email, *options.values_at(:delete_member, :send_goodbye, :send_notify))
end
- def unsubscribe_many(list_id, emails)
+ def unsubscribe_many(list_id, emails, options = {})
+ options = apply_defaults_to({:delete_member => true}.merge(options))
# Unsubscribe an array of email addresses
# emails = ['first@email.com', 'second@email.com']
- call("listBatchUnsubscribe", list_id, emails, true, @send_goodbye, @send_notify)
+ call("listBatchUnsubscribe", list_id, emails, *options.values_at(:delete_member, :send_goodbye, :send_notify))
end
def update_member(list_id, current_email, user_info = {}, email_type = "")
# Update a member of this list
call("listUpdateMember", list_id, current_email, user_info, email_type, true)
end
protected
+ def apply_defaults_to(options)
+ @config.merge(options)
+ end
+
def call(method, *args)
- @chimpApi.call(method, @api_key, *args)
+ @chimpApi.call(method, @config[:api_key], *args)
rescue XMLRPC::FaultException => error
raise HominidError.new(error.message)
rescue Exception => error
raise HominidCommunicationError.new(error.message)
end