Class: AWS::SES::Base

Inherits:
Object
  • Object
show all
Includes:
Info, SendEmail
Defined in:
lib/aws/ses/base.rb,
lib/aws/ses/addresses.rb

Overview

AWS::SES::Base is the abstract super class of all classes who make requests against SES

Direct Known Subclasses

Addresses

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Info

#quota, #statistics

Methods included from ExpirableMemoize::ClassMethods

#memoized

Methods included from ExpirableMemoize::InstanceMethods

#__called_from__, #__method__, #expirable_memoize

Methods included from SendEmail

#send_email, #send_raw_email

Constructor Details

#initialize(options = {}) ⇒ Object

Returns the object.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :access_key_id (String) — default: ""

    The user’s AWS Access Key ID

  • :secret_access_key (String) — default: ""

    The user’s AWS Secret Access Key

  • :use_ssl (Boolean) — default: true

    Connect using SSL?

  • :server (String) — default: "email.us-east-1.amazonaws.com"

    The server API endpoint host

  • :proxy_server (String) — default: nil

    An HTTP proxy server FQDN

  • :user_agent (String) — default: "github-aws-ses-ruby-gem"

    The HTTP User-Agent header value

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aws/ses/base.rb', line 73

def initialize( options = {} )

  options = { :access_key_id => "",
              :secret_access_key => "",
              :use_ssl => true,
              :server => DEFAULT_HOST,
              :path => "/",
              :user_agent => USER_AGENT,
              :proxy_server => nil
              }.merge(options)

  @server = options[:server]
  @proxy_server = options[:proxy_server]
  @use_ssl = options[:use_ssl]
  @path = options[:path]
  @user_agent = options[:user_agent]

  raise ArgumentError, "No :access_key_id provided" if options[:access_key_id].nil? || options[:access_key_id].empty?
  raise ArgumentError, "No :secret_access_key provided" if options[:secret_access_key].nil? || options[:secret_access_key].empty?
  raise ArgumentError, "No :use_ssl value provided" if options[:use_ssl].nil?
  raise ArgumentError, "Invalid :use_ssl value provided, only 'true' or 'false' allowed" unless options[:use_ssl] == true || options[:use_ssl] == false
  raise ArgumentError, "No :server provided" if options[:server].nil? || options[:server].empty?

  if options[:port]
    # user-specified port
    @port = options[:port]
  elsif @use_ssl
    # https
    @port = 443
  else
    # http
    @port = 80
  end

  @access_key_id = options[:access_key_id]
  @secret_access_key = options[:secret_access_key]

  # Use proxy server if defined
  # Based on patch by Mathias Dalheimer.  20070217
  proxy = @proxy_server ? URI.parse(@proxy_server) : OpenStruct.new
  @http = Net::HTTP::Proxy( proxy.host,
                            proxy.port,
                            proxy.user,
                            proxy.password).new(options[:server], @port)

  @http.use_ssl = @use_ssl

  # Don't verify the SSL certificates.  Avoids SSL Cert warning in log on every GET.
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



64
65
66
# File 'lib/aws/ses/base.rb', line 64

def port
  @port
end

#proxy_serverObject (readonly)

Returns the value of attribute proxy_server.



64
65
66
# File 'lib/aws/ses/base.rb', line 64

def proxy_server
  @proxy_server
end

#serverObject (readonly)

Returns the value of attribute server.



64
65
66
# File 'lib/aws/ses/base.rb', line 64

def server
  @server
end

#settingsObject

Returns the value of attribute settings.



125
126
127
# File 'lib/aws/ses/base.rb', line 125

def settings
  @settings
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



64
65
66
# File 'lib/aws/ses/base.rb', line 64

def use_ssl
  @use_ssl
end

Instance Method Details

#addressesObject



69
70
71
# File 'lib/aws/ses/addresses.rb', line 69

def addresses
  @addresses ||= Addresses.new(self)
end

#connectionObject



127
128
129
# File 'lib/aws/ses/base.rb', line 127

def connection
  @http
end

#get_aws_auth_param(timestamp, secret_access_key) ⇒ Object

Set the Authorization header using AWS signed header authentication



170
171
172
173
# File 'lib/aws/ses/base.rb', line 170

def get_aws_auth_param(timestamp, secret_access_key)
  encoded_canonical = SES.encode(secret_access_key, timestamp, false)
  SES.authorization_header(@access_key_id, 'HmacSHA256', encoded_canonical)
end

#request(action, params = {}) ⇒ Object

Make the connection to AWS passing in our request.

allow us to have a one line call in each method which will do all of the work in making the actual request to AWS.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/aws/ses/base.rb', line 134

def request(action, params = {})
  # Use a copy so that we don't modify the caller's Hash, remove any keys that have nil or empty values
  params = params.reject { |key, value| value.nil? or value.empty?}
  
  timestamp = Time.now.getutc

  params.merge!( {"Action" => action,
                  "SignatureVersion" => "2",
                  "SignatureMethod" => 'HmacSHA256',
                  "AWSAccessKeyId" => @access_key_id,
                  "Version" => API_VERSION,
                  "Timestamp" => timestamp.iso8601 } )

  query = params.sort.collect do |param|
    CGI::escape(param[0]) + "=" + CGI::escape(param[1])
  end.join("&")

  req = {}

  req['X-Amzn-Authorization'] = get_aws_auth_param(timestamp.httpdate, @secret_access_key)
  req['Date'] = timestamp.httpdate
  req['User-Agent'] = @user_agent 

  response = connection.post(@path, query, req)
  
  response_class = AWS::SES.const_get( "#{action}Response" )
  result = response_class.new(action, response)
  
  if result.error?
    raise ResponseError.new(result)
  end
  
  result
end