aws_sns_publisher 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/aws_sns_publisher.rb +90 -0
  2. metadata +45 -0
@@ -0,0 +1,90 @@
1
+ require 'rubygems'
2
+ require 'cgi'
3
+ require 'time'
4
+ require 'openssl'
5
+ require 'base64'
6
+ require 'net/http'
7
+
8
+ # publisher = AwsSNSPublisher.new(
9
+ # "sns.us-east-1.amazonaws.com",
10
+ # access_key: "hogehoge",
11
+ # secret_key: "hogehogehoge")
12
+ # puts publisher.publish(
13
+ # "Action" => "Publish",
14
+ # "Message" => "hogehoge",
15
+ # "Subject" => "hoge",
16
+ # "TopicArn" => "arn:aws:sns:us-east-1:")
17
+
18
+ class AwsSNSPublisher
19
+
20
+ def initialize(host, options = {})
21
+ @secret_key = options[:secret_key]
22
+ raise Exception.new("You must supply a :secret_key") unless @secret_key
23
+ @access_key = options[:access_key]
24
+ @host = host
25
+ end
26
+
27
+ def publish params
28
+ uri = 'http://'
29
+ uri << @host
30
+ uri << '/'
31
+ uri << '?'
32
+ uri << self.query_with_signature(params)
33
+ return Net::HTTP.get(
34
+ URI.parse( uri ))
35
+ end
36
+
37
+ def query_with_signature(hash)
38
+ return hash_to_query( add_signature(hash) )
39
+ end
40
+
41
+ def add_signature(params)
42
+ # supply timestamp, access key
43
+ params["Timestamp"] = Time.now.iso8601
44
+ params["AWSAccessKeyId"] = @access_key
45
+ # supply signature infomation
46
+ params["SignatureMethod"] = "HmacSHA256"
47
+ params["SignatureVersion"] = "2"
48
+
49
+ params.delete("Signature")
50
+
51
+ query_string = canonical_querystring(params)
52
+ string_to_sign = string_to_sign(query_string)
53
+ hmac = OpenSSL::HMAC::new(@secret_key, OpenSSL::Digest::SHA256.new)
54
+ hmac.update( string_to_sign )
55
+ # chomp is important! the base64 encoded version will have a newline at the end
56
+ signature = Base64.encode64(hmac.digest).chomp
57
+
58
+ params["Signature"] = signature
59
+
60
+ return params
61
+ end
62
+
63
+ # RFC3986.
64
+ def url_encode(string)
65
+ return CGI.escape(string).gsub("%7E", "~").gsub("+", "%20")
66
+ end
67
+
68
+ # canonical order => sort byte order.
69
+ def canonical_querystring(params)
70
+ values = params.keys.sort.collect {|key| [url_encode(key), url_encode(params[key].to_s)].join("=") }
71
+ return values.join("&")
72
+ end
73
+
74
+ def string_to_sign(query_string, options = {})
75
+ options[:verb] = "GET"
76
+ options[:request_uri] = "/"
77
+ options[:host] = @host
78
+ return options[:verb] + "\n" +
79
+ options[:host].downcase + "\n" +
80
+ options[:request_uri] + "\n" +
81
+ query_string
82
+ end
83
+
84
+ def hash_to_query(hash)
85
+ hash.collect { |k, v|
86
+ url_encode(k) + "=" + url_encode(v)
87
+ }.join("&")
88
+ end
89
+ end
90
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_sns_publisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sowawa (Keisuke SOGAWA)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: publishing sns via http.
15
+ email: keisuke.sogawa+gems@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/aws_sns_publisher.rb
21
+ homepage: http://rubygems.org/gems/aws_sns_publisher
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Simple publishing Amazon SNS
45
+ test_files: []