Class: ApiMailer::Base
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- ApiMailer::Base
- Includes:
- AbstractController::AssetPaths, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionView::Layouts
- Defined in:
- lib/api_mailer/base.rb
Instance Attribute Summary collapse
-
#action_name ⇒ Object
Returns the value of attribute action_name.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#responses ⇒ Object
Returns the value of attribute responses.
Class Method Summary collapse
- .default(value = nil) ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .respond_to?(method, include_private = false) ⇒ Boolean
- .test_deliveries ⇒ Object
Instance Method Summary collapse
- #collect_responses(headers) ⇒ Object
- #deliver ⇒ Object
- #each_template(paths, name, &block) ⇒ Object
- #html_part ⇒ Object
-
#initialize(action_name, *args) ⇒ Base
constructor
A new instance of Base.
- #mail(headers = {}, &block) ⇒ Object
- #process(method_name, *args) ⇒ Object
- #templates_path(headers) ⇒ Object
- #text_part ⇒ Object
Constructor Details
#initialize(action_name, *args) ⇒ Base
Returns a new instance of Base.
46 47 48 49 50 |
# File 'lib/api_mailer/base.rb', line 46 def initialize(action_name, *args) self.action_name = action_name self.responses = [] process(action_name, *args) end |
Instance Attribute Details
#action_name ⇒ Object
Returns the value of attribute action_name.
15 16 17 |
# File 'lib/api_mailer/base.rb', line 15 def action_name @action_name end |
#headers ⇒ Object
Returns the value of attribute headers.
17 18 19 |
# File 'lib/api_mailer/base.rb', line 17 def headers @headers end |
#responses ⇒ Object
Returns the value of attribute responses.
16 17 18 |
# File 'lib/api_mailer/base.rb', line 16 def responses @responses end |
Class Method Details
.default(value = nil) ⇒ Object
41 42 43 44 |
# File 'lib/api_mailer/base.rb', line 41 def self.default(value = nil) self.default_params = default_params.merge(value).freeze if value default_params end |
.method_missing(method_name, *args) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/api_mailer/base.rb', line 23 def self.method_missing(method_name, *args) if respond_to?(method_name) new(method_name, *args) else super end end |
.respond_to?(method, include_private = false) ⇒ Boolean
35 36 37 |
# File 'lib/api_mailer/base.rb', line 35 def self.respond_to?(method, include_private = false) super || action_methods.include?(method.to_s) end |
.test_deliveries ⇒ Object
31 32 33 |
# File 'lib/api_mailer/base.rb', line 31 def self.test_deliveries @test_deliveries ||= [] end |
Instance Method Details
#collect_responses(headers) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/api_mailer/base.rb', line 73 def collect_responses(headers) if block_given? collector = ActionMailer::Collector.new(lookup_context) { render(action_name) } yield(collector) self.responses = collector.responses else templates_name = headers.delete(:template_name) || action_name each_template(templates_path(headers), templates_name) do |template| self.formats = template.formats self.responses << { body: render(template: template), content_type: (template.respond_to?(:type) ? template.type : template.mime_type).to_s } end end end |
#deliver ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/api_mailer/base.rb', line 65 def deliver if Rails.env.test? self.class.test_deliveries << else () end end |
#each_template(paths, name, &block) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/api_mailer/base.rb', line 96 def each_template(paths, name, &block) templates = lookup_context.find_all(name, paths) if templates.empty? raise ActionView::MissingTemplate.new(paths, name, paths, false, 'mailer') else templates.uniq { |t| t.formats }.each(&block) end end |
#html_part ⇒ Object
109 110 111 |
# File 'lib/api_mailer/base.rb', line 109 def html_part Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/html"}.first).presence end |
#mail(headers = {}, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/api_mailer/base.rb', line 52 def mail(headers={}, &block) # Call all the procs (if any) default_values = {} self.class.default.each do |k,v| default_values[k] = v.is_a?(Proc) ? instance_eval(&v) : v end # Handle defaults self.headers = ActiveSupport::HashWithIndifferentAccess.new(default_values.merge(headers)) || {} collect_responses(headers, &block) end |
#process(method_name, *args) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/api_mailer/base.rb', line 113 def process(method_name, *args) payload = { :mailer => self.class.name, :action => method_name } ActiveSupport::Notifications.instrument("process.api_mailer", payload) do lookup_context.skip_default_locale! super end end |
#templates_path(headers) ⇒ Object
92 93 94 |
# File 'lib/api_mailer/base.rb', line 92 def templates_path(headers) [headers.delete(:template_path) || self.class.name.underscore] end |
#text_part ⇒ Object
105 106 107 |
# File 'lib/api_mailer/base.rb', line 105 def text_part Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/plain"}.first).presence end |