Skip to content

Commit

Permalink
Apply Standard Ruby autofixes
Browse files Browse the repository at this point in the history
  • Loading branch information
standard-ruby-action[bot] committed Oct 28, 2024
1 parent b91675b commit c99cac0
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 131 deletions.
6 changes: 3 additions & 3 deletions lib/mail-ses.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

require 'aws-sdk-sesv2'
require 'mail'
require 'mail/ses'
require "aws-sdk-sesv2"
require "mail"
require "mail/ses"
12 changes: 6 additions & 6 deletions lib/mail/ses.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'mail/ses/version'
require 'mail/ses/message_validator'
require 'mail/ses/options_builder'
require "mail/ses/version"
require "mail/ses/message_validator"
require "mail/ses/options_builder"

module Mail
# Mail delivery method handler for AWS SES
Expand All @@ -21,7 +21,7 @@ def initialize(options = {})
@mail_options = options.delete(:mail_options) || {}

@error_handler = options.delete(:error_handler)
raise ArgumentError.new(':error_handler must be a Proc') if @error_handler && !@error_handler.is_a?(Proc)
raise ArgumentError.new(":error_handler must be a Proc") if @error_handler && !@error_handler.is_a?(Proc)

@settings = {
return_response: options.delete(:return_response),
Expand All @@ -46,9 +46,9 @@ def deliver!(message, options = {})

begin
response = client.send_email(send_options)
message.message_id = "#{response.to_h[:message_id]}@#{settings[:message_id_domain] || 'email.amazonses.com'}"
message.message_id = "#{response.to_h[:message_id]}@#{settings[:message_id_domain] || "email.amazonses.com"}"
settings[:return_response] ? response : self
rescue StandardError => e
rescue => e
handle_error(e, send_options)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mail/ses/message_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def validate
def validate_class
return if @message.is_a?(Mail::Message)

raise ArgumentError.new('mail must be an instance of Mail::Message class')
raise ArgumentError.new("mail must be an instance of Mail::Message class")
end

def validate_delivery_params
Expand All @@ -31,7 +31,7 @@ def validate_delivery_params
def validate_attachments
return unless @message.has_attachments? && @message.text_part.nil? && @message.html_part.nil?

raise ArgumentError.new('Attachment provided without message body')
raise ArgumentError.new("Attachment provided without message body")
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/mail/ses/options_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class SES
# Builds options for Aws::SESV2::Client#send_email
class OptionsBuilder
SES_FIELDS = %i[ from_email_address
from_email_address_identity_arn
reply_to_addresses
feedback_forwarding_email_address
feedback_forwarding_email_address_identity_arn
email_tags
configuration_set_name ].freeze
from_email_address_identity_arn
reply_to_addresses
feedback_forwarding_email_address
feedback_forwarding_email_address_identity_arn
email_tags
configuration_set_name ].freeze

# message - The Mail::Message object to be sent.
# options - The Hash options which override any defaults
Expand Down Expand Up @@ -41,7 +41,7 @@ def message_options
cc_addresses: extract_value(:cc) || [],
bcc_addresses: extract_value(:bcc) || []
},
content: { raw: { data: @message.to_s } }
content: {raw: {data: @message.to_s}}
}.compact
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mail/ses/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Mail
class SES
VERSION = '1.1.0'
VERSION = "1.1.0"
end
end
94 changes: 47 additions & 47 deletions spec/mail_ses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,144 +1,144 @@
# frozen_string_literal: true

require 'spec_helper'
require "spec_helper"

RSpec.describe Mail::SES do
let(:ses_options) { { stub_responses: true } }
let(:ses_options) { {stub_responses: true} }

let(:ses) do
described_class.new(ses_options)
end

let(:mail) do
Mail.new do
from 'from@abc.com'
from "from@abc.com"
to %w[to1@def.com to2@xyz.com]
cc %w[cc1@xyz.com cc2@def.com]
bcc %w[bcc1@abc.com bcc2@def.com]
body 'This is the body'
body "This is the body"
end
end

describe '::VERSION' do
describe "::VERSION" do
it { expect(described_class::VERSION).to match(/\A\d+\.\d+\.\d+/) }
end

describe '#settings' do
describe "#settings" do
it do
expect(ses).to respond_to(:settings, :settings=)
expect(ses.settings).to eq(return_response: nil, message_id_domain: nil)
end
end

describe '#initialize' do
it 'accepts valid :error_handler' do
describe "#initialize" do
it "accepts valid :error_handler" do
expect(described_class.new(ses_options)).to be_a(Mail::SES)
end

it 'accepts valid :error_handler' do
it "accepts valid :error_handler" do
expect(described_class.new(ses_options.merge(error_handler: ->(a, b) {}))).to be_a(Mail::SES)
end

it 'rejects invalid :error_handler' do
expect { described_class.new(ses_options.merge(error_handler: 'foobar')) }.to raise_error(ArgumentError, ':error_handler must be a Proc')
it "rejects invalid :error_handler" do
expect { described_class.new(ses_options.merge(error_handler: "foobar")) }.to raise_error(ArgumentError, ":error_handler must be a Proc")
end

it 'handles :use_iam_profile option' do
allow_any_instance_of(Aws::InstanceProfileCredentials).to receive(:get_credentials).and_return('{}')
it "handles :use_iam_profile option" do
allow_any_instance_of(Aws::InstanceProfileCredentials).to receive(:get_credentials).and_return("{}")
ses = described_class.new(ses_options.merge(use_iam_profile: true))
expect(ses.client.config.credentials).to be_a(Aws::InstanceProfileCredentials)
end

