postmark 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
data/.bundle/config CHANGED
@@ -1,3 +1,2 @@
1
- ---
2
- BUNDLE_WITHOUT: ""
3
- BUNDLE_DISABLE_SHARED_GEMS: "1"
1
+ ---
2
+ BUNDLE_WITHOUT: ''
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,12 @@
1
1
  = Changelog
2
2
 
3
+ == 0.9.11
4
+
5
+ * Replaced Jeweler by Bundler
6
+ * Updated RSpec to 2.8
7
+ * Fixed specs
8
+ * Refactored the codebase
9
+
3
10
  == 0.9.10
4
11
 
5
12
  * Fixed Ruby 1.9 compatibility issue
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in postmark.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development do
8
+
9
+ end
10
+
11
+
data/README.rdoc CHANGED
@@ -114,7 +114,10 @@ You can also explicitly specify which one to be used, using
114
114
  * Chris Williams
115
115
  * Aitor García Rey
116
116
  * James Miller
117
+ * Yury Batenko
118
+ * Pavel Maksimenko
119
+ * Anton Astashov
117
120
 
118
121
  == Copyright
119
122
 
120
- Copyright (c) 2011 Wildbit LLC. See LICENSE for details.
123
+ Copyright (c) 2012 Wildbit LLC. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,63 +1,3 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "postmark"
8
- gem.summary = %Q{Official Postmark API wrapper.}
9
- gem.description = %Q{Use this gem to send emails through Postmark HTTP API and retrieve info about bounces.}
10
- gem.email = "ilya@wildbit.com"
11
- gem.homepage = "http://postmarkapp.com"
12
- gem.authors = ["Petyo Ivanov", "Ilya Sabanin", "Artem Chistyakov"]
13
-
14
- gem.add_development_dependency "rspec"
15
- gem.add_development_dependency "activesupport"
16
- gem.add_development_dependency "json"
17
- gem.add_development_dependency "ruby-debug"
18
- gem.add_development_dependency "fakeweb"
19
- gem.add_development_dependency "fakeweb-matcher"
20
- gem.add_development_dependency "timecop"
21
- gem.add_development_dependency "yajl-ruby"
22
-
23
- gem.post_install_message = %q[
24
- ==================
25
- Thanks for installing the postmark gem. If you don't have an account, please sign up at http://postmarkapp.com/.
26
- Review the README.rdoc for implementation details and examples.
27
- ==================
28
- ]
29
- end
30
- Jeweler::GemcutterTasks.new
31
- rescue LoadError
32
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
33
- end
34
-
35
- require 'spec/rake/spectask'
36
- Spec::Rake::SpecTask.new(:spec) do |spec|
37
- spec.libs << 'lib' << 'spec'
38
- spec.spec_files = FileList['spec/**/*_spec.rb']
39
- end
40
-
41
- Spec::Rake::SpecTask.new(:rcov) do |spec|
42
- spec.libs << 'lib' << 'spec'
43
- spec.pattern = 'spec/**/*_spec.rb'
44
- spec.rcov = true
45
- end
46
-
47
- task :spec => :check_dependencies
48
-
49
- task :default => :spec
50
-
51
- require 'rake/rdoctask'
52
- Rake::RDocTask.new do |rdoc|
53
- if File.exist?('VERSION')
54
- version = File.read('VERSION')
55
- else
56
- version = ""
57
- end
58
-
59
- rdoc.rdoc_dir = 'rdoc'
60
- rdoc.title = "postmark #{version}"
61
- rdoc.rdoc_files.include('README*')
62
- rdoc.rdoc_files.include('lib/**/*.rb')
63
- end
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.10
1
+ 0.9.11
data/lib/postmark.rb CHANGED
@@ -36,148 +36,149 @@ module Postmark
36
36
 
37
37
  MAX_RETRIES = 2
38
38
 
39
- class << self
40
- attr_accessor :host, :path_prefix, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
41
- :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries
39
+ extend self
42
40
 
43
- attr_writer :response_parser_class
41
+ attr_accessor :host, :path_prefix, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
42
+ :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries
44
43
 
