class Textris::Delivery::Mail

Public Instance Methods

deliver(to) click to toggle source
# File lib/textris/delivery/mail.rb, line 10
def deliver(to)
  template_vars = { :to_phone => to }

  from    = apply_template from_template,    template_vars
  to      = apply_template to_template,      template_vars
  subject = apply_template subject_template, template_vars
  body    = apply_template body_template,    template_vars

  ::Textris::Delivery::Mail::Mailer.notify(
    from, to, subject, body).deliver
end

Private Instance Methods

apply_template(template, variables) click to toggle source
# File lib/textris/delivery/mail.rb, line 52
def apply_template(template, variables)
  template.gsub(/\%\{[a-z_:]+\}/) do |match|
    directive = match.gsub(/[%{}]/, '')
    key       = directive.split(':').first
    modifiers = directive.split(':')[1] || ''

    content = get_template_interpolation(key, variables)
    content = apply_template_modifiers(content, modifiers.chars)
    content = 'unknown' unless content.present?

    content
  end
end
apply_template_modifiers(content, modifiers) click to toggle source
# File lib/textris/delivery/mail.rb, line 88
def apply_template_modifiers(content, modifiers)
  modifiers.each do |modifier|
    case modifier
    when 'd'
      content = content.underscore.dasherize
    when 'h'
      content = content.humanize.gsub(/[-_]/, ' ')
    when 'p'
      content = Phony.format(content) rescue content
    end
  end

  content
end
body_template() click to toggle source
# File lib/textris/delivery/mail.rb, line 47
def body_template
  Rails.application.config.try(:textris_mail_body_template) ||
    "%{content}"
end
from_format() click to toggle source
# File lib/textris/delivery/mail.rb, line 29
def from_format
  if message.twilio_messaging_service_sid
    '%{twilio_messaging_service_sid}'
  else
    '%{from_name:d}-%{from_phone}'
  end
end
from_template() click to toggle source
# File lib/textris/delivery/mail.rb, line 24
def from_template
  Rails.application.config.try(:textris_mail_from_template) ||
    "#{from_format}@%{env:d}.%{app:d}.com"
end
get_rails_variable(var) click to toggle source
# File lib/textris/delivery/mail.rb, line 79
def get_rails_variable(var)
  case var
  when 'app'
    Rails.application.class.parent_name
  when 'env'
    Rails.env
  end
end
get_template_interpolation(key, variables) click to toggle source
# File lib/textris/delivery/mail.rb, line 66
def get_template_interpolation(key, variables)
  case key
  when 'app', 'env'
    get_rails_variable(key)
  when 'texter', 'action', 'from_name', 'from_phone', 'content', 'twilio_messaging_service_sid'
    message.send(key)
  when 'media_urls'
    message.media_urls.join(', ')
  else
    variables[key.to_sym]
  end.to_s.strip
end
subject_template() click to toggle source
# File lib/textris/delivery/mail.rb, line 42
def subject_template
  Rails.application.config.try(:textris_mail_subject_template) ||
    "%{texter:dh} texter: %{action:h}"
end
to_template() click to toggle source
# File lib/textris/delivery/mail.rb, line 37
def to_template
  Rails.application.config.try(:textris_mail_to_template) ||
    "%{app:d}-%{env:d}-%{to_phone}-texts@mailinator.com"
end