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 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

  • :region (String) — default: "us-east-1"

    The server API endpoint host

  • :message_id_domain (String) — default: "us-east-1.amazonses.com"

    Domain used to build message_id header

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/aws/ses/base.rb', line 100

def initialize( options = {} )

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

  @signature_version = options[:signature_version] || 2
  @server = options[:server]
  @message_id_domain = options[:message_id_domain]
  @proxy_server = options[:proxy_server]
  @use_ssl = options[:use_ssl]
  @path = options[:path]
  @user_agent = options[:user_agent]
  @region = options[:region]
  @settings = {}

  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
end

Instance Attribute Details

#message_id_domainObject (readonly)

Returns the value of attribute message_id_domain.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def message_id_domain
  @message_id_domain
end

#portObject (readonly)

Returns the value of attribute port.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def port
  @port
end

#proxy_serverObject (readonly)

Returns the value of attribute proxy_server.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def proxy_server
  @proxy_server
end

#regionObject (readonly)

Returns the value of attribute region.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def region
  @region
end

#serverObject (readonly)

Returns the value of attribute server.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def server
  @server
end

#settingsObject

Returns the value of attribute settings.



89
90
91
# File 'lib/aws/ses/base.rb', line 89

def settings
  @settings
end

#signature_versionObject (readonly)

Returns the value of attribute signature_version.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

def signature_version
  @signature_version
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



88
89
90
# File 'lib/aws/ses/base.rb', line 88

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



154
155
156
# File 'lib/aws/ses/base.rb', line 154

def connection
  @http
end

#get_aws_auth_param(timestamp, secret_access_key, action = '', signature_version = 2) ⇒ Object

Set the Authorization header using AWS signed header authentication

Raises:

  • (ArgumentError)


197
198
199
200
201
202
203
204
205
206
# File 'lib/aws/ses/base.rb', line 197

def get_aws_auth_param(timestamp, secret_access_key, action = '', signature_version = 2)
  raise(ArgumentError, "signature_version must be `2` or `4`") unless signature_version == 2 || signature_version == 4
  encoded_canonical = SES.encode(secret_access_key, timestamp, false)

  if signature_version == 4
    SES.authorization_header_v4(sig_v4_auth_credential, sig_v4_auth_signed_headers, sig_v4_auth_signature(action))
  else
    SES.authorization_header(@access_key_id, 'HmacSHA256', encoded_canonical)
  end
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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/aws/ses/base.rb', line 161

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" => signature_version.to_s,
                  "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, action, signature_version)
  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