postmark 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.5
1
+ 0.5.0
data/lib/postmark.rb CHANGED
@@ -22,9 +22,11 @@ module Postmark
22
22
  'Accept' => 'application/json'
23
23
  }
24
24
 
25
+ MAX_RETRIES = 2
26
+
25
27
  class << self
26
28
  attr_accessor :host, :host_path, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
27
- :proxy_host, :proxy_port, :proxy_user, :proxy_pass
29
+ :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries
28
30
 
29
31
  attr_writer :response_parser_class
30
32
 
@@ -47,16 +49,22 @@ module Postmark
47
49
  @host_path ||= 'email'
48
50
  end
49
51
 
50
- # The HTTP open timeout (defaults to 2 seconds).
51
52
  def http_open_timeout
52
53
  @http_open_timeout ||= 5
53
54
  end
54
55
 
55
- # The HTTP read timeout (defaults to 15 seconds).
56
56
  def http_read_timeout
57
57
  @http_read_timeout ||= 15
58
58
  end
59
59
 
60
+ def max_retries
61
+ @max_retries ||= 3
62
+ end
63
+
64
+ def sleep_between_retries
65
+ @sleep_between_retries ||= 10
66
+ end
67
+
60
68
  def configure
61
69
  yield self
62
70
  end
@@ -70,6 +78,20 @@ module Postmark
70
78
  end
71
79
 
72
80
  def send_through_postmark(message) #:nodoc:
81
+ @retries = 0
82
+ begin
83
+ attempt_sending(message)
84
+ rescue Exception => e
85
+ if @retries < max_retries
86
+ @retries += 1
87
+ retry
88
+ else
89
+ raise
90
+ end
91
+ end
92
+ end
93
+
94
+ def attempt_sending(message)
73
95
  ResponseParsers.const_get(response_parser_class) # loads JSON lib, defining #to_json
74
96
  http = Net::HTTP::Proxy(proxy_host,
75
97
  proxy_port,
@@ -80,7 +102,7 @@ module Postmark
80
102
  http.open_timeout = http_open_timeout
81
103
  http.use_ssl = !!secure
82
104
 
83
- headers = HEADERS.merge({ "X-Postmark-Server-Token" => api_key })
105
+ headers = HEADERS.merge({ "X-Postmark-Server-Token" => api_key.to_s })
84
106
 
85
107
  response = http.post(url.path, convert_tmail(message).to_json, headers)
86
108
 
@@ -134,7 +156,8 @@ module Postmark
134
156
  headers = []
135
157
  message.each_header do |key, value|
136
158
  next if bogus_headers.include? key.dup.downcase
137
- headers << { "Name" => key, "Value" => value.body }
159
+ name = key.split(/-/).map {|i| i.capitalize }.join('-')
160
+ headers << { "Name" => name, "Value" => value.body }
138
161
  end
139
162
  headers
140
163
  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.4.5"
8
+ s.version = "0.5.0"
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"]
12
- s.date = %q{2010-01-25}
12
+ s.date = %q{2010-02-26}
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 = [
@@ -46,7 +46,7 @@ Gem::Specification.new do |s|
46
46
  }
47
47
  s.rdoc_options = ["--charset=UTF-8"]
48
48
  s.require_paths = ["lib"]
49
- s.rubygems_version = %q{1.3.5}
49
+ s.rubygems_version = %q{1.3.6}
50
50
  s.summary = %q{Ruby gem for sending emails through http://postmarkapp.com HTTP API}
51
51
  s.test_files = [
52
52
  "spec/postmark_spec.rb",
@@ -1,12 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Postmark" do
4
- context "configuration" do
5
- it "should allow configuration of host" do
6
- Postmark.configure { |config| config.host = "test" }
7
- Postmark.host.should == "test"
8
- end
9
- end
10
4
 
11
5
  let :message do
12
6
  TMail::Mail.new.tap do |mail|
@@ -27,69 +21,46 @@ describe "Postmark" do
27
21
  end
28
22
  end
29
23
 
30
- let :net_http_proxy do
31
- stub(:new => http_request)
32
- end
24
+ context "service call" do
33
25
 
34
- def http_response(code)
35
- Net::HTTPResponse.new("1.1", code, nil).tap do |resp|
36
- resp.stub! :body => '{ "Message": "OK" }'
26
+ before do
27
+ Postmark.sleep_between_retries = 0
37
28
  end
38
- end
39
-
40
- let :http_response_ok do
41
- http_response(200)
42
- end
43
-
44
- let :http_response_unauthorized do
45
- http_response(401)
46
- end
47
-
48
- let :http_response_invalid do
49
- http_response(422)
50
- end
51
-
52
- let :http_response_server_error do
53
- http_response(500)
54
- end
55
-
56
- let :http_response_unknown do
57
- http_response(503)
58
- end
59
-
60
- let :http_request do
61
- stub(:read_timeout= => nil, :open_timeout= => nil, :use_ssl= => nil)
62
- end
63
-
64
- before do
65
- Net::HTTP.stub!(:Proxy).and_return(net_http_proxy)
66
- end
67
29
 
68
- context "service call" do
69
30
  it "should send email successfully" do
70
- http_request.stub! :post => http_response_ok
71
- lambda { Postmark.send_through_postmark(message) }.should_not raise_error
31
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/", {})
32
+ Postmark.send_through_postmark(message)
33
+ FakeWeb.should have_requested(:post, "http://api.postmarkapp.com/email/")
72
34
  end
73
35
 
74
36
  it "should warn when header is invalid" do
75
- http_request.stub! :post => http_response_unauthorized
37
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/", {:status => [ "401", "Unauthorized" ], :body => "Missing API token"})
76
38
  lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InvalidApiKeyError)
77
39
  end
78
40
 
79
41
  it "should warn when json is not ok" do
80
- http_request.stub! :post => http_response_invalid
42
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/", {:status => [ "422", "Invalid" ], :body => "Invalid JSON"})
81
43
  lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InvalidMessageError)
82
44
  end
83
45
 
84
46
  it "should warn when server fails" do
85
- http_request.stub! :post => http_response_server_error
47
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/", {:status => [ "500", "Internal Server Error" ]})
86
48
  lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InternalServerError)