45
- def response_parser_class
46
- @response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
47
- end
44
+ attr_writer :response_parser_class
48
45
 
49
- # The port on which your Postmark server runs.
50
- def port
51
- @port || (secure ? 443 : 80)
52
- end
46
+ def response_parser_class
47
+ @response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
48
+ end
53
49
 
54
- # The host to connect to.
55
- def host
56
- @host ||= 'api.postmarkapp.com'
57
- end
50
+ # The port on which your Postmark server runs.
51
+ def port
52
+ @port || (secure ? 443 : 80)
53
+ end
58
54
 
59
- # The path of the listener
60
- def path_prefix
61
- @path_prefix ||= '/'
62
- end
55
+ # The host to connect to.
56
+ def host
57
+ @host ||= 'api.postmarkapp.com'
58
+ end
63
59
 
64
- def http_open_timeout
65
- @http_open_timeout ||= 5
66
- end
60
+ # The path of the listener
61
+ def path_prefix
62
+ @path_prefix ||= '/'
63
+ end
67
64
 
68
- def http_read_timeout
69
- @http_read_timeout ||= 15
70
- end
65
+ def http_open_timeout
66
+ @http_open_timeout ||= 5
67
+ end
71
68
 
72
- def max_retries
73
- @max_retries ||= 3
74
- end
69
+ def http_read_timeout
70
+ @http_read_timeout ||= 15
71
+ end
75
72
 
76
- def sleep_between_retries
77
- @sleep_between_retries ||= 10
78
- end
73
+ def max_retries
74
+ @max_retries ||= 3
75
+ end
79
76
 
80
- def configure
81
- yield self
82
- end
77
+ def sleep_between_retries
78
+ @sleep_between_retries ||= 10
79
+ end
83
80
 
84
- def send_through_postmark(message) #:nodoc:
85
- @retries = 0
86
- begin
87
- HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
88
- rescue DeliveryError => e
89
- if @retries < max_retries
90
- @retries += 1
91
- retry
92
- else
93
- raise
94
- end
95
- end
96
- end
81
+ def configure
82
+ yield self
83
+ end
97
84
 
98
- def convert_message_to_options_hash(message)
99
- options = Hash.new
100
- headers = extract_headers_according_to_message_format(message)
101
-
102
- options["From"] = message['from'].to_s if message.from
103
- options["ReplyTo"] = Array[message.reply_to].flatten.join(", ") if message.reply_to
104
- options["To"] = message['to'].to_s if message.to
105
- options["Cc"] = message['cc'].to_s if message.cc
106
- options["Bcc"] = Array[message.bcc].flatten.join(", ") if message.bcc
107
- options["Subject"] = message.subject
108
- options["Attachments"] = message.postmark_attachments
109
- options["Tag"] = message.tag.to_s if message.tag
110
- options["Headers"] = headers if headers.size > 0
111
-
112
- options = options.delete_if{|k,v| v.nil?}
113
-
114
- html = message.body_html
115
- text = message.body_text
116
-
117
- if message.multipart?
118
- options["HtmlBody"] = html
119
- options["TextBody"] = text
120
- elsif html
121
- options["HtmlBody"] = html
85
+ def send_through_postmark(message) #:nodoc:
86
+ @retries = 0
87
+ begin
88
+ HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
89
+ rescue DeliveryError => e
90
+ if @retries < max_retries
91
+ @retries += 1
92
+ retry
122
93
  else
123
- options["TextBody"] = text
94
+ raise
124
95
  end
125
-
126
- options
127
96
  end
97
+ end
128
98
 
129
- def delivery_stats
130
- HttpClient.get("deliverystats")
131
- end
99
+ def convert_message_to_options_hash(message)
100
+ options = Hash.new
101
+ headers = extract_headers_according_to_message_format(message)
132
102
 
133
- protected
103
+ options["From"] = message['from'].to_s if message.from
104
+ options["ReplyTo"] = Array[message.reply_to].flatten.join(", ") if message.reply_to
105
+ options["To"] = message['to'].to_s if message.to
106
+ options["Cc"] = message['cc'].to_s if message.cc
107
+ options["Bcc"] = Array[message.bcc].flatten.join(", ") if message.bcc
108
+ options["Subject"] = message.subject
109
+ options["Attachments"] = message.postmark_attachments
110
+ options["Tag"] = message.tag.to_s if message.tag
111
+ options["Headers"] = headers if headers.size > 0
134
112
 