it 'passes through options to AWS' do
it "passes through options to AWS" do
ses = described_class.new(ses_options.merge(log_level: :debug, retry_limit: 5))
expect(ses.client.config.log_level).to eq :debug
expect(ses.client.config.retry_limit).to eq 5
end
end

describe '#deliver!' do
it 'validates that mail is a Mail' do
expect { ses.deliver!(foo: :bar) }.to raise_error(ArgumentError, 'mail must be an instance of Mail::Message class')
describe "#deliver!" do
it "validates that mail is a Mail" do
expect { ses.deliver!(foo: :bar) }.to raise_error(ArgumentError, "mail must be an instance of Mail::Message class")
end

it 'validates integrity of Mail' do
expect { ses.deliver!(Mail.new) }.to raise_error(ArgumentError, 'SMTP From address may not be blank: nil')
expect { ses.deliver!(Mail.new { from 'foo@bar.com' }) }.to raise_error(ArgumentError, 'SMTP To address may not be blank: []')
it "validates integrity of Mail" do
expect { ses.deliver!(Mail.new) }.to raise_error(ArgumentError, "SMTP From address may not be blank: nil")
expect { ses.deliver!(Mail.new { from "foo@bar.com" }) }.to raise_error(ArgumentError, "SMTP To address may not be blank: []")
end

it 'validates attachment without body' do
it "validates attachment without body" do
mail.body = nil
mail.add_file __FILE__
expect { ses.deliver!(mail) }.to raise_error(ArgumentError, 'Attachment provided without message body')
expect { ses.deliver!(mail) }.to raise_error(ArgumentError, "Attachment provided without message body")
end

context 'when options set' do
before { allow(mail).to receive(:to_s).and_return('Fixed message body') }
let(:ses_options) { { stub_responses: true, mail_options: { from_email_address: 'foo@bar.com', from_email_address_identity_arn: 'sa1' } } }
context "when options set" do
before { allow(mail).to receive(:to_s).and_return("Fixed message body") }
let(:ses_options) { {stub_responses: true, mail_options: {from_email_address: "foo@bar.com", from_email_address_identity_arn: "sa1"}} }

let(:exp) do
{
from_email_address: 'foo@bar.com',
from_email_address_identity_arn: 'sa2',
from_email_address: "foo@bar.com",
from_email_address_identity_arn: "sa2",
destination: {
to_addresses: %w[to1@def.com to2@xyz.com],
cc_addresses: %w[cc1@xyz.com cc2@def.com],
bcc_addresses: %w[bcc1@abc.com bcc2@def.com]
},
content: {
raw: {
data: 'Fixed message body'
data: "Fixed message body"
}
}
}
end

it 'allows pass-thru and override of default options' do
it "allows pass-thru and override of default options" do
expect(ses.client).to receive(:send_email).with(exp)
ses.deliver!(mail, from_email_address_identity_arn: 'sa2')
ses.deliver!(mail, from_email_address_identity_arn: "sa2")
end
end

context "without message_id_domain config" do
it 'sets the default domain as mail.message_id' do
it "sets the default domain as mail.message_id" do
ses.deliver!(mail)
expect(mail.message_id).to eq('OutboundMessageId@email.amazonses.com')
expect(mail.message_id).to eq("OutboundMessageId@email.amazonses.com")
end
end

context "with message_id_domain config" do
let(:ses_options) { { stub_responses: true, message_id_domain: 'eu-west-1.amazonses.com' } }
it 'sets as mail.message_id with the configured domain' do
let(:ses_options) { {stub_responses: true, message_id_domain: "eu-west-1.amazonses.com"} }
it "sets as mail.message_id with the configured domain" do
ses.deliver!(mail)
expect(mail.message_id).to eq('OutboundMessageId@eu-west-1.amazonses.com')
expect(mail.message_id).to eq("OutboundMessageId@eu-west-1.amazonses.com")
end
end

it 'returns the AWS response' do
it "returns the AWS response" do
expect(ses.deliver!(mail)).to be_a(Mail::SES)
end

context 'when :return_response set' do
let(:ses_options) { { stub_responses: true, return_response: true } }
context "when :return_response set" do
let(:ses_options) { {stub_responses: true, return_response: true} }

it 'returns the AWS response' do
it "returns the AWS response" do
expect(ses.deliver!(mail)).to be_a(Seahorse::Client::Response)
end
end

context 'error handling' do
before { allow_any_instance_of(Aws::SESV2::Client).to receive(:send_email).and_raise(RuntimeError.new('test')) }
context "error handling" do
before { allow_any_instance_of(Aws::SESV2::Client).to receive(:send_email).and_raise(RuntimeError.new("test")) }

context 'when :error_handler not set' do
it 'raises the error' do
expect { ses.deliver!(mail) }.to raise_error(RuntimeError, 'test')
context "when :error_handler not set" do
it "raises the error" do
expect { ses.deliver!(mail) }.to raise_error(RuntimeError, "test")
end
end

context 'when :error_handler set' do
let(:ses_options) { { stub_responses: true, error_handler: ->(a, b) {} } }
context "when :error_handler set" do
let(:ses_options) { {stub_responses: true, error_handler: ->(a, b) {}} }

it 'calls the error handler' do
it "calls the error handler" do
expect(ses_options[:error_handler]).to receive(:call).and_call_original
ses.deliver!(mail)
end
Expand Down
Loading

0 comments on commit c99cac0

Please sign in to comment.