mailgun_rails 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
-
2
1
  module Mailgun
3
2
  class Deliverer
4
3
 
@@ -16,23 +15,62 @@ module Mailgun
16
15
  self.settings[:api_key]
17
16
  end
18
17
 
19
- def deliver!(email)
20
- mailgun_message = {
21
- :from => email.from,
22
- :to => email.to,
23
- :subject => email.subject,
24
- :html => email.body.to_s
25
- }
18
+ def deliver!(rails_message)
19
+ mailgun_message = prepare_mailgun_message_for(rails_message)
20
+ remove_empty_values mailgun_message
21
+ mailgun_client.send_message mailgun_message
22
+ end
23
+
24
+ private
25
+
26
+ def prepare_mailgun_message_for(rails_message)
27
+ mailgun_message = build_basic_mailgun_message_from_rails_message rails_message
28
+
29
+ prepare_reply_to rails_message, mailgun_message if rails_message.reply_to
30
+ prepare_mailgun_variables rails_message, mailgun_message
31
+ prepare_mailgun_recipient_variables rails_message, mailgun_message
32
+
33
+ mailgun_message
34
+ end
35
+
36
+ def build_basic_mailgun_message_from_rails_message(rails_message)
37
+ {:from => rails_message.from, :to => rails_message.to, :subject => rails_message.subject,
38
+ :html => extract_html(rails_message), :text => extract_text(rails_message)}
39
+ end
40
+
41
+ def prepare_reply_to(rails_message, mailgun_message)
42
+ mailgun_message['h:Reply-To'] = rails_message.reply_to
43
+ end
44
+
45
+ # @see http://stackoverflow.com/questions/4868205/rails-mail-getting-the-body-as-plain-text
46
+ def extract_html(rails_message)
47
+ if rails_message.html_part
48
+ rails_message.html_part.body.decoded
49
+ else
50
+ rails_message.content_type == 'text/html' ? rails_message.body.decoded : nil
51
+ end
52
+ end
26
53
 
27
- mailgun_message['h:Reply-To'] = email.reply_to if email.reply_to
54
+ def extract_text(rails_message)
55
+ if rails_message.multipart?
56
+ rails_message.text_part ? rails_message.text_part.body.decoded : nil
57
+ else
58
+ rails_message.content_type == 'text/plain' ? rails_message.body.decoded : nil
59
+ end
60
+ end
28
61
 
29
- email.mailgun_variables.try(:each) do |name, value|
62
+ def prepare_mailgun_variables(rails_message, mailgun_message)
63
+ rails_message.mailgun_variables.try(:each) do |name, value|
30
64
  mailgun_message["v:#{name}"] = value
31
65
  end
66
+ end
32
67
 
33
- mailgun_message['recipient-variables'] = email.mailgun_recipient_variables.to_json if email.mailgun_recipient_variables
68
+ def prepare_mailgun_recipient_variables(rails_message, mailgun_message)
69
+ mailgun_message['recipient-variables'] = rails_message.mailgun_recipient_variables.to_json if rails_message.mailgun_recipient_variables
70
+ end
34
71
 
35
- mailgun_client.send_message mailgun_message
72
+ def remove_empty_values(mailgun_message)
73
+ mailgun_message.delete_if { |key, value| value.nil? }
36
74
  end
37
75
 
38
76
  def mailgun_client
@@ -1,3 +1,3 @@
1
1
  module Mailgun
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailgun_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -90,7 +90,7 @@ files:
90
90
  - MIT-LICENSE
91
91
  - Rakefile
92
92
  - README.rdoc
93
- homepage: http://jorgemanrubia.net
93
+ homepage: https://github.com/jorgemanrubia/mailgun_rails/
94
94
  licenses: []
95
95
  post_install_message:
96
96
  rdoc_options: []
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  segments:
106
106
  - 0
107
- hash: 3733122354519055210
107
+ hash: 2237586014440986052
108
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  none: false
110
110
  requirements:
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  segments:
115
115
  - 0
116
- hash: 3733122354519055210
116
+ hash: 2237586014440986052
117
117
  requirements: []
118
118
  rubyforge_project:
119
119
  rubygems_version: 1.8.23