postmark 0.9.19 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.travis.yml +8 -0
  2. data/CHANGELOG.rdoc +20 -0
  3. data/Gemfile +6 -0
  4. data/README.md +351 -91
  5. data/VERSION +1 -1
  6. data/lib/postmark.rb +40 -132
  7. data/lib/postmark/api_client.rb +162 -0
  8. data/lib/postmark/bounce.rb +20 -17
  9. data/lib/postmark/handlers/mail.rb +10 -3
  10. data/lib/postmark/helpers/hash_helper.rb +35 -0
  11. data/lib/postmark/helpers/message_helper.rb +62 -0
  12. data/lib/postmark/http_client.rb +44 -28
  13. data/lib/postmark/inbound.rb +21 -0
  14. data/lib/postmark/inflector.rb +28 -0
  15. data/lib/postmark/message_extensions/mail.rb +50 -5
  16. data/lib/postmark/message_extensions/shared.rb +23 -28
  17. data/lib/postmark/version.rb +1 -1
  18. data/postmark.gemspec +4 -7
  19. data/spec/data/empty.gif +0 -0
  20. data/spec/integration/api_client_hashes_spec.rb +101 -0
  21. data/spec/integration/api_client_messages_spec.rb +127 -0
  22. data/spec/integration/mail_delivery_method_spec.rb +80 -0
  23. data/spec/spec_helper.rb +15 -5
  24. data/spec/support/helpers.rb +11 -0
  25. data/spec/{shared_examples.rb → support/shared_examples.rb} +0 -0
  26. data/spec/unit/postmark/api_client_spec.rb +246 -0
  27. data/spec/unit/postmark/bounce_spec.rb +142 -0
  28. data/spec/unit/postmark/handlers/mail_spec.rb +39 -0
  29. data/spec/unit/postmark/helpers/hash_helper_spec.rb +34 -0
  30. data/spec/unit/postmark/helpers/message_helper_spec.rb +115 -0
  31. data/spec/unit/postmark/http_client_spec.rb +204 -0
  32. data/spec/unit/postmark/inbound_spec.rb +88 -0
  33. data/spec/unit/postmark/inflector_spec.rb +35 -0
  34. data/spec/unit/postmark/json_spec.rb +37 -0
  35. data/spec/unit/postmark/message_extensions/mail_spec.rb +205 -0
  36. data/spec/unit/postmark_spec.rb +164 -0
  37. metadata +45 -93
  38. data/lib/postmark/attachments_fix_for_mail.rb +0 -48
  39. data/lib/postmark/message_extensions/tmail.rb +0 -115
  40. data/spec/bounce_spec.rb +0 -53
  41. data/spec/postmark_spec.rb +0 -253
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.19
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-02-18 00:00:00.000000000 Z
14
+ date: 2013-05-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -45,22 +45,6 @@ dependencies:
45
45
  - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- - !ruby/object:Gem::Dependency
49
- name: tmail
50
- requirement: !ruby/object:Gem::Requirement
51
- none: false
52
- requirements:
53
- - - ! '>='
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
- type: :development
57
- prerelease: false
58
- version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
- requirements:
61
- - - ! '>='
62
- - !ruby/object:Gem::Version
63
- version: '0'
64
48
  - !ruby/object:Gem::Dependency
65
49
  name: mail
66
50
  requirement: !ruby/object:Gem::Requirement
@@ -77,22 +61,6 @@ dependencies:
77
61
  - - ! '>='
78
62
  - !ruby/object:Gem::Version
79
63
  version: '0'
80
- - !ruby/object:Gem::Dependency
81
- name: rspec-core
82
- requirement: !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ~>
86
- - !ruby/object:Gem::Version
87
- version: '2.0'
88
- type: :development
89
- prerelease: false
90
- version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
- requirements:
93
- - - ~>
94
- - !ruby/object:Gem::Version
95
- version: '2.0'
96
64
  - !ruby/object:Gem::Dependency
97
65
  name: activesupport
98
66
  requirement: !ruby/object:Gem::Requirement
