ruby-mailchimp 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/mail_chimp.rb +57 -21
  2. metadata +1 -1
data/lib/mail_chimp.rb CHANGED
@@ -47,7 +47,7 @@ require 'action_mailer'
47
47
  # end
48
48
  #
49
49
  class MailChimp
50
-
50
+
51
51
  attr_accessor :email_server, :email_user, :email_password
52
52
 
53
53
  #
@@ -57,6 +57,22 @@ class MailChimp
57
57
  self.email_server = server
58
58
  self.email_user = user
59
59
  self.email_password = password
60
+ return true
61
+ end
62
+
63
+ #
64
+ # Convenience method - converts a list of e-mails into an array
65
+ # suitable for a single subscribe_users or unsubscribe_users call.
66
+ #
67
+ def emails_to_hash_array(list_id, email_array)
68
+ output_array = Array::new
69
+ email_array.each do |email|
70
+ output_hash = Hash::new
71
+ output_hash['EMAIL'] = email
72
+ output_hash['id'] = list_id
73
+ output_array << output_hash
74
+ end
75
+ return output_array
60
76
  end
61
77
 
62
78
  #
@@ -75,10 +91,10 @@ class MailChimp
75
91
 
76
92
  #
77
93
  # Executes a batch subscribe of multiple users.
78
- # hash_array - An array of hash lines
94
+ # hash_array - An array of hashes, each representing a user.
79
95
  #
80
96
  def subscribe_users(hash_array)
81
- post_to_mailchimp(hash_array, "subscribe.phtml")
97
+ post_to_mailchimp(hash_array, "/subscribe.phtml")
82
98
  end
83
99
 
84
100
  #
@@ -98,18 +114,20 @@ class MailChimp
98
114
  # hash_array - An array of hash lines
99
115
  #
100
116
  def unsubscribe_users(hash_array)
101
- post_to_mailchimp(hash_array, "unsub.phtml")
117
+ post_to_mailchimp(hash_array, "/unsub.phtml")
102
118
  end
103
119
 
104
120
  #
105
121
  # Reads IMAP mail from the mailbox initialized with initialize_inbox.
106
122
  # Returns a subscribers array containing hashes of user data, and an
107
- # unsubscriber's array containing hashes of user data. Email, IP, and date as
108
- # a string are returned to the caller.
123
+ # unsubscriber's array containing hashes of user data, as well as two integers
124
+ # indicating how many messages were found in the inbox and how many were processed successfully.
125
+ # Email, IP, and a string representation of the date.
109
126
  #
110
127
  #
111
- def check_inbox(delete=nil, mode=nil)
112
- delete = false if (delete.nil?)
128
+ def check_inbox(delete=nil, mode=nil, delete_skipped=nil)
129
+ delete = true if (delete.nil?)
130
+ delete_skipped = false if delete_skipped.nil?
113
131
  if (!mode.nil? && mode.upcase != "IMAP")
114
132
  STDERR.write("Mode #{mode} not supported. Only IMAP supported at this time.")
115
133
  elsif (email_server.nil? || email_user.nil? || email_password.nil?)
@@ -120,19 +138,28 @@ class MailChimp
120
138
  imap = Net::IMAP.new(email_server)
121
139
  imap.authenticate('LOGIN', email_user, email_password)
122
140
  imap.select('INBOX')
141
+ messages_found = 0
142
+ messages_processed = 0
123
143
  imap.search(['ALL']).each do |message_id|
124
144
  msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
145
+ messages_found += 1
125
146
  stored_value = Processor.receive(msg)
126
- subscribes << stored_value if (!stored_value.nil? && stored_value[:type] == :subscription)
127
- unsubscribes << stored_value if (!stored_value.nil? && stored_value[:type] == :unsubscription)
128
- #Mark message as deleted and it will be removed from storage when user session closes
129
- imap.store(message_id, "+FLAGS", [:Deleted]) if (delete == true)
130
- end
131
-
147
+ if (!stored_value.nil? && stored_value[:type] == :subscription)
148
+ subscribes << stored_value
149
+ imap.store(message_id, "+FLAGS", [:Deleted]) if (delete == true)
150
+ messages_processed += 1
151
+ elsif (!stored_value.nil? && stored_value[:type] == :unsubscription)
152
+ unsubscribes << stored_value
153
+ imap.store(message_id, "+FLAGS", [:Deleted]) if (delete == true)
154
+ messages_processed += 1
155
+ else
156
+ STDERR.write("Skipping message with raw data: #{msg}")
157
+ imap.store(message_id, "+FLAGS", [:Deleted]) if (delete_skipped == true)
158
+ end
159
+ end
132
160
  imap.expunge if (delete == true)
133
-
134
161
  imap.disconnect
135
- return [subscribes, unsubscribes]
162
+ return [subscribes, unsubscribes, messages_found, messages_processed]
136
163
  end
137
164
  return nil
138
165
  end
@@ -143,14 +170,27 @@ class MailChimp
143
170
  # Handles internal posting to MailChimp, given a processor page.
144
171
  #
145
172
  def post_to_mailchimp(hash_array, page)
173
+ post_results = Array.new
146
174
  hash_array.each do |additional_fields|
147
175
  http = Net::HTTP.new("www.mailchimp.com")
148
176
  headers = {
149
177
  "Content-type" => "application/x-www-form-urlencoded",
150
178
  "User-Agent" => "MCSubscriber ruby-0.1"
151
179
  }
152
- response, body = http.post("/unsub.phtml", hash_to_uri(additional_fields), headers)
180
+ response, body = http.post(page, hash_to_uri(additional_fields), headers)
181
+ status = body.match(/(<result>)(.+)(<\/result>)/)
182
+ report = "Unknown"
183
+ if (!status.nil? && status.size == 4)
184
+ if (status[2] == "error")
185
+ error_details = body.match(/(<EMAILERROR>)(.+)(<\/EMAILERROR>)/)
186
+ report = error_details[2] if (!error_details.nil? && error_details.size == 4)
187
+ else
188
+ report = status[2]
189
+ end
190
+ end
191
+ post_results << [additional_fields['EMAIL'], report]
153
192
  end
193
+ return post_results
154
194
  end
155
195
 
156
196
  #
@@ -166,13 +206,9 @@ class MailChimp
166
206
 
167
207
  class Processor < ActionMailer::Base
168
208
  def receive(msg)
169
- p msg.subject
170
- p msg.subject =~ /New Subscribe to/
171
- p msg.subject =~ /Unsubscribe from/
172
209
  #Process all new subscriptions
173
210
  is_subscription = !((msg.subject =~ /New Subscribe to/).nil?)
174
211
  is_unsubscription = !((msg.subject =~ /Unsubscribe from/).nil?)
175
- p "status = #{is_subscription} #{is_unsubscription}"
176
212
  if is_subscription || is_unsubscription
177
213
  stored_value = Hash::new
178
214
  if (is_subscription)
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ruby-mailchimp
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
6
+ version: 0.2.0
7
7
  date: 2007-05-10 00:00:00 -04:00
8
8
  summary: Allows programmatic subscription and unsubscription to MailChimp. Requires MailChimp account.
9
9
  require_paths: