Module: AWS::Rails

Defined in:
lib/aws/rails.rb

Overview

A handful of useful Rails integration methods.

If you require this gem inside a Rails application (via config.gem for rails 2 and bundler for rails 3) then AWS::Rails.setup is called automatically.

Class Method Summary collapse

Class Method Details

.add_action_mailer_delivery_method(name = :amazon_ses, options = {}) ⇒ nil

Adds a delivery method to ActionMailer that uses SimplEmailService.

Once you have an SES delivery method you can configure Rails to use this for ActionMailer in your environment configuration (e.g. RAILS_ROOT/config/environments/production.rb)

config.action_mailer.delivery_method = :amazon_ses

Defaults

Normally you don’t need to call this method. By default a delivery method named :amazon_ses is added to ActionMailer::Base. This delivery method uses your default configuration (#AWS.config).

Custom SES Options

If you need to supply configuration values for SES that are different than those in AWS.config then you can pass those options:

AWS.add_action_mailer_delivery_method(:ses, custom_options)

Parameters:

  • options (Hash) (defaults to: {})
  • name (Symbol) (defaults to: :amazon_ses)

    (:amazon_ses) The name of the delivery method. The name used here should be the same as you set in your environment config. If you name the delivery method :amazon_ses then you could do something like this in your config/environments/ENV.rb file:

    config.action_mailer.delivery_method = :amazon_ses
    
  • options (Hash) (defaults to: {})

    A hash of options that are passes to SimpleEmailService#new before delivering email.

Returns:

  • (nil)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/aws/rails.rb', line 173

def self.add_action_mailer_delivery_method name = :amazon_ses, options = {}

  amb = ::ActionMailer::Base

  if ::Rails.version.to_f >= 3
    amb.add_delivery_method(name, AWS::SimpleEmailService, options)
  else
    amb.send(:define_method, "perform_delivery_#{name}") do |mail|
      AWS::SimpleEmailService.new(options).send_raw_email(mail)
    end
  end

  nil

end

.load_yaml_configObject

Loads AWS configuration options from RAILS_ROOT/config/aws.yml.

This configuration file is optional. You can omit this file and instead use ruby to configure AWS inside a configuration initialization script (e.g. RAILS_ROOT/config/intializers/aws.rb).

If you have a yaml configuration file it should be formatted like the standard database.yml file in a Rails application. This means there should be one section for Rails environment:

development:
  access_key_id: YOUR_ACCESS_KEY_ID
  secret_access_key: YOUR_SECRET_ACCESS_KEY
  simple_db_consistent_reads: false

production:
  access_key_id: YOUR_ACCESS_KEY_ID
  secret_access_key: YOUR_SECRET_ACCESS_KEY
  simple_db_consistent_reads: true

You should also consider DRYing up your configuration file using YAML references:

development:
  access_key_id: YOUR_ACCESS_KEY_ID
  secret_access_key: YOUR_SECRET_ACCESS_KEY
  simple_db_consistent_reads: false

production:
  <<: *development
  simple_db_consistent_reads: true

The yaml file will also be ERB parsed so you can use ruby inside of it:

development:
  access_key_id: YOUR_ACCESS_KEY_ID
  secret_access_key: <%= read_secret_from_a_secure_location %>
  simple_db_consistent_reads: false

production:
  <<: *development
  simple_db_consistent_reads: true


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/aws/rails.rb', line 124

def self.load_yaml_config
  
  path = Pathname.new("#{rails_root}/config/aws.yml")
  
  if File.exists?(path)
    cfg = YAML::load(ERB.new(File.read(path)).result)
    unless cfg[rails_env]
      raise "config/aws.yml is missing a section for `#{rails_env}`"
    end
    AWS.config(cfg[rails_env])
  end
  
end

.log_to_rails_loggernil

Configures AWS to log to the Rails defualt logger.

Returns:

  • (nil)


191
192
193
194
# File 'lib/aws/rails.rb', line 191

def self.log_to_rails_logger
  AWS.config(:logger => rails_logger)
  nil
end

.setupnil

Adds extra functionality to Rails.

Normailly this method is invoked automatically when you require this gem in a Rails Application:

Rails 3+ (RAILS_ROOT/Gemfile)

gem 'aws-sdk-rails'

Rails 2.1 - 2.3 (RAILS_ROOT/config/environment.rb)

config.gem 'aws-sdk-rails'

Selective Rails Features

If you would prefer to cherry pick a few of the features added by this gem you can change your gem requirement to load ‘aws/rails’ instead:

Rails 3+ (RAILS_ROOT/Gemfile)

gem 'aws-sdk-rails', :require => 'aws/rails'

Rails 2.1 - 2.3 (RAILS_ROOT/config/environment.rb)

config.gem 'aws-sdk-rails', :lib => 'aws/rails'

In both of the examples above you can now configure AWS and call the setup methods of choice inside a config initializer (e.g. RAILS_ROOT/config/initializers/aws.rb):

AWS.config(...)
AWS.log_to_rails_logger
AWS.add_action_mailer_delivery_method

Returns:

  • (nil)


74
75
76
77
78
79
# File 'lib/aws/rails.rb', line 74

def self.setup
  load_yaml_config
  add_action_mailer_delivery_method
  log_to_rails_logger
  nil
end