@@ -109,54 +77,6 @@ dependencies:
109
77
  - - ~>
110
78
  - !ruby/object:Gem::Version
111
79
  version: '3.0'
112
- - !ruby/object:Gem::Dependency
113
- name: fakeweb
114
- requirement: !ruby/object:Gem::Requirement
115
- none: false
116
- requirements:
117
- - - ! '>='
118
- - !ruby/object:Gem::Version
119
- version: '0'
120
- type: :development
121
- prerelease: false
122
- version_requirements: !ruby/object:Gem::Requirement
123
- none: false
124
- requirements:
125
- - - ! '>='
126
- - !ruby/object:Gem::Version
127
- version: '0'
128
- - !ruby/object:Gem::Dependency
129
- name: fakeweb-matcher
130
- requirement: !ruby/object:Gem::Requirement
131
- none: false
132
- requirements:
133
- - - ! '>='
134
- - !ruby/object:Gem::Version
135
- version: '0'
136
- type: :development
137
- prerelease: false
138
- version_requirements: !ruby/object:Gem::Requirement
139
- none: false
140
- requirements:
141
- - - ! '>='
142
- - !ruby/object:Gem::Version
143
- version: '0'
144
- - !ruby/object:Gem::Dependency
145
- name: timecop
146
- requirement: !ruby/object:Gem::Requirement
147
- none: false
148
- requirements:
149
- - - ! '>='
150
- - !ruby/object:Gem::Version
151
- version: '0'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- none: false
156
- requirements:
157
- - - ! '>='
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
80
  - !ruby/object:Gem::Dependency
161
81
  name: yajl-ruby
162
82
  requirement: !ruby/object:Gem::Requirement
@@ -186,6 +106,7 @@ files:
186
106
  - .gitignore
187
107
  - .rake_tasks
188
108
  - .rspec
109
+ - .travis.yml
189
110
  - CHANGELOG.rdoc
190
111
  - Gemfile
191
112
  - LICENSE
@@ -194,27 +115,44 @@ files:
194
115
  - VERSION
195
116
  - init.rb
196
117
  - lib/postmark.rb
197
- - lib/postmark/attachments_fix_for_mail.rb
118
+ - lib/postmark/api_client.rb
198
119
  - lib/postmark/bounce.rb
199
120
  - lib/postmark/handlers/mail.rb
121
+ - lib/postmark/helpers/hash_helper.rb
122
+ - lib/postmark/helpers/message_helper.rb
200
123
  - lib/postmark/http_client.rb
124
+ - lib/postmark/inbound.rb
125
+ - lib/postmark/inflector.rb
201
126
  - lib/postmark/json.rb
202
127
  - lib/postmark/message_extensions/mail.rb
203
128
  - lib/postmark/message_extensions/shared.rb
204
- - lib/postmark/message_extensions/tmail.rb
205
129
  - lib/postmark/response_parsers/active_support.rb
206
130
  - lib/postmark/response_parsers/json.rb
207
131
  - lib/postmark/response_parsers/yajl.rb
208
132
  - lib/postmark/version.rb
209
133
  - postmark.gemspec
210
- - spec/bounce_spec.rb
211
- - spec/postmark_spec.rb
212
- - spec/shared_examples.rb
134
+ - spec/data/empty.gif
135
+ - spec/integration/api_client_hashes_spec.rb
136
+ - spec/integration/api_client_messages_spec.rb
137
+ - spec/integration/mail_delivery_method_spec.rb
213
138
  - spec/spec_helper.rb
139
+ - spec/support/helpers.rb
140
+ - spec/support/shared_examples.rb
141
+ - spec/unit/postmark/api_client_spec.rb
142
+ - spec/unit/postmark/bounce_spec.rb
143
+ - spec/unit/postmark/handlers/mail_spec.rb
144
+ - spec/unit/postmark/helpers/hash_helper_spec.rb
145
+ - spec/unit/postmark/helpers/message_helper_spec.rb
146
+ - spec/unit/postmark/http_client_spec.rb
147
+ - spec/unit/postmark/inbound_spec.rb
148
+ - spec/unit/postmark/inflector_spec.rb
149
+ - spec/unit/postmark/json_spec.rb
150
+ - spec/unit/postmark/message_extensions/mail_spec.rb
151
+ - spec/unit/postmark_spec.rb
214
152
  homepage: http://postmarkapp.com
