Class: MailChimp
- Inherits:
-
Object
- Object
- MailChimp
- Defined in:
- lib/mailchimp_api.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
Instance Method Summary collapse
- #call_server(method, params = {}) ⇒ Object
-
#campaign_create(list_id, from_email, from_name, subject, content, generate_text = true) ⇒ Object
tested.
-
#campaign_delete(cid) ⇒ Object
tested.
-
#campaign_send_now(cid) ⇒ Object
not tested.
-
#campaign_send_test(cid, emails) ⇒ Object
tested.
-
#campaign_update(cid, name, value) ⇒ Object
tested.
- #gen_params_list(params) ⇒ Object
-
#list_batch_subscribe(list_id, batch_emails) ⇒ Object
not tested.
-
#list_batch_unsubscribe(list_id, batch_emails) ⇒ Object
tested.
-
#list_members(list_id) ⇒ Object
not tested.
-
#list_subscribe(list_id, email) ⇒ Object
tested.
-
#list_unsubscribe(list_id, email) ⇒ Object
tested.
-
#lists ⇒ Object
tested.
-
#login(username, password) ⇒ Object
tested.
-
#ping ⇒ Object
tested.
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
12 13 14 |
# File 'lib/mailchimp_api.rb', line 12 def api @api end |
Instance Method Details
#call_server(method, params = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mailchimp_api.rb', line 109 def call_server method , params={} url = "api.mailchimp.com" path = "/1.1/?output=json&method=#{method}" params[:apikey] = @api unless method == "login" data = gen_params_list(params) #puts ">>> http://#{url}#{path}&#{data}" http = Net::HTTP.new(url) result = http.post(path, data) begin json_res = JSON.parse result.body rescue JSON::ParserError json_res = result.body end json_res end |
#campaign_create(list_id, from_email, from_name, subject, content, generate_text = true) ⇒ Object
tested
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mailchimp_api.rb', line 27 def campaign_create list_id, from_email, from_name, subject, content, generate_text = true params = {:options => {:list_id => list_id, :subject => subject, :from_name => from_name, :from_email => from_email}, :type => "regular", :content => content, :generate_text => generate_text} ret = call_server 'campaignCreate', params if ret.class == String ret = ret.slice(1, ret.size-2) end ret end |
#campaign_delete(cid) ⇒ Object
tested
46 47 48 49 50 |
# File 'lib/mailchimp_api.rb', line 46 def campaign_delete cid params = {:cid => cid} ret = call_server 'campaignDelete', params ret end |
#campaign_send_now(cid) ⇒ Object
not tested
53 54 55 56 57 |
# File 'lib/mailchimp_api.rb', line 53 def campaign_send_now cid params = {:cid => cid} ret = call_server 'campaignSendNow', params ret end |
#campaign_send_test(cid, emails) ⇒ Object
tested
60 61 62 63 64 |
# File 'lib/mailchimp_api.rb', line 60 def campaign_send_test cid, emails params = {:cid => cid, :test_emails => emails} ret = call_server 'campaignSendTest', params ret end |
#campaign_update(cid, name, value) ⇒ Object
tested
39 40 41 42 43 |
# File 'lib/mailchimp_api.rb', line 39 def campaign_update cid, name, value params = {:cid => cid, :name => name, :value => value} ret = call_server 'campaignUpdate', params ret end |
#gen_params_list(params) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mailchimp_api.rb', line 125 def gen_params_list params params.map do |key, value| if value.class == Hash value.map do |k, v| "#{key}[#{k}]=#{CGI::escape(v.to_s)}" end.join '&' elsif value.class == Array value.map do |v| "#{key}[]=#{CGI::escape(vi.to_s)}" end.join '&' else "#{key}=#{CGI::escape(value.to_s)}" end end.join '&' end |
#list_batch_subscribe(list_id, batch_emails) ⇒ Object
not tested
67 68 69 70 71 |
# File 'lib/mailchimp_api.rb', line 67 def list_batch_subscribe list_id, batch_emails params = {:id => list_id, :batch => batch_emails} ret = call_server 'listBatchSubscribe', params ret end |
#list_batch_unsubscribe(list_id, batch_emails) ⇒ Object
tested
74 75 76 77 78 |
# File 'lib/mailchimp_api.rb', line 74 def list_batch_unsubscribe list_id, batch_emails params = {:id => list_id, :emails => batch_emails, :delete_member => true, :send_goodbye => false } ret = call_server 'listBatchUnsubscribe', params ret end |
#list_members(list_id) ⇒ Object
not tested
103 104 105 106 107 |
# File 'lib/mailchimp_api.rb', line 103 def list_members list_id params = {:id => list_id} ret = call_server 'listMembers', params (ret.class == Hash) ? [] : ret end |
#list_subscribe(list_id, email) ⇒ Object
tested
81 82 83 84 85 86 |
# File 'lib/mailchimp_api.rb', line 81 def list_subscribe list_id, email params = {:id => list_id, :email_address => email, :merge_vars => {'FIRST' => '', 'LAST' => ''} , :double_optin => false} ret = call_server 'listSubscribe', params ret end |
#list_unsubscribe(list_id, email) ⇒ Object
tested
89 90 91 92 93 |
# File 'lib/mailchimp_api.rb', line 89 def list_unsubscribe list_id, email params = {:id => list_id, :email_address => email} ret = call_server 'listUnsubscribe', params ret end |
#lists ⇒ Object
tested
96 97 98 99 100 |
# File 'lib/mailchimp_api.rb', line 96 def lists params = {} ret = call_server 'lists', params ret end |
#login(username, password) ⇒ Object
tested
15 16 17 18 |
# File 'lib/mailchimp_api.rb', line 15 def login username, password ret = call_server 'login', {:username => username, :password => password} @api = ret.slice(1, ret.size-2) if ret.class == String end |
#ping ⇒ Object
tested
21 22 23 24 |
# File 'lib/mailchimp_api.rb', line 21 def ping ret = call_server 'ping' return ((ret.class == String) and !(ret =~ /Everything.*Chimpy/).nil?) ? true : false end |