135
- def extract_headers_according_to_message_format(message)
136
- if defined?(TMail) && message.is_a?(TMail::Mail)
137
- headers = extract_tmail_headers(message)
138
- elsif defined?(Mail) && message.kind_of?(Mail::Message)
139
- headers = extract_mail_headers(message)
140
- else
141
- raise "Can't convert message to a valid hash of API options. Unknown message format."
142
- end
113
+ options = options.delete_if{|k,v| v.nil?}
114
+
115
+ html = message.body_html
116
+ text = message.body_text
117
+
118
+ if message.multipart?
119
+ options["HtmlBody"] = html
120
+ options["TextBody"] = text
121
+ elsif html
122
+ options["HtmlBody"] = html
123
+ else
124
+ options["TextBody"] = text
143
125
  end
144
126
 
145
- def extract_mail_headers(message)
146
- headers = []
147
- message.header.fields.each do |field|
148
- key = field.name
149
- value = field.value
150
- next if bogus_headers.include? key.dup.downcase
151
- name = key.split(/-/).map {|i| i.capitalize }.join('-')
152
- headers << { "Name" => name, "Value" => value }
153
- end
154
- headers
127
+ options
128
+ end
129
+
130
+ def delivery_stats
131
+ HttpClient.get("deliverystats")
132
+ end
133
+
134
+ protected
135
+
136
+ def extract_headers_according_to_message_format(message)
137
+ if defined?(TMail) && message.is_a?(TMail::Mail)
138
+ headers = extract_tmail_headers(message)
139
+ elsif defined?(Mail) && message.kind_of?(Mail::Message)
140
+ headers = extract_mail_headers(message)
141
+ else
142
+ raise "Can't convert message to a valid hash of API options. Unknown message format."
155
143
  end
144
+ end
156
145
 
157
- def extract_tmail_headers(message)
158
- headers = []
159
- message.each_header do |key, value|
160
- next if bogus_headers.include? key.dup.downcase
161
- name = key.split(/-/).map {|i| i.capitalize }.join('-')
162
- headers << { "Name" => name, "Value" => value.body }
163
- end
164
- headers
146
+ def extract_mail_headers(message)
147
+ headers = []
148
+ message.header.fields.each do |field|
149
+ key = field.name
150
+ value = field.value
151
+ next if bogus_headers.include? key.downcase
152
+ name = key.split(/-/).map {|i| i.capitalize }.join('-')
153
+ headers << { "Name" => name, "Value" => value }
165
154
  end
155
+ headers
156
+ end
166
157
 
167
- def bogus_headers
168
- %q[
169
- return-path x-pm-rcpt
170
- from reply-to
171
- sender received
172
- date content-type
173
- cc bcc
174
- subject tag
175
- attachment
176
- ]
158
+ def extract_tmail_headers(message)
159
+ headers = []
160
+ message.each_header do |key, value|
161
+ next if bogus_headers.include? key.downcase
162
+ name = key.split(/-/).map {|i| i.capitalize }.join('-')
163
+ headers << { "Name" => name, "Value" => value.body }
177
164
  end
165
+ headers
166
+ end
178
167
 
168
+ def bogus_headers
169
+ %q[
170
+ return-path x-pm-rcpt
171
+ from reply-to
172
+ sender received
173
+ date content-type
174
+ cc bcc
175
+ subject tag
176
+ attachment
177
+ ]
179
178
  end
180
179
 
180
+
181
+
181
182
  self.response_parser_class = nil
182
183
 
183
184
  end
@@ -2,76 +2,75 @@ require 'cgi'
2
2
 
3
3
  module Postmark
4
4
  module HttpClient
5
- class << self
6
- def post(path, data = '')
7
- handle_response(http.post(url_path(path), data, headers))
8
- end
5
+ extend self
6
+ def post(path, data = '')
7
+ handle_response(http.post(url_path(path), data, headers))
8
+ end
9
9
 