215
153
  licenses: []
216
154
  post_install_message: ! "\n ==================\n Thanks for installing the postmark
217
- gem. If you don't have an account, please sign up at http://postmarkapp.com/.\n
155
+ gem. If you don't have an account, please\n sign up at http://postmarkapp.com/.\n\n
218
156
  \ Review the README.md for implementation details and examples.\n ==================\n
219
157
  \ "
220
158
  rdoc_options:
@@ -229,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
167
  version: '0'
230
168
  segments:
231
169
  - 0
232
- hash: -1526820895297093205
170
+ hash: -1032735191678886837
233
171
  required_rubygems_version: !ruby/object:Gem::Requirement
234
172
  none: false
235
173
  requirements:
@@ -238,12 +176,26 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
176
  version: 1.3.7
239
177
  requirements: []
240
178
  rubyforge_project:
241
- rubygems_version: 1.8.24
179
+ rubygems_version: 1.8.25
242
180
  signing_key:
243
181
  specification_version: 3
244
182
  summary: Official Postmark API wrapper.
245
183
  test_files:
246
- - spec/bounce_spec.rb
247
- - spec/postmark_spec.rb
248
- - spec/shared_examples.rb
184
+ - spec/data/empty.gif
185
+ - spec/integration/api_client_hashes_spec.rb
186
+ - spec/integration/api_client_messages_spec.rb
187
+ - spec/integration/mail_delivery_method_spec.rb
249
188
  - spec/spec_helper.rb
