Skip to content

Commit

Permalink
Merge pull request #19 from krasdevmeetup/master
Browse files Browse the repository at this point in the history
Update gem environment.
  • Loading branch information
temochka committed Jul 4, 2012
2 parents 748219b + 894c92f commit 5d41761
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 455 deletions.
5 changes: 2 additions & 3 deletions .bundle/config
@@ -1,3 +1,2 @@
---
BUNDLE_WITHOUT: ""
BUNDLE_DISABLE_SHARED_GEMS: "1"
---
BUNDLE_WITHOUT: ''
9 changes: 4 additions & 5 deletions .gitignore
@@ -1,5 +1,4 @@
*.sw?
.DS_Store
coverage
rdoc
pkg
*.gem
.bundle
Gemfile.lock
pkg/*
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--colour
--format documentation
7 changes: 7 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,12 @@
= Changelog

== 0.9.11

* Replaced Jeweler by Bundler
* Updated RSpec to 2.8
* Fixed specs
* Refactored the codebase

== 0.9.10

* Fixed Ruby 1.9 compatibility issue
Expand Down
11 changes: 11 additions & 0 deletions Gemfile
@@ -0,0 +1,11 @@
source "http://rubygems.org"

# Specify your gem's dependencies in postmark.gemspec
gemspec


group :development do

end


5 changes: 4 additions & 1 deletion README.rdoc
Expand Up @@ -114,7 +114,10 @@ You can also explicitly specify which one to be used, using
* Chris Williams
* Aitor García Rey
* James Miller
* Yury Batenko
* Pavel Maksimenko
* Anton Astashov

== Copyright

Copyright (c) 2011 Wildbit LLC. See LICENSE for details.
Copyright (c) 2012 Wildbit LLC. See LICENSE for details.
66 changes: 3 additions & 63 deletions Rakefile
@@ -1,63 +1,3 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "postmark"
gem.summary = %Q{Official Postmark API wrapper.}
gem.description = %Q{Use this gem to send emails through Postmark HTTP API and retrieve info about bounces.}
gem.email = "ilya@wildbit.com"
gem.homepage = "http://postmarkapp.com"
gem.authors = ["Petyo Ivanov", "Ilya Sabanin", "Artem Chistyakov"]

gem.add_development_dependency "rspec"
gem.add_development_dependency "activesupport"
gem.add_development_dependency "json"
gem.add_development_dependency "ruby-debug"
gem.add_development_dependency "fakeweb"
gem.add_development_dependency "fakeweb-matcher"
gem.add_development_dependency "timecop"
gem.add_development_dependency "yajl-ruby"

gem.post_install_message = %q[
==================
Thanks for installing the postmark gem. If you don't have an account, please sign up at http://postmarkapp.com/.
Review the README.rdoc for implementation details and examples.
==================
]
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

task :spec => :check_dependencies

task :default => :spec

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION')
version = File.read('VERSION')
else
version = ""
end

rdoc.rdoc_dir = 'rdoc'
rdoc.title = "postmark #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.9.10
0.9.11
229 changes: 115 additions & 114 deletions lib/postmark.rb
Expand Up @@ -36,148 +36,149 @@ module ResponseParsers

MAX_RETRIES = 2

class << self
attr_accessor :host, :path_prefix, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries
extend self

attr_writer :response_parser_class
attr_accessor :host, :path_prefix, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries

def response_parser_class
@response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
end
attr_writer :response_parser_class

# The port on which your Postmark server runs.
def port
@port || (secure ? 443 : 80)
end
def response_parser_class
@response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
end

# The host to connect to.
def host
@host ||= 'api.postmarkapp.com'
end
# The port on which your Postmark server runs.
def port
@port || (secure ? 443 : 80)
end

# The path of the listener
def path_prefix
@path_prefix ||= '/'
end
# The host to connect to.
def host
@host ||= 'api.postmarkapp.com'
end

def http_open_timeout
@http_open_timeout ||= 5
end
# The path of the listener
def path_prefix
@path_prefix ||= '/'
end

def http_read_timeout
@http_read_timeout ||= 15
end
def http_open_timeout
@http_open_timeout ||= 5
end

def max_retries
@max_retries ||= 3
end
def http_read_timeout
@http_read_timeout ||= 15
end

def sleep_between_retries
@sleep_between_retries ||= 10
end
def max_retries
@max_retries ||= 3
end

def configure
yield self
end
def sleep_between_retries
@sleep_between_retries ||= 10
end

def send_through_postmark(message) #:nodoc:
@retries = 0
begin
HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
rescue DeliveryError => e
if @retries < max_retries
@retries += 1
retry
else
raise
end
end
end
def configure
yield self
end

def convert_message_to_options_hash(message)
options = Hash.new
headers = extract_headers_according_to_message_format(message)

options["From"] = message['from'].to_s if message.from
options["ReplyTo"] = Array[message.reply_to].flatten.join(", ") if message.reply_to
options["To"] = message['to'].to_s if message.to
options["Cc"] = message['cc'].to_s if message.cc
options["Bcc"] = Array[message.bcc].flatten.join(", ") if message.bcc
options["Subject"] = message.subject
options["Attachments"] = message.postmark_attachments
options["Tag"] = message.tag.to_s if message.tag
options["Headers"] = headers if headers.size > 0

options = options.delete_if{|k,v| v.nil?}

html = message.body_html
text = message.body_text

if message.multipart?
options["HtmlBody"] = html
options["TextBody"] = text
elsif html
options["HtmlBody"] = html
def send_through_postmark(message) #:nodoc:
@retries = 0
begin
HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
rescue DeliveryError => e
if @retries < max_retries
@retries += 1
retry
else
options["TextBody"] = text
raise
end

options
end
end

def delivery_stats
HttpClient.get("deliverystats")
end
def convert_message_to_options_hash(message)
options = Hash.new
headers = extract_headers_according_to_message_format(message)

protected
options["From"] = message['from'].to_s if message.from
options["ReplyTo"] = Array[message.reply_to].flatten.join(", ") if message.reply_to
options["To"] = message['to'].to_s if message.to
options["Cc"] = message['cc'].to_s if message.cc
options["Bcc"] = Array[message.bcc].flatten.join(", ") if message.bcc
options["Subject"] = message.subject
options["Attachments"] = message.postmark_attachments
options["Tag"] = message.tag.to_s if message.tag
options["Headers"] = headers if headers.size > 0

def extract_headers_according_to_message_format(message)
if defined?(TMail) && message.is_a?(TMail::Mail)
headers = extract_tmail_headers(message)
elsif defined?(Mail) && message.kind_of?(Mail::Message)
headers = extract_mail_headers(message)
else
raise "Can't convert message to a valid hash of API options. Unknown message format."
end
options = options.delete_if{|k,v| v.nil?}

html = message.body_html
text = message.body_text

if message.multipart?
options["HtmlBody"] = html
options["TextBody"] = text
elsif html
options["HtmlBody"] = html
else
options["TextBody"] = text
end

def extract_mail_headers(message)
headers = []
message.header.fields.each do |field|
key = field.name
value = field.value
next if bogus_headers.include? key.dup.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value }
end
headers
options
end

def delivery_stats
HttpClient.get("deliverystats")
end

protected

def extract_headers_according_to_message_format(message)
if defined?(TMail) && message.is_a?(TMail::Mail)
headers = extract_tmail_headers(message)
elsif defined?(Mail) && message.kind_of?(Mail::Message)
headers = extract_mail_headers(message)
else
raise "Can't convert message to a valid hash of API options. Unknown message format."
end
end

def extract_tmail_headers(message)
headers = []
message.each_header do |key, value|
next if bogus_headers.include? key.dup.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value.body }
end
headers
def extract_mail_headers(message)
headers = []
message.header.fields.each do |field|
key = field.name
value = field.value
next if bogus_headers.include? key.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value }
end
headers
end

def bogus_headers
%q[
return-path x-pm-rcpt
from reply-to
sender received
date content-type
cc bcc
subject tag
attachment
]
def extract_tmail_headers(message)
headers = []
message.each_header do |key, value|
next if bogus_headers.include? key.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value.body }
end
headers
end

def bogus_headers
%q[
return-path x-pm-rcpt
from reply-to
sender received
date content-type
cc bcc
subject tag
attachment
]
end



self.response_parser_class = nil

end

0 comments on commit 5d41761

Please sign in to comment.