postmark 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  = Changelog
2
2
 
3
+ == 0.9.2
4
+
5
+ * Fixed "Illegal email address ']'" bug on Ruby 1.9
6
+
3
7
  == 0.9.1
4
8
 
5
9
  * Fixed TypeError when calling Bounce.all.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 0.9.2
data/lib/postmark.rb CHANGED
@@ -16,11 +16,11 @@ require_local 'attachments_fix_for_mail'
16
16
 
17
17
  module Postmark
18
18
 
19
- class InvalidApiKeyError < StandardError; end
20
- class UnknownError < StandardError; end
19
+ class UnknownError < StandardError; end
20
+ class InvalidApiKeyError < StandardError; end
21
21
  class InvalidMessageError < StandardError; end
22
22
  class InternalServerError < StandardError; end
23
- class UnknownMessageType < StandardError; end
23
+ class UnknownMessageType < StandardError; end
24
24
 
25
25
  module ResponseParsers
26
26
  autoload :Json, 'postmark/response_parsers/json'
@@ -83,7 +83,7 @@ module Postmark
83
83
  def send_through_postmark(message) #:nodoc:
84
84
  @retries = 0
85
85
  begin
86
- HttpClient.post("email", Postmark::Json.encode(convert(message)))
86
+ HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
87
87
  rescue Exception => e
88
88
  if @retries < max_retries
89
89
  @retries += 1
@@ -94,42 +94,25 @@ module Postmark
94
94
  end
95
95
  end
96
96
 
97
- def convert(message)
98
- if defined?(TMail) && message.is_a?(TMail::Mail)
99
- convert_tmail(message)
100
- else
101
- convert_mail(message)
102
- end
103
- end
97
+ def convert_message_to_options_hash(message)
98
+ options = Hash.new
99
+ headers = extract_headers_according_to_message_format(message)
104
100
 
105
- def delivery_stats
106
- HttpClient.get("deliverystats")
107
- end
108
-
109
- protected
110
-
111
- def convert_mail(message)
112
- options = { "From" => message.from.to_s, "Subject" => message.subject }
113
-
114
- headers = extract_mail_headers(message)
115
- options["Headers"] = headers unless headers.length == 0
116
-
117
- options["To"] = message.to.join(", ")
118
-
119
- options["Tag"] = message.tag.to_s unless message.tag.nil?
120
-
121
- options["Cc"] = message.cc.join(", ").to_s unless message.cc.nil?
122
-
123
- options["Bcc"] = message.bcc.join(", ").to_s unless message.bcc.nil?
124
-
125
- options["Attachments"] = message.postmark_attachments unless message.postmark_attachments.nil?
126
-
127
- if reply_to = message['reply-to']
128
- options["ReplyTo"] = reply_to.to_s
129
- end
101
+ options["From"] = message.from.try(:first)
102
+ options["ReplyTo"] = message.reply_to.try(:join, ", ")
103
+ options["To"] = message.to.try(:join, ", ")
104
+ options["Cc"] = message.cc.try(:join, ", ")
105
+ options["Bcc"] = message.bcc.try(:join, ", ")
106
+ options["Subject"] = message.subject
107
+ options["Attachments"] = message.postmark_attachments
108
+ options["Tag"] = message.tag.to_s if message.tag
109
+ options["Headers"] = headers if headers.size > 0
130
110
 
111
+ options = options.delete_if{|k,v| v.nil?}
112
+
131
113
  html = message.body_html
132
114
  text = message.body_text
115
+
133
116
  if message.multipart?
134
117
  options["HtmlBody"] = html
135
118
  options["TextBody"] = text
@@ -138,9 +121,26 @@ module Postmark
138
121
  else
139
122
  options["TextBody"] = text
140
123
  end
124
+
141
125
  options
142
126
  end
143
127
 
128
+ def delivery_stats
129
+ HttpClient.get("deliverystats")
130
+ end
131
+
132
+ protected
133
+
134
+ def extract_headers_according_to_message_format(message)
135
+ if defined?(TMail) && message.is_a?(TMail::Mail)
136
+ headers = extract_tmail_headers(message)
137
+ elsif defined?(Mail) && message.kind_of?(Mail::Message)
138
+ headers = extract_mail_headers(message)
139
+ else
140
+ raise "Can't convert message to a valid hash of API options. Unknown message format."
141
+ end
142
+ end
143
+
144
144
  def extract_mail_headers(message)
145
145
  headers = []
146
146
  message.header.fields.each do |field|
@@ -153,37 +153,6 @@ module Postmark
153
153
  headers
154
154
  end
155
155
 
156
- def convert_tmail(message)
157
- options = { "From" => message['from'].to_s, "To" => message['to'].to_s, "Subject" => message.subject }
158
-
159
- headers = extract_tmail_headers(message)
160
- options["Headers"] = headers unless headers.length == 0
161
-
162
- options["Tag"] = message.tag.to_s unless message.tag.nil?
163
-
164
- options["Cc"] = message['cc'].to_s unless message.cc.nil?
165
-
166
- options["Bcc"] = message['bcc'].to_s unless message.bcc.nil?
167
-
168
- options["Attachments"] = message.postmark_attachments unless message.postmark_attachments.nil?
169
-
170
- if reply_to = message['reply-to']
171
- options["ReplyTo"] = reply_to.to_s
172
- end
173
-
174
- html = message.body_html
175
- text = message.body_text
176
- if message.multipart?
177
- options["HtmlBody"] = html
178
- options["TextBody"] = text
179
- elsif html
180
- options["HtmlBody"] = message.body_html
181
- else
182
- options["TextBody"] = text
183
- end
184
- options
185
- end
186
-
187
156
  def extract_tmail_headers(message)