189
+ - spec/support/helpers.rb
190
+ - spec/support/shared_examples.rb
191
+ - spec/unit/postmark/api_client_spec.rb
192
+ - spec/unit/postmark/bounce_spec.rb
193
+ - spec/unit/postmark/handlers/mail_spec.rb
194
+ - spec/unit/postmark/helpers/hash_helper_spec.rb
195
+ - spec/unit/postmark/helpers/message_helper_spec.rb
196
+ - spec/unit/postmark/http_client_spec.rb
197
+ - spec/unit/postmark/inbound_spec.rb
198
+ - spec/unit/postmark/inflector_spec.rb
199
+ - spec/unit/postmark/json_spec.rb
200
+ - spec/unit/postmark/message_extensions/mail_spec.rb
201
+ - spec/unit/postmark_spec.rb
@@ -1,48 +0,0 @@
1
- module Postmark
2
-
3
- #
4
- # This fix is needed solely to support neat Rails 3 syntax to create emails.
5
- # That one:
6
- #
7
- # def invitation
8
- # mail(
9
- # :to => "someone@example.com",
10
- # :postmark_attachments => [File.open(...)]
11
- # )
12
- # end
13
- #
14
- # That code will automatically put the file to Mail::OptionalField of the Mail::Message object
15
- # and will try to encode it before delivery. You are not supposed to store files in
16
- # such fields, so Mail will raise an exception. That's why before we actually perform a
17
- # delivery we have to remove the files from OptionalField to a regular @_attachments variable.
18
- #
19
- module AttachmentsFixForMail
20
-
21
- def self.included(base)
22
- base.class_eval do
23
- alias_method :deliver_without_postmark_hook, :deliver
24
- alias_method :deliver_without_postmark_hook!, :deliver!
25
-
26
- def deliver!
27
- remove_postmark_attachments_from_standard_fields
28
- deliver_without_postmark_hook!
29
- end
30
-
31
- def deliver
32
- remove_postmark_attachments_from_standard_fields
33
- deliver_without_postmark_hook
34
- end
35
- end
36
- end
37
-
38
- private
39
-
40
- def remove_postmark_attachments_from_standard_fields
41
- field = self['POSTMARK-ATTACHMENTS']
42
- return if field.nil?
43
- self.postmark_attachments = field.value
44
- header.fields.delete_if{|f| f.name == 'postmark-attachments'}
45
- end
46
- end
47
-
48
- end
@@ -1,115 +0,0 @@
1
- ##
2
- # author: http://fernandoguillen.info
3
- # date: 2008-08-27
4
- #
5
- # When you have a TMail::Mail with plain text and html body parts
6
- # there is not any easy way to extract just the html body part
7
- #
8
- # This is a patch to try to resolve this
9
- #
10
- # just require 'extensions/tmail'
11
- # and every TMail::Mail object will have the .body_html method
12
- #
13
-
14
- module TMail
15
- class Mail
16
-
17
- include Postmark::SharedMessageExtensions
18
-
19
- #
20
- # returs an String with just the html part of the body
21
- # or nil if there is not any html part
22
- #
23
- def body_html
24
- result = nil
25
- if multipart?
26
- parts.each do |part|
27
- if part.multipart?
28
- part.parts.each do |part2|
29
- result = part2.unquoted_body if part2.content_type =~ /html/i
30
- end
31
- elsif !attachment?(part)
32
- result = part.unquoted_body if part.content_type =~ /html/i
33
- end
34
- end
35
- else
36
- result = unquoted_body if content_type =~ /html/i
37
- end
38
- result
39
- end
40
-
41
- #
42
- # returs an String with just the plain text part of the body
43
- # or nil if there is not any plain text part
44
- #
45
- def body_text
46
- result = unquoted_body
47
- if multipart?
48
- parts.each do |part|
49
- if part.multipart?
50
- part.parts.each do |part2|
51
- result = part2.unquoted_body if part2.content_type =~ /plain/i
52
- end
53
- elsif !attachment?(part)
54
- result = part.unquoted_body if part.content_type =~ /plain/i
55
- end
56
- end
57
- else
58
- result = unquoted_body if content_type =~ /plain/i
59
- end
60
- result
61
- end
62
-
63
- #
64
- # This is only for develop.
65
- # print on output all the parts of the Mail with some details
66
- #
67
- def parts_observer
68
- puts "INI"
69
- puts "content_type: #{content_type}"
70
- puts "body: #{body}"
71
- puts "parts.size: #{parts.size}"
72
-
73
- if multipart?
74
- parts.each_with_index do |part, index|
75
- puts ""
76
- puts " parts[#{index}]"
77
- puts " content_type: #{part.content_type}"
78
- puts " multipart? #{part.multipart?}"
79
-
80
- header = part["content-type"]
81
-
82
- if part.multipart?
83
- puts " --multipart--"
84
- part.parts.each_with_index do |part2, index2|
85
- puts " part[#{index}][#{index2}]"
86
- puts " content_type: #{part2.content_type}"
87
- puts " body: #{part2.unquoted_body}"
88
- end
89
- elsif header.nil?
90
- puts " --header nil--"
91
- elsif !attachment?(part)
92
- puts " --no multipart, no header nil, no attachment--"
93
- puts " content_type: #{part.content_type}"
94
- puts " body: #{part.unquoted_body}"
95
- else
96
- puts " --no multipart, no header nil, attachment--"
97
- puts " content_type: #{part.content_type}"
98
- end
99
-
100
- end
101
- else
102
- puts " --no multipart--"
103
- puts " content_type: #{content_type}"
104
- puts " body: #{unquoted_body}"
105
- end
106
-
107
- puts "END"
108
- end
109
-
110
- def export_attachments
111
- postmark_attachments
112
- end
113
-
114
- end
115
- end
@@ -1,53 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Bounce" do
4
- let(:bounce_json) { %{{"Type":"HardBounce","MessageID":"d12c2f1c-60f3-4258-b163-d17052546ae4","TypeCode":1,"Details":"test bounce","Email":"jim@test.com","BouncedAt":"#{Time.now.to_s}","DumpAvailable":true,"Inactive":false,"CanActivate":true,"ID":12,"Subject":"Hello from our app!"}} }
5
- let(:bounces_json) { %{{"Bounces": [#{bounce_json},#{bounce_json}]}} }
6
-
7
- context "single bounce" do
8
- let(:bounce) { Postmark::Bounce.find(12) }
9
-
10
- before do
11
- Timecop.freeze
12
- FakeWeb.register_uri(:get, "http://api.postmarkapp.com/bounces/12", { :body => bounce_json })
13
- end
14
-
15
- after do
16
- Timecop.return
17
- end
18
-
19
- it "should retrieve and parce bounce correctly" do
20
- bounce.type.should == "HardBounce"
21
- bounce.bounced_at.to_s.should == Time.now.to_s
22
- bounce.details.should == "test bounce"
23
- bounce.email.should == "jim@test.com"
24
- bounce.message_id.should == "d12c2f1c-60f3-4258-b163-d17052546ae4"
25
- bounce.subject.should == "Hello from our app!"
26
- end
27
-
28
- it "should retrieve bounce dump" do
29
- FakeWeb.register_uri(:get, "http://api.postmarkapp.com/bounces/12/dump", { :body => %{{"Body": "Some SMTP gibberish"}} } )
30
- bounce.dump.should == "Some SMTP gibberish"
31
- end
32
-
33
- it "should activate inactive bounce" do
34
- FakeWeb.register_uri(:put, "http://api.postmarkapp.com/bounces/12/activate", { :body => %{{"Message":"OK","Bounce":#{bounce_json}}} } )
35
- bounce.activate.should be_a(Postmark::Bounce)
36
- end
37
-
38
- end
39
-
40
- it "should retrieve bounces" do
41
- FakeWeb.register_uri(:get, "http://api.postmarkapp.com/bounces?count=30&offset=0", { :body => bounces_json })
42
- bounces = Postmark::Bounce.all
43
- bounces.should have(2).entries
44
- bounces[0].should be_a(Postmark::Bounce)
45
- end
46
-
47
- it "should retrieve bounce tags" do
48
- FakeWeb.register_uri(:get, "http://api.postmarkapp.com/bounces/tags", { :body => '["Signup","Commit Notification"]' })
49
- tags = Postmark::Bounce.tags
50
- tags.should have(2).entries
51
- tags.first.should == "Signup"
52
- end
53
- end
@@ -1,253 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Postmark do
4
-
5
- let :tmail_message do
6
- TMail::Mail.new.tap do |mail|
7
- mail.from = "sheldon@bigbangtheory.com"
8
- mail.to = "lenard@bigbangtheory.com"
9
- mail.subject = "Hello!"
10
- mail.body = "Hello Sheldon!"
11
- end
12
- end
13
-
14
- let :tmail_html_message do
15
- TMail::Mail.new.tap do |mail|
16
- mail.from = "sheldon@bigbangtheory.com"
17
- mail.to = "lenard@bigbangtheory.com"
18
- mail.subject = "Hello!"
19
- mail.body = "<b>Hello Sheldon!</b>"
20
- mail.content_type = "text/html"
21
- end
22
- end
23
-
24
- let :mail_message do
25
- Mail.new do
26
- from "sheldon@bigbangtheory.com"
27
- to "lenard@bigbangtheory.com"
28
- subject "Hello!"
29
- body "Hello Sheldon!"
30
- end
31
- end
32
-
33
- let :mail_html_message do
34
- mail = Mail.new do
35
- from "sheldon@bigbangtheory.com"
36
- to "lenard@bigbangtheory.com"
37
- subject "Hello!"
38
- html_part do
39
- body "<b>Hello Sheldon!</b>"
40
- end
41
- end
42
- end
43
-
44
- let :mail_multipart_message do
45
- mail = Mail.new do
46
- from "sheldon@bigbangtheory.com"
47
- to "lenard@bigbangtheory.com"
48
- subject "Hello!"
49
- text_part do
50
- body "Hello Sheldon!"
51
- end
52
- html_part do
53
- body "<b>Hello Sheldon!</b>"
54
- end
55
- end
56
- end
57
-
58
- context "service call" do
59
-
60
- before(:all) do
61
- Postmark.sleep_between_retries = 0
62
- end
63
-
64
- def stub_web!(data={})
65
- data[:body] ||= response_body(data[:status].nil? ? 200 : data[:status].first)
66
- FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email", data)
67
- end
68
-
69
- def response_body(status, message="")
70
- body = {"ErrorCode" => status, "Message" => message}.to_json
71
- end
72
-
73
- it "should send email successfully" do
74
- stub_web!
75
- Postmark.send_through_postmark(mail_message)
76
- FakeWeb.should have_requested(:post, "http://api.postmarkapp.com/email")
77
- end
78
-
79
- it "should warn when header is invalid" do
80
- stub_web!({:status => [ "401", "Unauthorized" ]})
81
- lambda { Postmark.send_through_postmark(mail_message) }.should raise_error(Postmark::InvalidApiKeyError)
82
- end
83
-
84
- it "should warn when json is not ok" do
85
- stub_web!({:status => [ "422", "Invalid" ]})
86
- lambda { Postmark.send_through_postmark(mail_message) }.should raise_error(Postmark::InvalidMessageError)
87
- end
88
-
89
- it "should warn when server fails" do
90
- stub_web!({:status => [ "500", "Internal Server Error" ]})
91
- lambda { Postmark.send_through_postmark(mail_message) }.should raise_error(Postmark::InternalServerError)
92
- end
93
-
94
- it "should warn when unknown stuff fails" do
95
- stub_web!({:status => [ "485", "Custom HTTP response status" ]})
96
- lambda { Postmark.send_through_postmark(mail_message) }.should raise_error(Postmark::UnknownError)
97
- end
98
-
99
- it "should warn when the request times out" do
100
- Postmark::HttpClient.should_receive(:post).at_least(:once).and_raise(Timeout::Error)
101
- lambda { Postmark.send_through_postmark(mail_message) }.should raise_error(Postmark::TimeoutError)
102
- end
103
-
104
- it "should retry 3 times" do
105
- FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email",
106
- [
107
- { :status => [ 500, "Internal Server Error" ], :body => response_body(500, 'Internal Server Error') },
108
- { :status => [ 500, "Internal Server Error" ], :body => response_body(500, 'Internal Server Error') },
109
- { :body => response_body(500, 'Internal Server Error') }
110
- ])
111
- lambda { Postmark.send_through_postmark(mail_message) }.should_not raise_error
112
- end
113
-
114
- it "should retry on timeout" do
115
- Postmark::HttpClient.should_receive(:post).and_raise(Timeout::Error)
116
- Postmark::HttpClient.should_receive(:post).and_return('{}')
117
- lambda { Postmark.send_through_postmark(mail_message) }.should_not raise_error
118
- end
119
- end
120
-
121
- context "delivery stats" do
122
- let(:response_body) { %{{"InactiveMails":1,"Bounces":[{"TypeCode":0,"Name":"All","Count":2},{"Type":"HardBounce","TypeCode":1,"Name":"Hard bounce","Count":1},{"Type":"SoftBounce","TypeCode":4096,"Name":"Soft bounce","Count":1}]}} }
123
-
124
- it "should query the service for delivery stats" do
125
- FakeWeb.register_uri(:get, "http://api.postmarkapp.com/deliverystats", { :body => response_body })
126
- results = Postmark.delivery_stats
127
- results["InactiveMails"].should == 1
128
- results["Bounces"].should be_an(Array)
129
- results["Bounces"].should have(3).entries
130
- FakeWeb.should have_requested(:get, "http://api.postmarkapp.com/deliverystats")
131
- end
132
- end
133
-
134
- context "tmail parse", :ruby => 1.8 do
135
- require 'tmail'
136
- subject { tmail_message }
137
- it_behaves_like :mail
138
- end
139
-
140
- context "when mail parse" do
141
- subject { mail_message }
142
- it_behaves_like :mail
143
-
144
- it "should set html body for html message" do
145
- Postmark.send(:convert_message_to_options_hash, mail_html_message)['HtmlBody'].should_not be_nil
146
- end
147
-
148
- it "should encode custom headers properly" do
149
- subject.header["CUSTOM-HEADER"] = "header"
150
- subject.should be_serialized_to %q[{"Subject":"Hello!", "From":"sheldon@bigbangtheory.com", "To":"lenard@bigbangtheory.com", "TextBody":"Hello Sheldon!", "Headers":[{"Name":"Custom-Header", "Value":"header"}]}]
151
- end
152
- end
153
-
154
- context "mail delivery method" do
155
- it "should be able to set delivery_method" do
156
- mail_message.delivery_method Mail::Postmark
157
- end
158
-
159
- it "should wrap Postmark.send_through_postmark" do
160
- message = mail_message
161
- Postmark.should_receive(:send_through_postmark).with(message)
162
- mail_message.delivery_method Mail::Postmark
163
- mail_message.deliver
164
- end
165
-
166
- it "should allow setting of api_key" do
167
- mail_message.delivery_method Mail::Postmark, {:api_key => 'api-key'}
168
- mail_message.delivery_method.settings[:api_key].should == 'api-key'
169
- end
170
- end
171
-
172
- context "attachments hook", :ruby => 1.9 do
173
- before(:all) { Mail::Message.send(:include, Postmark::AttachmentsFixForMail) }
174
-
175
- before do
176
- mail_message.delivery_method Mail::Postmark
177
- mail_message.should_receive(:remove_postmark_attachments_from_standard_fields)
178
- Postmark.should_receive(:send_through_postmark).with(mail_message)
179
- end
180
-
181
- it "should run postmark attachments hook when using deliver! method" do
182
- mail_message.deliver!
183
- end
184
-
185
- it "should run postmark attachments hook when using deliver method" do
186
- mail_message.deliver
187
- end
188
- end
189
-
190
- context "attachments setter" do
191
- let(:attached_file) { mock("file") }
192
- let(:attached_hash) { {'Name' => 'picture.jpeg',
193
- 'ContentType' => 'image/jpeg'} }
194
- let(:exported_file) { {'Name' => 'file.jpeg',
195
- 'ContentType' => 'application/octet-stream',
196
- 'Content' => ''} }
197
-
198
- before do
199
- attached_file.stub(:is_a?) { |arg| arg == File ? true : false }
200
- attached_file.stub(:path) { '/tmp/file.jpeg' }
201
- end
202
-
203
- it "should store attachments as array" do
204
- mail_message.postmark_attachments = attached_hash
205
- mail_message.postmark_attachments.should be_kind_of(Array)
206
- end
207
-
208
- it "should save the attachments in attachments array" do
209
- IO.should_receive(:read).with("/tmp/file.jpeg").and_return("")
210
-
211
- mail_message.postmark_attachments = [attached_hash, attached_file]
212
- attachments = mail_message.export_attachments
213
-
214
- attachments.should include(attached_hash)
215
- attachments.should include(exported_file)
216
- end
217
- end
218
-
219
- context "native attachments" do
220
- let(:file_data) { 'binarydatahere' }
221
- let(:exported_data) do
222
- {'Name' => 'face.jpeg',
223
- 'Content' => "YmluYXJ5ZGF0YWhlcmU=\n",
224
- 'ContentType' => 'image/jpeg'}
225
- end
226
-
227
- before do
228
- mail_message.attachments["face.jpeg"] = file_data
229
- end
230
-
231
- it "exports native attachments" do
232
- attachments = mail_message.export_attachments
233
- attachments.should include(exported_data)
234
- end
235
- end
236
-
237
- context "JSON library support" do
238
- [:Json, :ActiveSupport, :Yajl].each do |lib|
239
- begin
240
- original_parser_class = Postmark.response_parser_class
241
-
242
- it "decodes json with #{lib}" do
243
- Postmark.response_parser_class = lib
244
- Postmark::Json.decode(%({"Message":"OK"})).should == { "Message" => "OK" }
245
- end
246
-
247
- Postmark.response_parser_class = original_parser_class
248
- rescue LoadError # No ActiveSupport or Yajl :(
249
- end
250
- end
251
- end
252
-
253
- end