aliyun-mqs 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abd6a6844d4985d359ec5a61d4623b989523ca02
4
- data.tar.gz: 0d659d51d20a37c68faa71aa32e1e32f87590e52
3
+ metadata.gz: 8a3074e0ab5236d6fd2250bf20a109efb4476f41
4
+ data.tar.gz: a84cbd6fb8bd7d4600a0c897b0c6443c660c094d
5
5
  SHA512:
6
- metadata.gz: 8f973502a48e1a9937dff5abd27e99b917d2e04eb14a5ffe729b7f5eed90841af9a84f6a8153559a8f8babb5c6c01c90f159808f8f5e04751e2cb787e42d00cd
7
- data.tar.gz: ed4cdf563e6e182b52c63bf13cf42551df76d90d9771e2576a5b9b84e67866773e892a3da5a66fb2708c8bf2845dc3e55543e0d3e02d4b4c643728f0fe7c2972
6
+ metadata.gz: cb742df399b32d36da37cf5daa1eed5c12fd43db3c9ef2e66dc5be4f1244e9229084a02f067d2db9122bc25cf0c1d3ae9b3bac0d447f76a58d45b87343c3dc3e
7
+ data.tar.gz: 70fe12855fac56cf0654d464aff3c6a98bf88d2242707b0034c35b836757c40081ff4207e7340410076fa9d4fdadaa2cf9fbec3c3c774bb9836bf0483f683d11
data/.gitignore CHANGED
@@ -12,3 +12,5 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ /.ruby-version
16
+ /.ruby-gemset
@@ -0,0 +1,8 @@
1
+ before_install:
2
+ - gem update --system 2.1.11
3
+ scripte: "CODECLIMATE_REPO_TOKEN=d9e569a99fb83321e72de3c22e73c4ef9536726a91d524d3e08d9d1b726dcc72 bundle exec rake"
4
+ language: ruby
5
+ rvm:
6
+ - "2.0.0"
7
+ - "2.1.0"
8
+ - "2.1.1"
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source 'https://rubygems.org'
1
+ source 'http://ruby.taobao.org'
2
2
 
3
3
  # Specify your gem's dependencies in mqs.gemspec
4
4
  gemspec