10
- def put(path, data = '')
11
- handle_response(http.put(url_path(path), data, headers))
12
- end
10
+ def put(path, data = '')
11
+ handle_response(http.put(url_path(path), data, headers))
12
+ end
13
13
 
14
- def get(path, query = {})
15
- handle_response(http.get(url_path(path + to_query_string(query)), headers))
16
- end
14
+ def get(path, query = {})
15
+ handle_response(http.get(url_path(path + to_query_string(query)), headers))
16
+ end
17
17
 
18
- protected
18
+ protected
19
19
 
20
- def to_query_string(hash)
21
- return "" if hash.empty?
22
- "?" + hash.map { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join("&")
23
- end
20
+ def to_query_string(hash)
21
+ return "" if hash.empty?
22
+ "?" + hash.map { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join("&")
23
+ end
24
24
 
25
- def protocol
26
- Postmark.secure ? "https" : "http"
27
- end
25
+ def protocol
26
+ Postmark.secure ? "https" : "http"
27
+ end
28
28
 
29
- def url
30
- URI.parse("#{protocol}://#{Postmark.host}:#{Postmark.port}/")
31
- end
29
+ def url
30
+ URI.parse("#{protocol}://#{Postmark.host}:#{Postmark.port}/")
31
+ end
32
32
 
33
- def handle_response(response)
34
- case response.code.to_i
35
- when 200
36
- return Postmark::Json.decode(response.body)
37
- when 401
38
- raise InvalidApiKeyError, error_message(response.body)
39
- when 422
40
- raise InvalidMessageError, error_message(response.body)
41
- when 500
42
- raise InternalServerError, response.body
43
- else
44
- raise UnknownError, response
45
- end
33
+ def handle_response(response)
34
+ case response.code.to_i
35
+ when 200
36
+ return Postmark::Json.decode(response.body)
37
+ when 401
38
+ raise InvalidApiKeyError, error_message(response.body)
39
+ when 422
40
+ raise InvalidMessageError, error_message(response.body)
41
+ when 500
42
+ raise InternalServerError, response.body
43
+ else
44
+ raise UnknownError, response
46
45
  end
46
+ end
47
47
 
48
- def headers
49
- @headers ||= HEADERS.merge({ "X-Postmark-Server-Token" => Postmark.api_key.to_s })
50
- end
48
+ def headers
49
+ @headers ||= HEADERS.merge({ "X-Postmark-Server-Token" => Postmark.api_key.to_s })
50
+ end
51
51
 
52
- def url_path(path)
53
- Postmark.path_prefix + path
54
- end
52
+ def url_path(path)
53
+ Postmark.path_prefix + path
54
+ end
55
55
 
56
- def http
57
- @http ||= build_http
58
- end
56
+ def http
57
+ @http ||= build_http
58
+ end
59
59
 
60
- def build_http
61
- http = Net::HTTP::Proxy(Postmark.proxy_host,
62
- Postmark.proxy_port,
63
- Postmark.proxy_user,
64
- Postmark.proxy_pass).new(url.host, url.port)
60
+ def build_http
61
+ http = Net::HTTP::Proxy(Postmark.proxy_host,
62
+ Postmark.proxy_port,
63
+ Postmark.proxy_user,
64
+ Postmark.proxy_pass).new(url.host, url.port)
65
65
 
66
- http.read_timeout = Postmark.http_read_timeout
67
- http.open_timeout = Postmark.http_open_timeout
68
- http.use_ssl = !!Postmark.secure
69
- http
70
- end
66
+ http.read_timeout = Postmark.http_read_timeout
67
+ http.open_timeout = Postmark.http_open_timeout
68
+ http.use_ssl = !!Postmark.secure
69
+ http
70
+ end
71
71
 
72
- def error_message(response_body)
73
- Postmark::Json.decode(response_body)["Message"]
74
- end
72
+ def error_message(response_body)
73
+ Postmark::Json.decode(response_body)["Message"]
75
74
  end
76
75
  end
77
76
  end