Skip to content

Commit

Permalink
Fixing load problem on Rails 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
isabanin committed Sep 16, 2010
1 parent 68dab9a commit 04bbd97
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 39 deletions.
6 changes: 3 additions & 3 deletions lib/postmark.rb
Expand Up @@ -4,15 +4,15 @@ def require_local(file)

require 'net/http'
require 'net/https'
# It's important to load mail before extensions.
require 'mail'

require_local 'bounce'
require_local 'json'
require_local 'http_client'
require_local 'message_extensions/shared'
require_local 'message_extensions/tmail'
require_local 'message_extensions/mail'
require 'mail/postmark'
require_local 'handlers/mail'
require_local 'attachments_fix_for_mail'

module Postmark

Expand Down
43 changes: 43 additions & 0 deletions lib/postmark/attachments_fix_for_mail.rb
@@ -0,0 +1,43 @@
module Postmark

#
# This fix is needed solely to support neat Rails 3 syntax to create emails.
# That one:
#
# def invitation
# mail(
# :to => "someone@example.com",
# :postmark_attachments => [File.open(...)]
# )
# end
#
# That code will automatically put the file to Mail::OptionalField of the Mail::Message object
# and will try to encode it before delivery. You are not supposed to store files in
# such fields, so Mail will raise an exception. That's why before we actually perform a
# delivery we have to remove the files from OptionalField to a regular @_attachments variable.
#
module AttachmentsFixForMail

def self.included(base)
base.class_eval do
alias_method_chain :deliver, :postmark_hook
end
end

def deliver_with_postmark_hook
remove_postmark_attachments_from_standard_fields
deliver_without_postmark_hook
end

private

def remove_postmark_attachments_from_standard_fields
field = self['POSTMARK-ATTACHMENTS']
return if field.nil?
self.postmark_attachments = field.value
self['POSTMARK-ATTACHMENTS'] = nil
end

end

end
8 changes: 5 additions & 3 deletions lib/mail/postmark.rb → lib/postmark/handlers/mail.rb
@@ -1,14 +1,16 @@
module Mail
class Postmark
def initialize(values)
self.settings = {:api_key => nil}.merge(values)
end

attr_accessor :settings

def initialize(values)
self.settings = { :api_key => nil }.merge(values)
end

def deliver!(mail)
::Postmark.api_key = settings[:api_key]
::Postmark.send_through_postmark(mail)
end

end
end
32 changes: 0 additions & 32 deletions lib/postmark/message_extensions/mail.rb
Expand Up @@ -16,37 +16,5 @@ def body_text
end
end

def deliver_with_postmark_hook
remove_postmark_attachments_from_standard_fields
deliver_without_postmark_hook
end

alias_method_chain :deliver, :postmark_hook

private

#
# This is needed solely to support neat Rails 3 syntax to create emails.
# That one:
#
# def invitation
# mail(
# :to => "someone@example.com",
# :postmark_attachments => [File.open(...)]
# )
# end
#
# That code will automatically put the file to Mail::OptionalField of the Mail::Message object
# and will try to encode it before delivery. You are not supposed to store files in
# such fields, so Mail will raise an exception. That's why before we actually perform a
# delivery we have to remove the files from OptionalField to a regular @_attachments variable.
#
def remove_postmark_attachments_from_standard_fields
field = self['POSTMARK-ATTACHMENTS']
return if field.nil?
self.postmark_attachments = field.value
self['POSTMARK-ATTACHMENTS'] = nil
end

end
end
3 changes: 2 additions & 1 deletion postmark.gemspec
Expand Up @@ -32,9 +32,10 @@ Gem::Specification.new do |s|
"features/step_definitions/postmark_steps.rb",
"features/support/env.rb",
"init.rb",
"lib/mail/postmark.rb",
"lib/postmark.rb",
"lib/postmark/attachments_fix_for_mail.rb",
"lib/postmark/bounce.rb",
"lib/postmark/handlers/mail.rb",
"lib/postmark/http_client.rb",
"lib/postmark/json.rb",
"lib/postmark/message_extensions/mail.rb",
Expand Down

0 comments on commit 04bbd97

Please sign in to comment.