class GovFakeNotify::SendEmailCommand

A service used when the sending of an email is requested

Attributes

attachment_store[R]
base_url[R]
id[R]
message_body[R]
params[R]
service[R]
store[R]

Public Class Methods

call(params, **kwargs) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 12
def self.call(params, **kwargs)
  # do nothing yet
  new(params, **kwargs).call
end
new(params, base_url:, service:, store: Store.instance, attachment_store: AttachmentStore.instance) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 17
def initialize(params, base_url:, service:, store: Store.instance, attachment_store: AttachmentStore.instance)
  @params = params.dup
  @store = store
  @attachment_store = attachment_store
  @base_url = base_url
  @service = service
end

Public Instance Methods

call() click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 25
def call
  template_data = store.transaction { store["template-#{params['template_id']}"] }
  send_email_from_template(template_data)
  persist_status(template_data)
  self
end
success?() click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 32
def success?
  true
end
to_json(*_args) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 36
def to_json(*_args) # rubocop:disable Metrics/MethodLength
  ::JSON.generate({
                    "id": id,
                    "reference": 'STRING',
                    "content": {
                      "body": message_body,
                      "from_number": 'govfakenotify@email.com'
                    },
                    "uri": "#{base_url}/v2/notifications/#{id}",
                    "template": {
                      "id": 'f33517ff-2a88-4f6e-b855-c550268ce08a',
                      "version": 1,
                      "uri": "#{base_url}/v2/template/ceb50d92-100d-4b8b-b559-14fa3b091cd"
                    }
                  })
end

Private Instance Methods

format_line_html(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 137
def format_line_html(line)
  replaced = line.gsub(/\(\(([^)]*)\)\)/) do
    post_process_value_html params['personalisation'][Regexp.last_match[1]]
  end
  wrap_line_html(replaced)
end
format_line_text(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 144
def format_line_text(line)
  replaced = line.gsub(/\(\(([^)]*)\)\)/) do
    post_process_value_text params['personalisation'][Regexp.last_match[1]]
  end
  wrap_line_text(replaced)
end
mail_message_html(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 105
def mail_message_html(template_data)
  layout = Tilt.new(File.absolute_path('../../views/layouts/govuk.html.erb', __dir__))
  layout.render do
    template_content_html(template_data)
  end
end
mail_message_text(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 112
def mail_message_text(template_data)
  layout = Tilt.new(File.absolute_path('../../views/layouts/govuk.text.erb', __dir__))
  layout.render do
    template_content_text(template_data)
  end
end
persist_status(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 80
def persist_status(template_data) # rubocop:disable Metrics/MethodLength
  store.transaction do
    store["message-#{id}"] = { id: id,
                               email_address: params['email_address'],
                               type: 'email',
                               status: 'delivered',
                               template: {
                                 Version: 1,
                                 id: 'f33517ff-2a88-4f6e-b855-c550268ce08a',
                                 uri: "#{base_url}/v2/template/ceb50d92-100d-4b8b-b559-14fa3b091cd"
                               },
                               body: message_body,
                               subject: template_data['subject'],
                               created_at: Time.now.to_s }
  end
end
post_process_value_html(value) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 151
def post_process_value_html(value)
  return value unless value.is_a?(Hash) && value.keys.include?('file')

  render_file_html(value)
end
post_process_value_text(value) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 157
def post_process_value_text(value)
  return value unless value.is_a?(Hash) && value.keys.include?('file')

  render_file_text(value)
end
pre_process_files() click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 97
def pre_process_files
  params['personalisation'].each_pair do |key, value|
    next unless value.is_a?(Hash) && value.keys.include?('file')

    params['personalisation'][key] = attachment_store.store(value)
  end
end
render_file_html(file_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 193
def render_file_html(file_data)
  Tilt.new(File.absolute_path('../../views/govuk/file.html.erb', __dir__)).render(nil, file_data: file_data,
                                                                                       base_url: base_url)
end
render_file_text(file_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 198
def render_file_text(file_data)
  Tilt.new(File.absolute_path('../../views/govuk/file.text.erb', __dir__)).render(nil, file_data: file_data,
                                                                                       base_url: base_url)
end
render_horizontal_line_html() click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 177
def render_horizontal_line_html
  Tilt.new(File.absolute_path('../../views/govuk/horizontal_line.html.erb', __dir__)).render
end
render_horizontal_line_text() click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 181
def render_horizontal_line_text
  Tilt.new(File.absolute_path('../../views/govuk/horizontal_line.text.erb', __dir__)).render
end
render_paragraph_html(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 185
def render_paragraph_html(line)
  Tilt.new(File.absolute_path('../../views/govuk/paragraph.html.erb', __dir__)).render(nil, content: line)
end
render_paragraph_text(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 189
def render_paragraph_text(line)
  Tilt.new(File.absolute_path('../../views/govuk/paragraph.text.erb', __dir__)).render(nil, content: line)
end
send_email_from_template(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 57
def send_email_from_template(template_data) # rubocop:disable Metrics/MethodLength
  pre_process_files
  our_params = params
  our_html_body = mail_message_html(template_data)
  our_text_body = mail_message_text(template_data)
  our_service = service
  @message_body = our_html_body
  mail = Mail.new do
    from    our_service['service_email']
    to      our_params['email_address']
    subject template_data['subject']
    html_part do
      content_type 'text/html; charset=UTF-8'
      body our_html_body
    end
    text_part do
      body our_text_body
    end
  end
  mail.deliver
  @id = SecureRandom.uuid
end
template_content_html(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 119
def template_content_html(template_data)
  template = template_data['message']
  buffer = ''.dup
  template.each_line do |line|
    buffer << format_line_html(line)
  end
  buffer
end
template_content_text(template_data) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 128
def template_content_text(template_data)
  template = template_data['message']
  buffer = ''.dup
  template.each_line do |line|
    buffer << format_line_text(line)
  end
  buffer
end
wrap_line_html(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 163
def wrap_line_html(line)
  case line
  when /^---/ then render_horizontal_line_html
  else render_paragraph_html(line)
  end
end
wrap_line_text(line) click to toggle source
# File lib/gov_fake_notify/commands/send_email_command.rb, line 170
def wrap_line_text(line)
  case line
  when /^---/ then render_horizontal_line_text
  else render_paragraph_text(line)
  end
end