aliyun_mns_queue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf52c5dae3363906bef015c2f1f5275a336d2504
4
+ data.tar.gz: 56717485de16cce94618bf93fac09ebe378e52e2
5
+ SHA512:
6
+ metadata.gz: 4594c1c22a3e509a47b374b1d2467befdf6c5981ddfdf5f4ea4177395b7ccf74791252792c44481f068b7293139b21caf27bb8c1209f5a331ca91cc9071b3518
7
+ data.tar.gz: 344fdaf67aa296cd6b4656c4ee24199dcad3942598f2aeabc3eeca797047fbaa50f572483654edbca8f1e179e46b4a392d8b4a2fa6c071b802195d8dbcc9135a
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ spec/spec_helper.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Alvin Ye
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ ## Intro
2
+
3
+ aliyun-mns
4
+
5
+ 阿里云消息队列
6
+
7
+ #### Usage
8
+
9
+ ```
10
+
11
+ gem "aliyun-mns"
12
+
13
+ AliyunMns.setup do |config|
14
+ config.access_key = 'abc...'
15
+ config.secret_key = 'w5....'
16
+ config.api_version = "2015-06-06"
17
+ config.endpoint = "14589xxxxx.mns.cn-shanghai.aliyuncs.com"
18
+ config.protocol = "http"
19
+ end
20
+
21
+ msg = '{"id": 123, "text": "eee"}'
22
+ p ["Send", AliyunMns.send_msg("item-dwg-upgrade", Base64.encode64(msg))]
23
+
24
+ msg = AliyunMns.receive("item-dwg-upgrade")
25
+ receipt_handle = msg["Message"]["ReceiptHandle"]
26
+ body = Base64.decode64(msg["Message"]["MessageBody"]) rescue nil
27
+
28
+ p ["receive", "body", body, "receipt_handle", receipt_handle ]
29
+
30
+ p ["delete", AliyunMns.delete("item-dwg-upgrade", receipt_handle)]
31
+
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'aliyun_mns_queue'
7
+ s.version = '0.0.1'
8
+ s.authors = ['Alvin Ye']
9
+ s.email = ['alvin.ye.cn@gmail.com']
10
+ s.description = %q{ruby client for aliyun mns queue without topic}
11
+ s.summary = %q{ruby client for aliyun mns queue without topic }
12
+ s.homepage = 'https://github.com/alvin2ye/aliyun_mns'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|s.features)/})
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_runtime_dependency "crack"
21
+ s.add_runtime_dependency "json"
22
+ s.add_development_dependency 'bundler'
23
+ s.add_development_dependency 'rake', '~> 11.2.2'
24
+ end
data/lib/aliyun_mns.rb ADDED
@@ -0,0 +1,102 @@
1
+ require "time"
2
+ require "uri"
3
+ require "digest"
4
+ require "base64"
5
+ require "openssl"
6
+ require "net/http"
7
+ require "json"
8
+ require "crack"
9
+
10
+ module AliyunMns
11
+ class Error < StandardError; end
12
+
13
+ def self.setup
14
+ yield self
15
+ end
16
+
17
+ class << self
18
+ attr_accessor :access_key, :secret_key, :api_version, :endpoint, :protocol
19
+
20
+ def base64_hmac(data)
21
+ Base64.encode64("#{OpenSSL::HMAC.digest("sha1", secret_key, data)}").strip
22
+ end
23
+
24
+ def authorization(op = {})
25
+ body = [op[:method], op[:content_md5], op[:content_type],
26
+ op[:date], "x-mns-version:#{api_version}", op[:resource]].join("\n")
27
+
28
+ "MNS #{access_key}:#{base64_hmac(body)}"
29
+ end
30
+
31
+ def content_xml(content)
32
+ content = <<-XML
33
+ <?xml version="1.0"?>
34
+ <Message xmlns="http://mqs.aliyuncs.com/doc/v1/">
35
+ <MessageBody>#{content}</MessageBody>
36
+ </Message>
37
+ XML
38
+ end
39
+
40
+ def send_msg(queue, content)
41
+ httpdate = Time.now.httpdate
42
+ uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages")
43
+ content = content_xml(content)
44
+ content_md5 = Base64::encode64(Digest::MD5.hexdigest(content)).chop
45
+ authorization_code = authorization(method: "POST",
46
+ content_md5: content_md5,
47
+ content_type: "text/xml;charset=utf-8",
48
+ date: httpdate,
49
+ resource: uri.path)
50
+
51
+ request = Net::HTTP::Post.new(uri.path, {
52
+ "Authorization" => authorization_code,
53
+ "Date" => httpdate,
54
+ "Host" => uri.host,
55
+ "x-mns-version" => api_version,
56
+ "Content-Length" => content.size.to_s,
57
+ "Content-MD5" => content_md5,
58
+ "Content-type" => "text/xml;charset=utf-8"
59
+ })
60
+
61
+ request.body = content
62
+ Net::HTTP.new(uri.host, uri.port).request(request)
63
+ end
64
+
65
+ def receive(queue)
66
+ httpdate = Time.now.httpdate
67
+ uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages")
68
+ authorization_code = authorization(method: "GET",
69
+ date: httpdate,
70
+ resource: uri.path)
71
+
72
+ request = Net::HTTP::Get.new(uri.path, {
73
+ "Authorization" => authorization_code,
74
+ "Date" => httpdate,
75
+ "Host" => uri.host,
76
+ "x-mns-version" => api_version
77
+ })
78
+
79
+ response = Net::HTTP.new(uri.host, uri.port).request(request)
80
+
81
+ JSON.parse(Crack::XML.parse(response.body).to_json)
82
+ end
83
+
84
+ def delete(queue, receipt_handle)
85
+ httpdate = Time.now.httpdate
86
+ uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages?ReceiptHandle=#{receipt_handle}")
87
+
88
+ authorization_code = authorization(method: "DELETE",
89
+ date: httpdate,
90
+ resource: "#{uri.path}?#{uri.query}")
91
+
92
+ request = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}", {
93
+ "Authorization" => authorization_code,
94
+ "Date" => httpdate,
95
+ "Host" => uri.host,
96
+ "x-mns-version" => api_version
97
+ })
98
+
99
+ Net::HTTP.new(uri.host, uri.port).request(request)
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyun_mns_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alvin Ye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: crack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 11.2.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 11.2.2
69
+ description: ruby client for aliyun mns queue without topic
70
+ email:
71
+ - alvin.ye.cn@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - aliyun-mns.gemspec
83
+ - lib/aliyun_mns.rb
84
+ homepage: https://github.com/alvin2ye/aliyun_mns
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.6
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: ruby client for aliyun mns queue without topic
108
+ test_files: []