Class: MailchimpClient
- Inherits:
-
Object
show all
- Defined in:
- lib/mcmailer/mailchimp_client.rb
Defined Under Namespace
Classes: ListNotFound, RequestTimeout, UnexpectedResponse
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(api_key, timeout) ⇒ MailchimpClient
Returns a new instance of MailchimpClient.
23
24
25
26
27
|
# File 'lib/mcmailer/mailchimp_client.rb', line 23
def initialize(api_key, timeout)
@api_key = api_key
@gibbon = Gibbon.new(@api_key)
@gibbon.timeout = timeout
end
|
Instance Attribute Details
#previous_response ⇒ Object
Returns the value of attribute previous_response.
21
22
23
|
# File 'lib/mcmailer/mailchimp_client.rb', line 21
def previous_response
@previous_response
end
|
Instance Method Details
#batch_destroy!(emails) ⇒ Object
87
88
89
90
91
|
# File 'lib/mcmailer/mailchimp_client.rb', line 87
def batch_destroy!(emails)
@previous_response = @gibbon.list_batch_unsubscribe(:id => @list['id'], :emails => emails, :delete_member => true, :send_goodbye => false, :send_notify => false)
rescue Timeout::Error
raise RequestTimeout
end
|
#batch_subscribe(batch) ⇒ Object
75
76
77
78
79
|
# File 'lib/mcmailer/mailchimp_client.rb', line 75
def batch_subscribe(batch)
@previous_response = @gibbon.list_batch_subscribe(:id => @list['id'], :batch => batch, :double_optin => false, :update_existing => true)
rescue Timeout::Error
raise RequestTimeout
end
|
#destroy!(email) ⇒ Object
81
82
83
84
85
|
# File 'lib/mcmailer/mailchimp_client.rb', line 81
def destroy!(email)
@previous_response = @gibbon.list_unsubscribe(:id => @list['id'], :email_address => email, :delete_member => true, :send_goodbye => false, :send_notify => false)
rescue Timeout::Error
raise RequestTimeout
end
|
#destroy_all_subscribers! ⇒ Object
WARNING: For large lists this is much slower than deleting all subscribers through Mailchimp’s website, but it should work. It’s useful for scenarios that require automation like Mailchimp unit tests.
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/mcmailer/mailchimp_client.rb', line 97
def destroy_all_subscribers!
responses = []
batch_size = 1500
until subscribers.count.zero? do
begin
emails = self.subscribers(0, batch_size).collect {|s| s['email']}
batch_destroy!(emails)
rescue UnexpectedResponse
raise
end
responses << @previous_response
break if @previous_response['success_count'].zero?
end
@previous_response = responses
rescue Timeout::Error
raise RequestTimeout
end
|
#subscribe(email, merge_vars = {}) ⇒ Object
63
64
65
66
67
|
# File 'lib/mcmailer/mailchimp_client.rb', line 63
def subscribe(email, merge_vars={})
@previous_response = @gibbon.list_subscribe(:id => @list['id'], :email_address => email, :double_optin => false, :update_existing => true, :merge_vars => merge_vars)
rescue Timeout::Error
raise RequestTimeout
end
|
#subscriber_info(email) ⇒ Object
69
70
71
72
73
|
# File 'lib/mcmailer/mailchimp_client.rb', line 69
def subscriber_info(email)
@previous_response = @gibbon.list_member_info(:id => @list['id'], :email_address => [email])
rescue Timeout::Error
raise RequestTimeout
end
|
#subscribers(start = 0, limit = 100) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/mcmailer/mailchimp_client.rb', line 52
def subscribers(start=0, limit=100)
@previous_response = @gibbon.list_members(:id => @list['id'], :start => start, :limit => limit)
if !@previous_response.has_key?('data')
raise UnexpectedResponse, "response hash did not have a 'data' key"
end
@previous_response['data']
rescue Timeout::Error
raise RequestTimeout
end
|
#use_list(name) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/mcmailer/mailchimp_client.rb', line 29
def use_list(name)
begin
@previous_response = @gibbon.lists
rescue Timeout::Error
raise RequestTimeout
end
if @previous_response.has_key?('data')
lists = @previous_response['data']
matches = lists.select {|list| list.has_key?('name') && list['name'] == name}
if matches.empty?
raise ListNotFound, "provided name => '#{name}'"
elsif matches.length > 1
raise UnexpectedResponse, "list names should be unique but found #{matches.length} lists by the name of '#{name}'"
else
@list = matches.first
end
else
raise UnexpectedResponse, "response hash did not have a 'data' key"
end
end
|