Class: ApiMailer::Base

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::AssetPaths, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation
Defined in:
lib/api_mailer/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action_name, *args) ⇒ Base

Returns a new instance of Base.



44
45
46
47
48
# File 'lib/api_mailer/base.rb', line 44

def initialize(action_name, *args)
  self.action_name = action_name
  self.responses = []
  process(action_name, *args)
end

Instance Attribute Details

#action_nameObject

Returns the value of attribute action_name.



13
14
15
# File 'lib/api_mailer/base.rb', line 13

def action_name
  @action_name
end

#headersObject

Returns the value of attribute headers.



15
16
17
# File 'lib/api_mailer/base.rb', line 15

def headers
  @headers
end

#responsesObject

Returns the value of attribute responses.



14
15
16
# File 'lib/api_mailer/base.rb', line 14

def responses
  @responses
end

Class Method Details

.default(value = nil) ⇒ Object



39
40
41
42
# File 'lib/api_mailer/base.rb', line 39

def self.default(value = nil)
  self.default_params = default_params.merge(value).freeze if value
  default_params
end

.method_missing(method_name, *args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/api_mailer/base.rb', line 21

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/api_mailer/base.rb', line 33

def self.respond_to?(method, include_private = false)
  super || action_methods.include?(method.to_s)
end

.test_deliveriesObject



29
30
31
# File 'lib/api_mailer/base.rb', line 29

def self.test_deliveries
  @test_deliveries ||= []
end

Instance Method Details

#collect_responses(headers) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/api_mailer/base.rb', line 71

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

#deliverObject



63
64
65
66
67
68
69
# File 'lib/api_mailer/base.rb', line 63

def deliver
  if Rails.env.test?
    self.class.test_deliveries << build_message
  else
    deliver_message(build_message)
  end
end

#each_template(paths, name, &block) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/api_mailer/base.rb', line 94

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_partObject



107
108
109
# File 'lib/api_mailer/base.rb', line 107

def html_part
  Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/html"}.first).presence
end

#mail(headers = {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/api_mailer/base.rb', line 50

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



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/api_mailer/base.rb', line 111

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



90
91
92
# File 'lib/api_mailer/base.rb', line 90

def templates_path(headers)
  [headers.delete(:template_path) || self.class.name.underscore]
end

#text_partObject



103
104
105
# File 'lib/api_mailer/base.rb', line 103

def text_part
  Hashie::Mash.new(responses.select{|part| part[:content_type] == "text/plain"}.first).presence
end