postmark 0.1.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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Petyo Ivanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ = Postmark gem
2
+
3
+ Ruby gem for sending emails through http://postmarkapp.com HTTP API
4
+
5
+ == Example
6
+
7
+ #!/usr/bin/env ruby
8
+
9
+ require 'rubygems'
10
+ require 'postmark'
11
+ require 'tmail'
12
+
13
+ Postmark.api_key = "your-api-key"
14
+
15
+ message = TMail::Mail.new
16
+ message.from = "leonard@bigbangtheory.com"
17
+ message.to = "Sheldon Cooper, Ph.D. <sheldon@bigbangtheory.com>"
18
+ message.subject = "Hi Sheldon!"
19
+ message.content_type = "text/html"
20
+ message.body = "Hello my friend!"
21
+
22
+ p Postmark.send_through_postmark(message).body
23
+
24
+ == Requirements
25
+
26
+ The gem relies on TMail for building the message. You will also need postmark account, server and sender signature set up to use it.
27
+ If you plan using it in a rails project, check out the postmark-rails gem, which is meant to integrate with ActionMailer.
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ * Send me a pull request. Bonus points for topic branches.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2009 Wildbit LLC. See LICENSE for details.
@@ -0,0 +1,63 @@
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{Ruby gem for sending emails through http://postmarkapp.com HTTP API}
9
+ gem.description = %Q{Ruby gem for sending emails through http://postmarkapp.com HTTP API. It relieas on TMail::Mail for message construction.}
10
+ gem.email = "underlog@gmail.com"
11
+ gem.homepage = "http://github.com/underlog/postmark"
12
+ gem.authors = ["Petyo Ivanov"]
13
+ gem.add_development_dependency "rspec"
14
+ gem.add_development_dependency "cucumber"
15
+ gem.add_dependency "tmail"
16
+ gem.files << "lib/postmark/tmail_mail_extension.rb"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ begin
39
+ require 'cucumber/rake/task'
40
+ Cucumber::Rake::Task.new(:features)
41
+
42
+ task :features => :check_dependencies
43
+ rescue LoadError
44
+ task :features do
45
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
46
+ end
47
+ end
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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,15 @@
1
+ Feature: sending emails through Postmark API
2
+ In order to communicate effectively with the world
3
+ My application should be able to deliver emails through the postmark API
4
+
5
+ Background:
6
+ Given the service listens to "http://postmarkapp.local"
7
+ And I have an account with api key "mykey"
8
+
9
+ Scenario: Sending simple email
10
+ Given I send the following email:
11
+ | From | leonard@bigbangtheory.com |
12
+ | To | sheldon@bigbangtheory.com |
13
+ | Subject | About that last night |
14
+ | Body | Sorry for not coming |
15
+ Then the service should receive an email on behalf of "mykey" for "sheldon@bigbangtheory.com"
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'postmark'
3
+
4
+ require 'spec/expectations'
@@ -0,0 +1,106 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'rubygems'
4
+ require 'active_support'
5
+ require 'tmail'
6
+ require 'json'
7
+ require 'postmark/tmail_mail_extension'
8
+
9
+ module Postmark
10
+
11
+ class InvalidApiKeyError < StandardError; end
12
+ class UnknownError < StandardError; end
13
+ class InvalidMessageError < StandardError; end
14
+ class InternalServerError < StandardError; end
15
+
16
+ HEADERS = {
17
+ 'Content-type' => 'application/json',
18
+ 'Accept' => 'application/json'
19
+ }
20
+
21
+ class << self
22
+ attr_accessor :host, :host_path, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
23
+ :proxy_host, :proxy_port, :proxy_user, :proxy_pass
24
+
25
+ # The port on which your Postmark server runs.
26
+ def port
27
+ @port || (secure ? 443 : 80)
28
+ end
29
+
30
+ # The host to connect to.
31
+ def host
32
+ @host ||= 'postmarkapp.com'
33
+ end
34
+
35
+ # The path of the listener
36
+ def host_path
37
+ @host_path ||= 'email'
38
+ end
39
+
40
+ # The HTTP open timeout (defaults to 2 seconds).
41
+ def http_open_timeout
42
+ @http_open_timeout ||= 2
43
+ end
44
+
45
+ # The HTTP read timeout (defaults to 5 seconds).
46
+ def http_read_timeout
47
+ @http_read_timeout ||= 5
48
+ end
49
+
50
+ def configure
51
+ yield self
52
+ end
53
+
54
+ def protocol #:nodoc:
55
+ secure ? "https" : "http"
56
+ end
57
+
58
+ def url #:nodoc:
59
+ URI.parse("#{protocol}://#{host}:#{port}/#{host_path}/")
60
+ end
61
+
62
+ def send_through_postmark(message) #:nodoc:
63
+ http = Net::HTTP::Proxy(proxy_host,
64
+ proxy_port,
65
+ proxy_user,
66
+ proxy_pass).new(url.host, url.port)
67
+
68
+ http.read_timeout = http_read_timeout
69
+ http.open_timeout = http_open_timeout
70
+ http.use_ssl = !!secure
71
+
72
+ headers = HEADERS.merge({ "X-Postmark-Server-Token" => api_key })
73
+
74
+ response = http.post(url.path, convert_tmail(message).to_json, headers)
75
+
76
+ case response.code.to_i
77
+ when 200
78
+ return response
79
+ when 401
80
+ raise InvalidApiKeyError, error_message(response.body)
81
+ when 422
82
+ raise InvalidMessageError, error_message(response.body)
83
+ when 500
84
+ raise InternalServerError, response.body
85
+ else
86
+ raise UnknownError, response
87
+ end
88
+ end
89
+
90
+ def error_message(response_body)
91
+ JSON.parse(response_body)["Message"]
92
+ end
93
+
94
+ def convert_tmail(message)
95
+ { "From" => message['from'].to_s, "To" => message['to'].to_s, "Subject" => message.subject }.tap do |hash|
96
+ if html = message.body_html
97
+ hash["HtmlBody"] = html
98
+ else
99
+ hash["TextBody"] = message.body
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,87 @@
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 'tmail_mail_extension'
11
+ # and every TMail::Mail object will have the .body_html method
12
+ #
13
+
14
+ module TMail
15
+ class Mail
16
+
17
+ #
18
+ # returs an String with just the html part of the body
19
+ # or nil if there is not any html part
20
+ #
21
+ def body_html
22
+ result = nil
23
+ if multipart?
24
+ parts.each do |part|
25
+ if part.multipart?
26
+ part.parts.each do |part2|
27
+ result = part2.unquoted_body if part2.content_type =~ /html/i
28
+ end
29
+ elsif !attachment?(part)
30
+ result = part.unquoted_body if part.content_type =~ /html/i
31
+ end
32
+ end
33
+ else
34
+ result = unquoted_body if content_type =~ /html/i
35
+ end
36
+ result
37
+ end
38
+
39
+ #
40
+ # This is only for develop.
41
+ # print on output all the parts of the Mail with some details
42
+ #
43
+ def parts_observer
44
+ puts "INI"
45
+ puts "content_type: #{content_type}"
46
+ puts "body: #{body}"
47
+ puts "parts.size: #{parts.size}"
48
+
49
+ if multipart?
50
+ parts.each_with_index do |part, index|
51
+ puts ""
52
+ puts " parts[#{index}]"
53
+ puts " content_type: #{part.content_type}"
54
+ puts " multipart? #{part.multipart?}"
55
+
56
+ header = part["content-type"]
57
+
58
+ if part.multipart?
59
+ puts " --multipartt--"
60
+ part.parts.each_with_index do |part2, index2|
61
+ puts " part[#{index}][#{index2}]"
62
+ puts " content_type: #{part2.content_type}"
63
+ puts " body: #{part2.unquoted_body}"
64
+ end
65
+ elsif header.nil?
66
+ puts " --header nil--"
67
+ elsif !attachment?(part)
68
+ puts " --no multipart, no header nil, no attachment--"
69
+ puts " content_type: #{part.content_type}"
70
+ puts " body: #{part.unquoted_body}"
71
+ else
72
+ puts " --no multipart, no header nil, attachment--"
73
+ puts " content_type: #{part.content_type}"
74
+ end
75
+
76
+ end
77
+ else
78
+ puts " --no multipart--"
79
+ puts " content_type: #{content_type}"
80
+ puts " body: #{unquoted_body}"
81
+ end
82
+
83
+ puts "END"
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{postmark}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Petyo Ivanov"]
12
+ s.date = %q{2009-11-04}
13
+ s.description = %q{Ruby gem for sending emails through http://postmarkapp.com HTTP API. It relieas on TMail::Mail for message construction.}
14
+ s.email = %q{underlog@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "features/postmark.feature",
27
+ "features/step_definitions/postmark_steps.rb",
28
+ "features/support/env.rb",
29
+ "lib/postmark.rb",
30
+ "lib/postmark/tmail_mail_extension.rb",
31
+ "lib/postmark/tmail_mail_extension.rb",
32
+ "postmark.gemspec",
33
+ "spec/postmark_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/underlog/postmark}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.4}
41
+ s.summary = %q{Ruby gem for sending emails through http://postmarkapp.com HTTP API}
42
+ s.test_files = [
43
+ "spec/postmark_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 0"])
53
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
54
+ s.add_runtime_dependency(%q<tmail>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ s.add_dependency(%q<cucumber>, [">= 0"])
58
+ s.add_dependency(%q<tmail>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 0"])
62
+ s.add_dependency(%q<cucumber>, [">= 0"])
63
+ s.add_dependency(%q<tmail>, [">= 0"])
64
+ end
65
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
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
+
11
+ let :message do
12
+ TMail::Mail.new.tap do |mail|
13
+ mail.from = "sheldon@bigbangtheory.com"
14
+ mail.to = "lenard@bigbangtheory.com"
15
+ mail.subject = "Hello!"
16
+ mail.body = "Hello Sheldon!"
17
+ end
18
+ end
19
+
20
+ let :html_message do
21
+ TMail::Mail.new.tap do |mail|
22
+ mail.from = "sheldon@bigbangtheory.com"
23
+ mail.to = "lenard@bigbangtheory.com"
24
+ mail.subject = "Hello!"
25
+ mail.body = "<b>Hello Sheldon!</b>"
26
+ mail.content_type = "text/html"
27
+ end
28
+ end
29
+
30
+ let :net_http_proxy do
31
+ stub(:new => http_request)
32
+ end
33
+
34
+ def http_response(code)
35
+ Net::HTTPResponse.new("1.1", code, nil).tap do |resp|
36
+ resp.stub! :body => '{ "Message": "OK" }'
37
+ 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
+
68
+ context "service call" do
69
+ 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
72
+ end
73
+
74
+ it "should warn when header is invalid" do
75
+ http_request.stub! :post => http_response_unauthorized
76
+ lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InvalidApiKeyError)
77
+ end
78
+
79
+ it "should warn when json is not ok" do
80
+ http_request.stub! :post => http_response_invalid
81
+ lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InvalidMessageError)
82
+ end
83
+
84
+ it "should warn when server fails" do
85
+ http_request.stub! :post => http_response_server_error
86
+ lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::InternalServerError)
87
+ end
88
+
89
+ it "should warn when unknown stuff fails" do
90
+ http_request.stub! :post => http_response_unknown
91
+ lambda { Postmark.send_through_postmark(message) }.should raise_error(Postmark::UnknownError)
92
+ end
93
+ end
94
+
95
+ context "tmail parse" do
96
+ it "should set text body for plain message" do
97
+ Postmark.convert_tmail(message)['TextBody'].should_not be_nil
98
+ end
99
+
100
+ it "should set html body for html message" do
101
+ Postmark.convert_tmail(html_message)['HtmlBody'].should_not be_nil
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'postmark'
4
+ require 'rubygems'
5
+ require 'ruby-debug'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Petyo Ivanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-04 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: cucumber
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: tmail
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Ruby gem for sending emails through http://postmarkapp.com HTTP API. It relieas on TMail::Mail for message construction.
46
+ email: underlog@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - features/postmark.feature
62
+ - features/step_definitions/postmark_steps.rb
63
+ - features/support/env.rb
64
+ - lib/postmark.rb
65
+ - lib/postmark/tmail_mail_extension.rb
66
+ - postmark.gemspec
67
+ - spec/postmark_spec.rb
68
+ - spec/spec.opts
69
+ - spec/spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/underlog/postmark
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.4
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Ruby gem for sending emails through http://postmarkapp.com HTTP API
98
+ test_files:
99
+ - spec/postmark_spec.rb
100
+ - spec/spec_helper.rb