87
49
  end
88
50
 
89
51
  it "should warn when unknown stuff fails" do
90
- http_request.stub! :post => http_response_unknown
52
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/", {:status => [ "485", "Custom HTTP response status" ]})
91
53
  lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::UnknownError)
92
54
  end
55
+
56
+ it "should retry 3 times" do
57
+ FakeWeb.register_uri(:post, "http://api.postmarkapp.com/email/",
58
+ [ { :status => [ 500, "Internal Server Error" ] },
59
+ { :status => [ 500, "Internal Server Error" ] },
60
+ { } ]
61
+ )
62
+ lambda { Postmark.send_through_postmark(message) }.should_not raise_error
63
+ end
93
64
  end
94
65
 
95
66
  context "tmail parse" do
@@ -116,7 +87,7 @@ describe "Postmark" do
116
87
  end
117
88
 
118
89
  it "should encode headers properly" do
119
- json = %q[{"Subject":"Hello!", "From":"sheldon@bigbangtheory.com", "ReplyTo":"a@a.com, b@b.com", "To":"lenard@bigbangtheory.com", "TextBody":"Hello Sheldon!", "Headers":[{"Name":"custom-header", "Value":"header"}]}]
90
+ json = %q[{"Subject":"Hello!", "From":"sheldon@bigbangtheory.com", "ReplyTo":"a@a.com, b@b.com", "To":"lenard@bigbangtheory.com", "TextBody":"Hello Sheldon!", "Headers":[{"Name":"Custom-Header", "Value":"header"}]}]
120
91
  result = Postmark.convert_tmail(message_with_headers)
121
92
  result.should == JSON.parse(json)
122
93
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,8 @@ require 'rubygems'
5
5
  require 'active_support'
6
6
  require 'json'
7
7
  require 'ruby-debug'
8
+ require 'fakeweb'
9
+ require 'fakeweb_matcher'
8
10
  require 'spec'
9
11
  require 'spec/autorun'
10
12
 
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Petyo Ivanov
@@ -9,39 +14,45 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-25 00:00:00 +02:00
17
+ date: 2010-02-26 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: rspec
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  - !ruby/object:Gem::Dependency
26
33
  name: cucumber
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
33
41
  version: "0"
34
- version:
42
+ type: :development
43
+ version_requirements: *id002
35
44
  - !ruby/object:Gem::Dependency
36
45
  name: tmail
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
40
48
  requirements:
41
49
  - - ">="
42
50
  - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
43
53
  version: "0"
44
- version:
54
+ type: :runtime
55
+ version_requirements: *id003
45
56
  description: Ruby gem for sending emails through http://postmarkapp.com HTTP API. It relieas on TMail::Mail for message construction.
46
57
  email: underlog@gmail.com
47
58
  executables: []
@@ -84,18 +95,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
95
  requirements:
85
96
  - - ">="
86
97
  - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
87
100
  version: "0"
88
- version:
89
101
  required_rubygems_version: !ruby/object:Gem::Requirement
90
102
  requirements:
91
103
  - - ">="
92
104
  - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
93
107
  version: "0"
94
- version:
95
108
  requirements: []
96
109
 
97
110
  rubyforge_project:
98
- rubygems_version: 1.3.5
111
+ rubygems_version: 1.3.6
99
112
  signing_key:
100
113
  specification_version: 3
101
114
  summary: Ruby gem for sending emails through http://postmarkapp.com HTTP API