api_mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api_mailer.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_mailer (0.0.1)
5
+ actionpack (>= 3.0)
6
+ activesupport (>= 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (4.0.0)
12
+ activesupport (= 4.0.0)
13
+ builder (~> 3.1.0)
14
+ erubis (~> 2.7.0)
15
+ rack (~> 1.5.2)
16
+ rack-test (~> 0.6.2)
17
+ activesupport (4.0.0)
18
+ i18n (~> 0.6, >= 0.6.4)
19
+ minitest (~> 4.2)
20
+ multi_json (~> 1.3)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 0.3.37)
23
+ atomic (1.1.10)
24
+ builder (3.1.4)
25
+ columnize (0.3.6)
26
+ debugger (1.6.2)
27
+ columnize (>= 0.3.1)
28
+ debugger-linecache (~> 1.2.0)
29
+ debugger-ruby_core_source (~> 1.2.3)
30
+ debugger-linecache (1.2.0)
31
+ debugger-ruby_core_source (1.2.3)
32
+ diff-lcs (1.2.4)
33
+ erubis (2.7.0)
34
+ i18n (0.6.4)
35
+ minitest (4.7.5)
36
+ multi_json (1.7.7)
37
+ rack (1.5.2)
38
+ rack-test (0.6.2)
39
+ rack (>= 1.0)
40
+ rspec (2.14.1)
41
+ rspec-core (~> 2.14.0)
42
+ rspec-expectations (~> 2.14.0)
43
+ rspec-mocks (~> 2.14.0)
44
+ rspec-core (2.14.6)
45
+ rspec-expectations (2.14.3)
46
+ diff-lcs (>= 1.1.3, < 2.0)
47
+ rspec-mocks (2.14.4)
48
+ thread_safe (0.1.2)
49
+ atomic
50
+ tzinfo (0.3.38)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ api_mailer!
57
+ debugger
58
+ rspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../lib/api_mailer/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Carl Allen"]
5
+ gem.email = ["github@allenofmn.com"]
6
+ gem.description = %q{A simple replication of ActionMailer for API based mailing that doesn't require the mail gem}
7
+ gem.summary = %q{A simple replication of ActionMailer for API based mailing that doesn't require the mail gem}
8
+ gem.homepage = "http://github.com/sportngin/api_mailer"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(spec)/})
13
+ gem.name = "api_mailer"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = ApiMailer::VERSION
16
+
17
+ gem.add_development_dependency "rspec"
18
+ gem.add_development_dependency "debugger"
19
+ gem.add_dependency "actionpack", ">= 3.0"
20
+ gem.add_dependency "activesupport", ">= 3.0"
21
+ end
@@ -0,0 +1,112 @@
1
+ require 'abstract_controller'
2
+ module ApiMailer
3
+ class Base < AbstractController::Base
4
+ abstract!
5
+
6
+ include AbstractController::Rendering
7
+
8
+ attr_accessor :action_name
9
+ attr_accessor :responses
10
+ attr_accessor :headers
11
+
12
+ private_class_method :new
13
+
14
+ self.view_paths = ["app/views", "app/views/mailers"]
15
+
16
+ def self.method_missing(method_name, *args)
17
+ if respond_to?(method_name)
18
+ new(method_name, *args)
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def self.test_deliveries
25
+ @test_deliveries ||= []
26
+ end
27
+
28
+ def self.respond_to?(method, include_private = false)
29
+ super || action_methods.include?(method.to_s)
30
+ end
31
+
32
+ class_attribute :default_params
33
+ self.default_params = {}.freeze
34
+ def self.default(value = nil)
35
+ self.default_params = default_params.merge(value).freeze if value
36
+ default_params
37
+ end
38
+
39
+ def initialize(action_name, *args)
40
+ self.action_name = action_name
41
+ self.responses = []
42
+ process(action_name, *args)
43
+ end
44
+
45
+ def mail(headers={})
46
+ # Call all the procs (if any)
47
+ default_values = {}
48
+ self.class.default.each do |k,v|
49
+ default_values[k] = v.is_a?(Proc) ? instance_eval(&v) : v
50
+ end
51
+
52
+ # Handle defaults
53
+ self.headers = ActiveSupport::HashWithIndifferentAccess.new(headers.reverse_merge(default_values))
54
+
55
+ collect_responses(headers)
56
+ end
57
+
58
+ def deliver
59
+ if Rails.env.test?
60
+ self.class.test_deliveries << build_message
61
+ else
62
+ deliver_message(build_message)
63
+ end
64
+ end
65
+
66
+ def collect_responses(headers)
67
+ templates_name = headers.delete(:template_name) || action_name
68
+
69
+ each_template(templates_path(headers), templates_name) do |template|
70
+ self.formats = template.formats
71
+
72
+ self.responses << {
73
+ body: render(template: template),
74
+ content_type: template.type.to_s
75
+ }
76
+ end
77
+ end
78
+
79
+ def templates_path(headers)
80
+ [headers.delete(:template_path) || self.class.name.underscore]
81
+ end
82
+
83
+ def each_template(paths, name, &block)
84
+ templates = lookup_context.find_all(name, paths)
85
+ if templates.empty?
86
+ raise ActionView::MissingTemplate.new(paths, name, paths, false, 'mailer')
87
+ else
88
+ templates.uniq { |t| t.formats }.each(&block)
89
+ end
90
+ end
91
+
92
+
93
+ [:html_part, :text_part].each do |part|
94
+ define_method part do
95
+ Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/html"}.first).presence
96
+ end
97
+ end
98
+
99
+ def process(method_name, *args)
100
+ payload = {
101
+ :mailer => self.class.name,
102
+ :action => method_name
103
+ }
104
+
105
+ ActiveSupport::Notifications.instrument("process.api_mailer", payload) do
106
+ lookup_context.skip_default_locale!
107
+
108
+ super
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,27 @@
1
+ ApiMailer::Configuration = Object.new
2
+
3
+ ApiMailer::Configuration.instance_eval do
4
+ def filepath
5
+ @filepath ||= Rails.root.join("config/api_mailer.yml")
6
+ end
7
+ def configurations
8
+ @configurations ||= load_config
9
+ end
10
+
11
+ def load_config
12
+ if File.exists?(filepath)
13
+ ActiveSupport::HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(filepath)).result)[Rails.env.to_s])
14
+ else
15
+ raise Exception.new("File not found: config/api_mailer.yml")
16
+ end
17
+ end
18
+
19
+ def get(name)
20
+ configurations[name.to_s]
21
+ end
22
+
23
+ def keys
24
+ configurations.keys
25
+ end
26
+ alias :[] :get
27
+ end
@@ -0,0 +1,3 @@
1
+ module ApiMailer
2
+ VERSION = "0.0.1"
3
+ end
data/lib/api_mailer.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "api_mailer/base"
2
+ require "api_mailer/configuration"
3
+ require 'abstract_controller'
4
+
5
+ module ApiMailer
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ module ApiMailer
4
+ describe Base do
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ module ApiMailer
4
+ describe Configuration do
5
+ it "should load configuaration" do
6
+ ApiMailer::Configuration[:setting_1].should == "Hello World!"
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,2 @@
1
+ test:
2
+ setting_1: "Hello World!"
@@ -0,0 +1,25 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'active_support/hash_with_indifferent_access'
9
+ require "api_mailer"
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+
21
+ config.before(:each) do
22
+ Rails = double(root: Pathname.new(__FILE__).dirname,
23
+ env: double(to_s: "test", test?: true))
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Carl Allen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: debugger
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: actionpack
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description: A simple replication of ActionMailer for API based mailing that doesn't
79
+ require the mail gem
80
+ email:
81
+ - github@allenofmn.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - Rakefile
90
+ - api_mailer.gemspec
91
+ - lib/api_mailer.rb
92
+ - lib/api_mailer/base.rb
93
+ - lib/api_mailer/configuration.rb
94
+ - lib/api_mailer/version.rb
95
+ - spec/api_mailer/base_spec.rb
96
+ - spec/api_mailer/configuration_spec.rb
97
+ - spec/config/api_mailer.yml
98
+ - spec/spec_helper.rb
99
+ homepage: http://github.com/sportngin/api_mailer
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A simple replication of ActionMailer for API based mailing that doesn't require
123
+ the mail gem
124
+ test_files:
125
+ - spec/api_mailer/base_spec.rb
126
+ - spec/api_mailer/configuration_spec.rb
127
+ - spec/config/api_mailer.yml
128
+ - spec/spec_helper.rb