api_mailer 0.0.3 → 0.0.4

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.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ script: bundle exec rspec spec
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api_mailer (0.0.1)
4
+ api_mailer (0.0.3)
5
5
  actionpack (>= 3.0)
6
6
  activesupport (>= 3.0)
7
+ hashie
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -31,6 +32,7 @@ GEM
31
32
  debugger-ruby_core_source (1.2.3)
32
33
  diff-lcs (1.2.4)
33
34
  erubis (2.7.0)
35
+ hashie (2.0.5)
34
36
  i18n (0.6.4)
35
37
  minitest (4.7.5)
36
38
  multi_json (1.7.7)
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # API Mailer
2
+
3
+ ## The whys
4
+
5
+ * SMTP is silly, let's use flexibly APIs instead
6
+ * It doesn't use the huge, bloated Mail gem
7
+ * It doesn't use the huge, bloated SMTP gem
8
+ * Its pluggable
9
+
10
+ ## Usage
11
+
12
+ ```ruby
13
+ # app/mailers/mailing_base.rb
14
+ class MailingBase < ApiMailer::Base
15
+ def build_message
16
+ # This method must be defined, it builds the package for deliver
17
+ # here is an example json object
18
+ headers.extract(:to, :from, :subject).merge(html: responses.html_part.body.to_s).to_json
19
+ end
20
+
21
+ def deliver_message(message)
22
+ #send the message somewhere using POST or whatever
23
+ end
24
+ end
25
+
26
+ # app/mailers/my_mailer.rb
27
+ class MyMailer < MailingBase
28
+ def cool_message_bro(user)
29
+ @user = user
30
+ mail(to: "email_me@example.com",
31
+ from: "sender@example.com",
32
+ subject: "Cool Message for you, Bro",
33
+ other_header: "value")
34
+ end
35
+ end
36
+
37
+ # app/views/my_mailer/cool_message_bro.html.erb
38
+ Cool message, <%= @user.name %>!
39
+
40
+ # sending mail
41
+ MyMailer.cool_message_bro(user).deliver
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ _*Coming Soon!*_
@@ -47,7 +47,7 @@ module ApiMailer
47
47
  process(action_name, *args)
48
48
  end
49
49
 
50
- def mail(headers={})
50
+ def mail(headers={}, &block)
51
51
  # Call all the procs (if any)
52
52
  default_values = {}
53
53
  self.class.default.each do |k,v|
@@ -55,9 +55,9 @@ module ApiMailer
55
55
  end
56
56
 
57
57
  # Handle defaults
58
- self.headers = ActiveSupport::HashWithIndifferentAccess.new(headers.reverse_merge(default_values)) || {}
58
+ self.headers = ActiveSupport::HashWithIndifferentAccess.new(default_values.merge(headers)) || {}
59
59
 
60
- collect_responses(headers)
60
+ collect_responses(headers, &block)
61
61
  end
62
62
 
63
63
  def deliver
@@ -69,15 +69,21 @@ module ApiMailer
69
69
  end
70
70
 
71
71
  def collect_responses(headers)
72
- templates_name = headers.delete(:template_name) || action_name
72
+ if block_given?
73
+ collector = ActionMailer::Collector.new(lookup_context) { render(action_name) }
74
+ yield(collector)
75
+ self.responses = collector.responses
76
+ else
77
+ templates_name = headers.delete(:template_name) || action_name
73
78
 
74
- each_template(templates_path(headers), templates_name) do |template|
75
- self.formats = template.formats
79
+ each_template(templates_path(headers), templates_name) do |template|
80
+ self.formats = template.formats
76
81
 
77
- self.responses << {
78
- body: render(template: template),
79
- content_type: (template.respond_to?(:type) ? template.type : template.mime_type).to_s
80
- }
82
+ self.responses << {
83
+ body: render(template: template),
84
+ content_type: (template.respond_to?(:type) ? template.type : template.mime_type).to_s
85
+ }
86
+ end
81
87
  end
82
88
  end
83
89
 
@@ -94,11 +100,12 @@ module ApiMailer
94
100
  end
95
101
  end
96
102
 
103
+ def text_part
104
+ Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/plain"}.first).presence
105
+ end
97
106
 
98
- [:html_part, :text_part].each do |part|
99
- define_method part do
100
- Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/html"}.first).presence
101
- end
107
+ def html_part
108
+ Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/html"}.first).presence
102
109
  end
103
110
 
104
111
  def process(method_name, *args)
@@ -1,3 +1,3 @@
1
1
  module ApiMailer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,6 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class MyMailer < ApiMailer::Base
4
+ def mail_me(options)
5
+ mail(options)
6
+ end
7
+ end
8
+
3
9
  module ApiMailer
4
10
  describe Base do
11
+ it "should create an instance and call instance method when the calling the class method" do
12
+ MyMailer.any_instance.should_receive :mail_me
13
+ MyMailer.mail_me
14
+ end
15
+
16
+ it "should call deliver_message and build_message when you deliver a message" do
17
+ Rails.env.stub(:test?).and_return(false)
18
+ MyMailer.any_instance.should_receive :collect_responses
19
+ message = MyMailer.mail_me(to: "billy@example.com")
20
+ message.should_receive :build_message
21
+ message.should_receive :deliver_message
22
+ message.deliver
23
+ end
5
24
  end
6
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-28 00:00:00.000000000 Z
12
+ date: 2013-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -100,8 +100,10 @@ extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
102
  - .gitignore
103
+ - .travis.yml
103
104
  - Gemfile
104
105
  - Gemfile.lock
106
+ - README.md
105
107
  - Rakefile
106
108
  - api_mailer.gemspec
107
109
  - lib/api_mailer.rb