5
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # aliyun-mqs
2
+ [![Build Status](https://travis-ci.org/skinnyworm/aliyun-mqs.svg)](https://travis-ci.org/skinnyworm/aliyun-mqs) [![Code Climate](https://codeclimate.com/github/skinnyworm/aliyun-mqs.png)](https://codeclimate.com/github/skinnyworm/aliyun-mqs) [![Code Coverage](https://codeclimate.com/github/skinnyworm/aliyun-mqs/coverage.png)](https://codeclimate.com/github/skinnyworm/aliyun-mqs) [![Gem Version](https://badge.fury.io/rb/aliyun-mqs.svg)](http://badge.fury.io/rb/aliyun-mqs)
2
3
 
3
4
  Talk to the mighty Aliyun MQS with charming ruby.
4
5
 
@@ -14,110 +15,207 @@ And then execute:
14
15
 
15
16
  $ bundle
16
17
 
17
- Or install it yourself as:
18
+ As this branch of the gem is not yet merged into the master branch, you can not install it via `gem install` for now.
18
19
 
19
- $ gem install aliyun-mqs
20
+ ## Configuration
20
21
 
21
- ## Usage
22
+ ### Command line configuration
22
23
 
23
- ### Require
24
+ The gem come with a command line tool `mqs`. It can help you easily manage the aliyun mqs within the terminal. In order to use this tool, you need to provide a configuration yaml file in your home directory which contains information about your mqs access_id and keys.
25
+
26
+ The configuration file should be stored at `~/.aliyun-mqs.yml`
24
27
 
25
- ```ruby
26
- require 'aliyun/mqs'
27
28
  ```
28
29
 
29
- ###
30
+ access_id: 'lUxxxxxxxx'
31
+ key: 'VWxxxxxxxxxxxxxxxxxxxxx'
32
+ region: 'cn-hangzhou'
33
+ owner_id: 'ckxxxxxxxx'
34
+
35
+ ```
36
+
37
+
38
+ ### Rails configuration
30
39
 
31
- ### Configure
40
+ If you are going to use this gem in a rails environment. You need to create a configuration file at `<RAILS_ROOT>/config/aliyun-mqs.yml`. In this way, you can use different set of queues for your development or production environments.
41
+
42
+ ```
43
+
44
+ development:
45
+ access_id: 'lUxxxxxxxx'
46
+ key: 'VWxxxxxxxxxxxxxxxxxxxxx'
47
+ region: 'cn-hangzhou'
48
+ owner_id: 'ckxxxxxxxx'
49
+
50
+ production:
51
+ access_id: 'lUxxxxxxxx'
52
+ key: 'VWxxxxxxxxxxxxxxxxxxxxx'
53
+ region: 'cn-hangzhou'
54
+ owner_id: 'ckxxxxxxxx'
55
+
56
+ ```
57
+
58
+ ### Config in an application
59
+
60
+ At last you can also config the gem in place, by excute the following code before invoking and queue service.
32
61
 
33
62
  ```ruby
34
63
  Aliyun::Mqs.configure do |config|
35
- config.access_key_id = '0123456789ABCDEF'
36
- config.access_key_secret = '*********************'
37
- config.access_region = 'cn-hangzhou'
38
- config.access_owner_id = 'cirno'
64
+ config.access_id = 'access-id'
65
+ config.key = "key"
66
+ config.region = 'region'
67
+ config.owner_id = 'owner-id'
39
68
  end
40
69
  ```
41
70
 
42
- ### Response
43
71
 
44
- Most API returns a `Response` object, which contains status and all leaf element in the XML response.
45
- Eg: Every leaf element of
72
+ ## Commandline
73
+
74
+ This gem comes with a handy commandline tool `mqs` to help you manage your queue. Once the queue is installed. Execute 'mqs --help' to find out what commands are supported.
75
+
76
+ ```
77
+ $ mqs --help
78
+
79
+ Commands:
80
+ mqs consume [queue] -wait <wait_seconds> # 从[queue]队列接受消息并删除
81
+ mqs create [queue] # 创建一个消息队列
82
+ mqs delete [queue] # 删除一个消息队列
83
+ mqs peek [queue] # 从[queue]队列中peek消息
84
+ mqs queues # 列出所有消息队列列表
85
+ mqs send [queue] [message] # 往[queue]队列发送[message]消息
86
+ ```
87
+
88
+ Following are few examples.
89
+
90
+ #### 消息队列列表
91
+
92
+ ```
93
+ $ mqs queues
94
+ 消息队列列表
95
+ another
96
+ another1
97
+ another2
98
+ another3
99
+ another4
100
+ another5
101
+ ```
102
+
103
+ #### 往队列发送消息
46
104
 
47
- ```xml
105
+ ```
106
+ $ mqs send another "Test message"
107
+ 发送消息到another队列
48
108
  <?xml version="1.0"?>
49
109
  <Message xmlns="http://mqs.aliyuncs.com/doc/v1">
50
- <MessageBodyMD5>654E27D8879283831B664BD8B7F0AD4</MessageBodyMD5>
51
- <MessageId>6003A74BEB1D5460-1-147DF4497D4-200000024</MessageId>
110
+ <MessageBodyMD5>82DFA5549EBC9AFC168EB7931EBECE5F</MessageBodyMD5>
111
+ <MessageId>55D5B01D1AE93D78-1-14979D45F33-200000001</MessageId>
52
112
  </Message>
53
113
  ```
54
- can be accessed as:
55
114
 
56
- ```ruby
57
- Aliyun::Response::message_body_md5
58
- Aliyun::Response::message_id
115
+ #### Peek队列中的消息
116
+ ```
117
+ $ mqs peek another
118
+ Peek 队列another中的消息
119
+ =============================================
120
+ 队列: another
121
+ ID: 55D5B01D1AE93D78-1-14979D45F33-200000001
122
+ MD5: 82DFA5549EBC9AFC168EB7931EBECE5F
123
+ Enqueue at: 2014-11-04 16:03:21 +0800
124
+ First enqueue at: 2014-11-04 16:03:21 +0800
125
+ Dequeue count: 0
126
+ Priority: 10
127
+ =============================================
128
+ Test message
59
129
  ```
60
130
 
61
- To determine wheater a request call is succeed, use `Response::success?`. `Response` is an imutable object, all instance method with side-effects will return a new instance of `Response`. So, instead of
62
131
 
63
- ```ruby
64
- message = queue.receive # message is an Response
65
- message.visibility = 1.minute
66
- message.visibility = 8.minute # wrong, reuse receipt handle
132
+ #### 消费队列中的消息
133
+
134
+ Be careful, consume command will first receive the message and then delete the message within a time period specfied by the queue.
135
+
136
+ ```
137
+ $ mqs consume another
138
+ Consume 队列another中的消息
139
+ =============================================
140
+ 队列: another
141
+ ID: 55D5B01D1AE93D78-1-14979D45F33-200000001
142
+ MD5: 82DFA5549EBC9AFC168EB7931EBECE5F
143
+ Receipt handle: 1-ODU4OTkzNDU5My0xNDE1MDg4MzU2LTEtMTA=
144
+ Enqueue at: 2014-11-04 16:03:21 +0800
145
+ First enqueue at: 2014-11-04 16:05:26 +0800
146
+ Next visible at: 2014-11-04 16:05:56 +0800
147
+ Dequeue count: 1
148
+ Priority: 10
149
+ =============================================
150
+ Test message
67
151
  ```
68
152
 
69
- you shoud pass the return value (agian, an `Response`) around:
70
153
 
71
- ```ruby
72
- resp = queue.receive
73
- resp = resp.visibility = 1.minute
74
- resp = resp.visibility = 8.minute
75
- ```
76
154
 
77
- ### Get an existing queue
155
+ ## Usage
156
+
157
+ Following are some example useage of the gem. You can read the specs to understand the full features of this gem
78
158
 
79
159
  ```ruby
80
- queue = Aliyun::Mqs.get 'queue-name'
81
- queue = Aliyun::Mqs.get 'queue-name', :access_owner_id: 'your_id'
82
- ```
160
+ #get a list of queue object
161
+ queues = Aliyun::Mqs::Queue.queues
83
162
 
84
- ### Delete a queue
163
+ #get all queues start with name 'query'
164
+ queues =Aliyun::Mqs::Queue.queues(query: "query")
85
165
 
86
- ```ruby
87
- queue.destroy
88
- ```
166
+ #get all queues start with name 'query'
167
+ queues = Aliyun::Mqs::Queue.queues(size: 5)
89
168
 
90
- ### Send messages
169
+ #Obtain a queue object with name "aQueue"
170
+ queue = Aliyun::Mqs::Queue["aQueue"]
91
171
 
92
- ```ruby
93
- resp = queue.send "Hello, World!"
94
- resp = queue.send "Hello, Cirno!", delay_seconds: 9, priority: 9
95
- ```
172
+ #Create a new queue
173
+ Aliyun::Mqs::Queue["aQueue"].create
96
174
 
97
- ### Recveive messages
175
+ #Create a new queue with polling wait 30 seconds
176
+ Aliyun::Mqs::Queue["aQueue"].create(:PollingWaitSeconds => 30)
98
177
 
99
- ```ruby
100
- message = queue.receive
101
- message = queue.receive waitseconds: 10, peekonly: true
102
- message = queue.peek
103
- ```
178
+ #Delete an existing queue
179
+ Aliyun::Mqs::Queue["aQueue"].delete
104
180
 
105
- ### Delete messages
181
+ #Send a text message
182
+ Aliyun::Mqs::Queue["aQueue"].send_message "text message"
106
183
 
107
- ```ruby
184
+ #Send a text message with priority option
185
+ Aliyun::Mqs::Queue["aQueue"].send_message "text message", :Priority=>1
186
+
187
+ #Receive a message
188
+ message = Aliyun::Mqs::Queue["aQueue"].receive_message
189
+
190
+ #Sample rspec for a message
191
+ expect(message).not_to be_nil
192
+ expect(message.id).to eq("5fea7756-0ea4-451a-a703-a558b933e274")
193
+ expect(message.body).to eq("This is a test message")
194
+ expect(message.body_md5).to eq("fafb00f5732ab283681e124bf8747ed1")
195
+ expect(message.receipt_handle).to eq("MbZj6wDWli+QEauMZc8ZRv37sIW2iJKq3M9Mx/KSbkJ0")
196
+ expect(message.enqueue_at).to eq(Time.at(1250700979248000/1000.0))
197
+ expect(message.first_enqueue_at).to eq(Time.at(1250700779318000/1000.0))
198
+ expect(message.next_visible_at).to eq(Time.at(1250700799348000/1000.0))
199
+ expect(message.dequeue_count).to eq(1)
200
+ expect(message.priority).to eq(8)
201
+
202
+ #Receive a message with option to override the default poll wait time of the queue.
203
+ message = Aliyun::Mqs::Queue["aQueue"].receive_message wait_seconds: 60
204
+
205
+ #Peek message in the queue
206
+ message = Aliyun::Mqs::Queue["aQueue"].peek_message
207
+
208
+ #Delete received message
209
+ message = Aliyun::Mqs::Queue["aQueue"].receive_message
108
210
  message.delete
109
- queue.delete message
110
- queue.delete "#{you_message_recipet_handlerer}"
111
- ```
112
211
 
113
- ### Change message visibility (not implemented yet)
212
+ #Change message visibility
213
+ message = Aliyun::Mqs::Queue["aQueue"].receive_message
214
+ message.change_visibility 10
114
215
 
115
- ```ruby
116
- resp = message.visibility = Time.now + 1.hour
117
- resp = message.visibility = Time.now.to_f * 1000 + 10
118
- resp = message.visibility += 1.hour
119
216
  ```
120
217
 
218
+
121
219
  ## Contributing
122
220
 
123
221
  1. Fork it ( https://github.com/mgampkay/aliyun-mqs/fork )
@@ -125,7 +223,3 @@ resp = message.visibility += 1.hour
125
223
  3. Commit your changes (`git commit -am 'Add some feature'`)
126
224
  4. Push to the branch (`git push origin my-new-feature`)
127
225
  5. Create a new Pull Request
128
-
129
- ## References
130
-
131
- + xiaohuilam's [aliyun-mqs-php-library](https://github.com/xiaohuilam/aliyun-mqs-php-library)
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ require File.join('bundler', 'gem_tasks')
3
+ require File.join('rspec', 'core', 'rake_task')
2
4
 
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -6,8 +6,8 @@ require 'aliyun/mqs/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'aliyun-mqs'
8
8
  spec.version = Aliyun::Mqs::VERSION
9
- spec.authors = ["mgampkay"]
10
- spec.email = ["mgampkay@gmail.com"]
9
+ spec.authors = ["mgampkay","skinnyworm"]
10
+ spec.email = ["mgampkay@gmail.com", "askinnyworm@gmail.com"]
11
11
  spec.summary = 'Ruby SDK for Aliyun MQS (non-official)'
12
12
  spec.description = 'Non-official SDK for Aliyun MQS'
13
13
  spec.homepage = 'https://github.com/mgampkay/aliyun-mqs'
@@ -20,7 +20,11 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'nokogiri', '>= 1.6'
22
22
  spec.add_dependency 'activesupport', '>= 4.1'
23
+ spec.add_dependency "rest-client"
24
+ spec.add_dependency "thor"
23
25
 
24
- spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "bundler"
25
27
  spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "pry"
26
30
  end
data/bin/mqs ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'aliyun/mqs'
3
+
4
+ Aliyun::Mqs::Cli.start(ARGV)
@@ -1,26 +1,50 @@
1
+ require 'active_support/core_ext'
2
+ require 'rest-client'
3
+ require 'nokogiri'
4
+ require 'yaml'
1
5
  require 'aliyun/mqs/version'
2
- require 'aliyun/mqs/queue'
3
- require 'aliyun/mqs/response'
4
- require 'aliyun/mqs/configuration'
5
- require 'aliyun/mqs/http'
6
+ # RestClient.log=STDOUT
7
+
8
+ # Monkey patch hash to support xml array and xml object
9
+ class Hash
10
+ def self.xml_array content, *path
11
+ o = xml_object(content, *path)
12
+ return (o.is_a?(Array) ? o : [o]).reject{|n| n.empty?}
13
+ end
14
+
15
+ def self.xml_object content, *path
16
+ h = from_xml(content)
17
+ path.reduce(h){|memo, node| memo = memo[node] || {}}
18
+ end
19
+ end
6
20
 
7
21
  module Aliyun
8
22
  module Mqs
23
+ require 'aliyun/mqs/queue'
24
+ require 'aliyun/mqs/message'
25
+ require 'aliyun/mqs/request'
26
+ require 'aliyun/mqs/cli'
9
27
 
10
28
  class << self
11
- attr_writer :configuration
12
- end
13
-
14
- def self.configuration
15
- @configuration ||= Configuration.new
16
- end
29
+ def configuration
30
+ @configuration ||= begin
31
+ if defined? Rails
32
+ config_file = Rails.root.join("config/aliyun-mqs.yml")
33
+ else
34
+ config_file = File.expand_path("~/.aliyun-mqs.yml")
35
+ end
17
36
 
18
- def self.configure
19
- yield(configuration)
20
- end
37
+ if (File.exist?(config_file))
38
+ config = YAML.load(ERB.new(File.new(config_file).read).result)
39
+ config = config[Rails.env] if defined? Rails
40
+ end
41
+ OpenStruct.new(config || {access_id:"", key:"", region:"", owner_id:""})
42
+ end
43
+ end
21
44
 
22
- def self.get(name, access_owner_id: nil)
23
- Aliyun::Mqs::Queue.new(name, access_owner_id: access_owner_id)
45
+ def configure
46
+ yield(configuration)
47
+ end
24
48
  end
25
49
 
26
50
  end
@@ -0,0 +1,54 @@
1
+ require 'thor'
2
+
3
+ module Aliyun::Mqs
4
+ class Cli < Thor
5
+
6
+ desc "queues", "列出 QueueOwnerId 下的消息队列列表"
7
+ def queues()
8
+ execute("消息队列列表"){ Queue.queues }
9
+ end
10
+
11
+ desc "delete [queue]", "删除一个消息队列"
12
+ def delete(name)
13
+ execute("删除消息队列'#{name}'"){ Queue[name].delete }
14
+ end
15
+
16
+ desc "create [queue]", "创建一个消息队列"
17
+ def create(name)
18
+ execute("创建消息队列'#{name}'"){ Queue[name].create }
19
+ end
20
+
21
+ desc "consume [queue] -wait <wait_seconds>", "从[queue]队列接受消息并删除"
22
+ option :wait
23
+ def consume(name)
24
+ execute("Consume 队列#{name}中的消息") do
25
+ message = Queue[name].receive_message(wait_seconds: options[:wait])
26
+ message.delete
27
+ message
28
+ end
29
+ end
30
+
31
+ desc "send [queue] [message]", "往[queue]队列发送[message]消息"
32
+ def send(name, content)
33
+ execute("发送消息到#{name}队列"){ Queue[name].send_message content }
34
+ end
35
+
36
+ desc "peek [queue]", "从[queue]队列中peek消息"
37
+ def peek(name)
38
+ execute("Peek 队列#{name}中的消息"){ Queue[name].peek_message }
39
+ end
40
+
41
+ private
42
+ def execute info=nil
43
+ begin
44
+ puts info
45
+ result = yield()
46
+ puts result
47
+ rescue RequestException => ex
48
+ puts "#{ex['Code']}: #{ex['Message']}"
49
+ end
50
+ puts "\n"
51
+ end
52
+
53
+ end
54
+ end