Class: AwsSNSPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_sns_publisher.rb

Overview

publisher = AwsSNSPublisher.new(

"sns.us-east-1.amazonaws.com",
access_key: "hogehoge", 
secret_key: "hogehogehoge")

puts publisher.publish(

"Action" => "Publish",
"Message" => "hogehoge",
"Subject" => "hoge",
"TopicArn" => "arn:aws:sns:us-east-1:")

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ AwsSNSPublisher

Returns a new instance of AwsSNSPublisher.

Raises:

  • (Exception)

20
21
22
23
24
25
# File 'lib/aws_sns_publisher.rb', line 20

def initialize(host, options = {})
  @secret_key = options[:secret_key]
  raise Exception.new("You must supply a :secret_key") unless @secret_key
  @access_key = options[:access_key]
  @host = host
end

Instance Method Details

#add_signature(params) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aws_sns_publisher.rb', line 41

def add_signature(params)
  # supply timestamp, access key
  params["Timestamp"]      = Time.now.iso8601
  params["AWSAccessKeyId"] = @access_key
  # supply signature infomation
  params["SignatureMethod"]  = "HmacSHA256"
  params["SignatureVersion"] = "2"
  
  params.delete("Signature")

  query_string = canonical_querystring(params)
  string_to_sign = string_to_sign(query_string)
  hmac = OpenSSL::HMAC::new(@secret_key, OpenSSL::Digest::SHA256.new)
  hmac.update( string_to_sign )
  # chomp is important! the base64 encoded version will have a newline at the end
  signature = Base64.encode64(hmac.digest).chomp 
 
  params["Signature"] = signature
 
  return params
end

#canonical_querystring(params) ⇒ Object

canonical order => sort byte order.


69
70
71
72
# File 'lib/aws_sns_publisher.rb', line 69

def canonical_querystring(params)
  values = params.keys.sort.collect {|key|  [url_encode(key), url_encode(params[key].to_s)].join("=") }
  return values.join("&")
end

#hash_to_query(hash) ⇒ Object


84
85
86
87
88
# File 'lib/aws_sns_publisher.rb', line 84

def hash_to_query(hash)
  hash.collect { |k, v|
    url_encode(k) + "=" + url_encode(v)
  }.join("&")
end

#publish(params) ⇒ Object


27
28
29
30
31
32
33
34
35
# File 'lib/aws_sns_publisher.rb', line 27

def publish params
  uri = 'http://'
  uri << @host
  uri << '/'
  uri << '?'
  uri << self.query_with_signature(params) 
  return Net::HTTP.get(
    URI.parse( uri ))
end

#query_with_signature(hash) ⇒ Object


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

def query_with_signature(hash)
  return hash_to_query( add_signature(hash)  )
end

#string_to_sign(query_string, options = {}) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'lib/aws_sns_publisher.rb', line 74

def string_to_sign(query_string, options = {})
  options[:verb] = "GET"
  options[:request_uri] = "/"
  options[:host] = @host
  return options[:verb] + "\n" + 
      options[:host].downcase + "\n" +
      options[:request_uri] + "\n" +
      query_string
end

#url_encode(string) ⇒ Object

RFC3986.


64
65
66
# File 'lib/aws_sns_publisher.rb', line 64

def url_encode(string)
  return CGI.escape(string).gsub("%7E", "~").gsub("+", "%20")
end