Module: SqsAccelerator::SqsHelper

Defined in:
lib/sqs_helper.rb

Constant Summary collapse

SIGNATURE_VERSION =

Thanks to RightScale for the great RightAWS library. Parts of this code have been inspired by/stolen from RightAws.

"1"
API_VERSION =
"2008-01-01"
DEFAULT_HOST =
"queue.amazonaws.com"
DEFAULT_PORT =
80
DEFAULT_PROTOCOL =
'http'
REQUEST_TTL =
30
DEFAULT_VISIBILITY_TIMEOUT =
30
MAX_MESSAGE_SIZE =
(8 * 1024)
@@digest =
OpenSSL::Digest::Digest.new("sha1")

Instance Method Summary collapse

Instance Method Details

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sqs_helper.rb', line 41

def generate_request_hash(action, params={})
  message = params[:message]
  params.each{ |key, value| params.delete(key) if (value.nil? || key.is_a?(Symbol)) }
  request_hash = { "Action"           => action,
                   "Expires"          => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
                   "AWSAccessKeyId"   => aws_access_key_id,
                   "Version"          => API_VERSION,
                   "SignatureVersion" => SIGNATURE_VERSION }
  request_hash["MessageBody"] = message if message
  request_hash.merge(params)
  request_data = request_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
  request_hash['Signature'] = sign(aws_secret_access_key, request_data)
  logger.debug "request_hash:\n#{request_hash.pretty_inspect}"
  return request_hash
end

#parse_response(string) ⇒ Object



57
58
59
60
61
62
# File 'lib/sqs_helper.rb', line 57

def parse_response(string)
  parser = XML::Parser.string(string)
  doc = parser.parse
  doc.root.namespaces.default_prefix = "sqs"
  return doc
end

#sign(aws_secret_access_key, auth_string) ⇒ Object



23
24
25
# File 'lib/sqs_helper.rb', line 23

def sign(aws_secret_access_key, auth_string)
  Base64.encode64(OpenSSL::HMAC.digest(@@digest, aws_secret_access_key, auth_string)).strip
end

#URLencode(raw) ⇒ Object

From Amazon’s SQS Dev Guide, a brief description of how to escape: “URL encode the computed signature and other query parameters as specified in RFC1738, section 2.2. In addition, because the + character is interpreted as a blank space by Sun Java classes that perform URL decoding, make sure to encode the + character although it is not required by RFC1738.” Avoid using CGI::escape to escape URIs. CGI::escape will escape characters in the protocol, host, and port sections of the URI. Only target chars in the query string should be escaped.



36
37
38
39
# File 'lib/sqs_helper.rb', line 36

def URLencode(raw)
  e = URI.escape(raw)
  e.gsub(/\+/, "%2b")
end