188
157
  headers = []
189
158
  message.each_header do |key, value|
@@ -196,18 +165,12 @@ module Postmark
196
165
 
197
166
  def bogus_headers
198
167
  %q[
199
- return-path
200
- x-pm-rcpt
201
- from
202
- reply-to
203
- sender
204
- received
205
- date
206
- content-type
207
- cc
208
- bcc
209
- subject
210
- tag
168
+ return-path x-pm-rcpt
169
+ from reply-to
170
+ sender received
171
+ date content-type
172
+ cc bcc
173
+ subject tag
211
174
  attachment
212
175
  ]
213
176
  end
data/postmark.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{postmark}
8
- s.version = "0.9.1"
8
+ s.version = "0.9.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Petyo Ivanov", "Ilya Sabanin"]
12
- s.date = %q{2010-09-20}
12
+ s.date = %q{2010-09-29}
13
13
  s.description = %q{Ruby gem for sending emails through http://postmarkapp.com HTTP API. It relieas on TMail::Mail for message construction.}
14
14
  s.email = %q{underlog@gmail.com}
15
15
  s.extra_rdoc_files = [
data/spec/bounce_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe "Bounce" do
4
4
  let(:bounce_json) { %{{"Type":"HardBounce","TypeCode":1,"Details":"test bounce","Email":"jim@test.com","BouncedAt":"#{Time.now.to_s}","DumpAvailable":true,"Inactive":false,"CanActivate":true,"ID":12}} }
5
- let(:bounces_json) { "[#{bounce_json},#{bounce_json}]" }
5
+ let(:bounces_json) { %{{"Bounces": [#{bounce_json},#{bounce_json}]}} }
6
6
 
7
7
  context "single bounce" do
8
8
  let(:bounce) { Postmark::Bounce.find(12) }
@@ -113,16 +113,16 @@ describe "Postmark" do
113
113
  context "tmail parse" do
114
114
  def be_serialized_to(json)
115
115
  simple_matcher "be serialized to #{json}" do |message|
116
- Postmark.send(:convert_tmail, tmail_message).should == JSON.parse(json)
116
+ Postmark.send(:convert_message_to_options_hash, tmail_message).should == JSON.parse(json)
117
117
  end
118
118
  end
119
119
 
120
120
  it "should set text body for plain message" do
121
- Postmark.send(:convert_tmail, tmail_message)['TextBody'].should_not be_nil
121
+ Postmark.send(:convert_message_to_options_hash, tmail_message)['TextBody'].should_not be_nil
122
122
  end
123
123
 
124
124
  it "should set html body for html message" do
125
- Postmark.send(:convert_tmail, tmail_html_message)['HtmlBody'].should_not be_nil
125
+ Postmark.send(:convert_message_to_options_hash, tmail_html_message)['HtmlBody'].should_not be_nil
126
126
  end
127
127
 
128
128
  it "should encode custom headers headers properly" do
@@ -159,21 +159,21 @@ describe "Postmark" do
159
159
  context "mail parse" do
160
160
  def be_serialized_to(json)
161
161
  simple_matcher "be serialized to #{json}" do |message|
162
- Postmark.send(:convert_mail, mail_message).should == JSON.parse(json)
162
+ Postmark.send(:convert_message_to_options_hash, mail_message).should == JSON.parse(json)
163
163
  end
164
164
  end
165
165
 
166
166
  it "should set text body for plain message" do
167
- Postmark.send(:convert_mail, mail_message)['TextBody'].should_not be_nil
167
+ Postmark.send(:convert_message_to_options_hash, mail_message)['TextBody'].should_not be_nil
168
168
  end
169
169
 
170
170
  it "should set html body for html message" do
171
- Postmark.send(:convert_mail, mail_html_message)['HtmlBody'].should_not be_nil
171
+ Postmark.send(:convert_message_to_options_hash, mail_html_message)['HtmlBody'].should_not be_nil
172
172
  end
173
173
 
174
174
  it "should set html and text body for multipart message" do
175
- Postmark.send(:convert_mail, mail_multipart_message)['HtmlBody'].should_not be_nil
176
- Postmark.send(:convert_mail, mail_multipart_message)['TextBody'].should_not be_nil
175
+ Postmark.send(:convert_message_to_options_hash, mail_multipart_message)['HtmlBody'].should_not be_nil
176
+ Postmark.send(:convert_message_to_options_hash, mail_multipart_message)['TextBody'].should_not be_nil
177
177
  end
178
178
 
179
179
  it "should encode custom headers properly" do
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
3
4
  require 'mail'
4
5
  require 'tmail'
5
6
  require 'postmark'
6
- require 'rubygems'
7
7
  require 'active_support'
8
8
  require 'json'
9
9
  require 'ruby-debug'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postmark
3
3
  version: !ruby/object:Gem::Version
4
- hash: 57
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 1
10
- version: 0.9.1
9
+ - 2
10
+ version: 0.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Petyo Ivanov
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-09-20 00:00:00 +08:00
19
+ date: 2010-09-29 00:00:00 +08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency