magellan-rails 0.0.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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/Gemfile +7 -0
  4. data/Gemfile.lock +76 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +29 -0
  7. data/Rakefile +7 -0
  8. data/bin/magellan-rails +7 -0
  9. data/gems/magellan-publisher/.gitignore +37 -0
  10. data/gems/magellan-publisher/Gemfile +11 -0
  11. data/gems/magellan-publisher/Gemfile.lock +51 -0
  12. data/gems/magellan-publisher/LICENSE.txt +22 -0
  13. data/gems/magellan-publisher/README.md +29 -0
  14. data/gems/magellan-publisher/Rakefile +14 -0
  15. data/gems/magellan-publisher/lib/magellan/publisher/version.rb +5 -0
  16. data/gems/magellan-publisher/lib/magellan/publisher.rb +37 -0
  17. data/gems/magellan-publisher/magellan-publisher.gemspec +27 -0
  18. data/gems/magellan-publisher/spec/magellan/publisher_spec.rb +39 -0
  19. data/gems/magellan-publisher/spec/spec_helper.rb +8 -0
  20. data/lib/generators/install_generator.rb +20 -0
  21. data/lib/generators/templates/Magellan.yml +115 -0
  22. data/lib/magellan/extentions/rails/engine.rb +28 -0
  23. data/lib/magellan/extentions/rails.rb +2 -0
  24. data/lib/magellan/extentions.rb +2 -0
  25. data/lib/magellan/rails/executor.rb +35 -0
  26. data/lib/magellan/rails/railtie.rb +12 -0
  27. data/lib/magellan/rails/request.rb +111 -0
  28. data/lib/magellan/rails/response.rb +35 -0
  29. data/lib/magellan/rails/version.rb +5 -0
  30. data/lib/magellan/rails.rb +10 -0
  31. data/lib/magellan/subscriber/base.rb +26 -0
  32. data/lib/magellan/subscriber/executor.rb +21 -0
  33. data/lib/magellan/subscriber/mapper.rb +105 -0
  34. data/lib/magellan/subscriber/request.rb +25 -0
  35. data/lib/magellan/subscriber/rspec.rb +7 -0
  36. data/lib/magellan/subscriber/testing/integration.rb +22 -0
  37. data/lib/magellan/subscriber.rb +10 -0
  38. data/lib/magellan/worker/config.rb +33 -0
  39. data/lib/magellan/worker/core.rb +70 -0
  40. data/lib/magellan/worker/executor.rb +38 -0
  41. data/lib/magellan/worker.rb +14 -0
  42. data/lib/magellan.rb +28 -0
  43. data/magellan-rails.gemspec +28 -0
  44. data/spec/magellan/rails/request_spec.rb +103 -0
  45. data/spec/magellan/rails/response_spec.rb +58 -0
  46. data/spec/magellan/subscriber/mapper_spec.rb +264 -0
  47. data/spec/magellan/worker/core_spec.rb +70 -0
  48. data/spec/spec_helper.rb +4 -0
  49. metadata +167 -0
@@ -0,0 +1,58 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "spec_helper"
4
+ require 'json'
5
+ require 'base64'
6
+
7
+ describe Magellan::Rails::Request do
8
+ describe '#parse_rack_response(response)' do
9
+ let(:response){ Magellan::Rails::Response.new }
10
+ subject { response }
11
+ before(:all) do
12
+ @response_data = [200, [], StringIO.new("Hello! Magellan!")]
13
+ end
14
+ it do
15
+ response.parse_rack_response(@response_data)
16
+ expect(response.status).to eq(200)
17
+ expect(response.headers).to eq([])
18
+ expect(response.body).to eq("Hello! Magellan!")
19
+ expect(response.body_encoding).to eq(:plain)
20
+ end
21
+ end
22
+
23
+ describe "#to_message" do
24
+ context "plain response" do
25
+ let(:response){ Magellan::Rails::Response.new }
26
+ subject { response }
27
+ before(:all) do
28
+ @response_body = "Hello! Magellan!"
29
+ @response_data = [200, [], StringIO.new(@response_body)]
30
+ end
31
+ it do
32
+ response.parse_rack_response(@response_data)
33
+ data = JSON.parse(response.to_message)
34
+ expect(data["status"]).to eq(200)
35
+ expect(data["headers"]).to eq([])
36
+ expect(data["body"]).to eq("Hello! Magellan!")
37
+ expect(data["body_encoding"]).to eq("plain")
38
+ end
39
+ end
40
+
41
+ context "binary response" do
42
+ let(:response){ Magellan::Rails::Response.new }
43
+ subject { response }
44
+ before(:all) do
45
+ @response_body = "\xff"
46
+ @response_data = [200, [], StringIO.new(@response_body)]
47
+ end
48
+ it do
49
+ response.parse_rack_response(@response_data)
50
+ data = JSON.parse(response.to_message)
51
+ expect(data["status"]).to eq(200)
52
+ expect(data["headers"]).to eq([])
53
+ expect(data["body"]).to eq(Base64.strict_encode64(@response_body))
54
+ expect(data["body_encoding"]).to eq("base64")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,264 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ require "magellan/subscriber"
6
+ require "magellan/subscriber/mapper"
7
+
8
+ describe Magellan::Subscriber::Mapper do
9
+
10
+ describe "#map_action" do
11
+ before do
12
+ @mapper = Magellan::Subscriber::Mapper.new(nil)
13
+ end
14
+ context "topic: a" do
15
+ before do
16
+ @mapper.map = { "a" => "books#index" }
17
+ end
18
+ context "matches" do
19
+ [ "a" ].each do |word|
20
+ it "'#{word}'" do
21
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
22
+ end
23
+ end
24
+ end
25
+ context "doesn't match" do
26
+ [ "/a", "a/", "b" ].each do |word|
27
+ it "'#{word}'" do
28
+ expect(@mapper.map_action(word)).to be_empty
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ context "topic: a/b" do
35
+ before do
36
+ @mapper.map = { "a/b" => "books#index" }
37
+ end
38
+ context "matches" do
39
+ it "'a/b'" do
40
+ expect(@mapper.map_action("a/b")).to eql([["BookSubscriber", "index"]])
41
+ end
42
+ end
43
+ context "doesn't match" do
44
+ [ "/a/b", "a/b/", "a/c", "c/b" ].each do |word|
45
+ it "'#{word}'" do
46
+ expect(@mapper.map_action(word)).to be_empty
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ context "topic: /a" do
53
+ before do
54
+ @mapper.map = { "/a" => "books#index" }
55
+ end
56
+ context "matches" do
57
+ it "'/a'" do
58
+ expect(@mapper.map_action("/a")).to eql([["BookSubscriber", "index"]])
59
+ end
60
+ end
61
+ context "doesn't match" do
62
+ [ "a", "/a/b", "a/b" ].each do |word|
63
+ it "'#{word}'" do
64
+ expect(@mapper.map_action(word)).to be_empty
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ context "topic: a/" do
71
+ before do
72
+ @mapper.map = { "a/" => "books#index" }
73
+ end
74
+ context "matches" do
75
+ it "'a/'" do
76
+ expect(@mapper.map_action("a/")).to eql([["BookSubscriber", "index"]])
77
+ end
78
+ end
79
+ context "doesn't match" do
80
+ [ "a", "b/a/", "/a/" ].each do |word|
81
+ it "'#{word}'" do
82
+ expect(@mapper.map_action(word)).to be_empty
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ context "topic +" do
89
+ before do
90
+ @mapper.map = { "+" => "books#index" }
91
+ end
92
+ context "matches" do
93
+ [ "a", "b" ].each do |word|
94
+ it "'#{word}'" do
95
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
96
+ end
97
+ end
98
+ end
99
+ context "doesn't match" do
100
+ [ "/a", "a/b", "a/" ].each do |word|
101
+ it "'#{word}'" do
102
+ expect(@mapper.map_action(word)).to be_empty
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ context "topic +/a" do
109
+ before do
110
+ @mapper.map = { "+/a" => "books#index" }
111
+ end
112
+ context "matches" do
113
+ [ "a/a", "b/a" ].each do |word|
114
+ it "'#{word}'" do
115
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
116
+ end
117
+ end
118
+ end
119
+ context "doesn't match" do
120
+ [ "/b/a", "b/a/", "b/b", "c/b/a" ].each do |word|
121
+ it "'#{word}'" do
122
+ expect(@mapper.map_action(word)).to be_empty
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ context "topic a/+" do
129
+ before do
130
+ @mapper.map = { "a/+" => "books#index" }
131
+ end
132
+ context "matches" do
133
+ [ "a/a", "a/b" ].each do |word|
134
+ it "'#{word}'" do
135
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
136
+ end
137
+ end
138
+ end
139
+ context "doesn't match" do
140
+ [ "/a/b", "a/b/", "b/b", "a/b/c" ].each do |word|
141
+ it "'#{word}'" do
142
+ expect(@mapper.map_action(word)).to be_empty
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ context "topic a/+/b" do
149
+ before do
150
+ @mapper.map = { "a/+/b" => "books#index" }
151
+ end
152
+ context "matches" do
153
+ [ "a/a/b", "a/c/b" ].each do |word|
154
+ it "'#{word}'" do
155
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
156
+ end
157
+ end
158
+ end
159
+ context "doesn't match" do
160
+ [ "/a/c/b", "a/c/b/", "a/c/d/b", "a/b/c" ].each do |word|
161
+ it "'#{word}'" do
162
+ expect(@mapper.map_action(word)).to be_empty
163
+ end
164
+ end
165
+ end
166
+ end
167
+
168
+ context "topic #" do
169
+ before do
170
+ @mapper.map = { "#" => "books#index" }
171
+ end
172
+ context "matches" do
173
+ [ "a", "/a", "a/", "/", "a/b", "a/b/c", "/a/b/c/" ].each do |word|
174
+ it "'#{word}'" do
175
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ context "topic a/#" do
182
+ before do
183
+ @mapper.map = { "a/#" => "books#index" }
184
+ end
185
+ context "matches" do
186
+ [ "a", "a/b", "a/b/c", "a/", "a/b/" ].each do |word|
187
+ it "'#{word}'" do
188
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
189
+ end
190
+ end
191
+ end
192
+ context "doesn't match" do
193
+ [ "/a", "/a/b", "b/a", "b" ].each do |word|
194
+ it "'#{word}'" do
195
+ expect(@mapper.map_action(word)).to be_empty
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ context "topic a/+/#" do
202
+ before do
203
+ @mapper.map = { "a/+/#" => "books#index" }
204
+ end
205
+ context "matches" do
206
+ [ "a/b", "a/b/", "a/b/c", "a/b/c/d" ].each do |word|
207
+ it "'#{word}'" do
208
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
209
+ end
210
+ end
211
+ end
212
+ context "doesn't match" do
213
+ [ "/a", "/a/b", "b/a", "b/a/c" ].each do |word|
214
+ it "'#{word}'" do
215
+ expect(@mapper.map_action(word)).to be_empty
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ describe "MQTT の仕様にはないパターン" do
222
+ context "topic a/#/b" do
223
+ before do
224
+ @mapper.map = { "a/#/b" => "books#index" }
225
+ end
226
+ context "matches" do
227
+ [ "a/b", "a/c/b", "a/c/d/b" ].each do |word|
228
+ it "'#{word}'" do
229
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
230
+ end
231
+ end
232
+ end
233
+ context "doesn't match" do
234
+ [ "/a/b", "a/b/", "b/a", "a/b/c" ].each do |word|
235
+ it "'#{word}'" do
236
+ expect(@mapper.map_action(word)).to be_empty
237
+ end
238
+ end
239
+ end
240
+ end
241
+
242
+ context "topic #/a" do
243
+ before do
244
+ @mapper.map = { "#/a" => "books#index" }
245
+ end
246
+ context "matches" do
247
+ [ "a", "/a", "b/a", "/b/a", "c/b/a" ].each do |word|
248
+ it "'#{word}'" do
249
+ expect(@mapper.map_action(word)).to eql([["BookSubscriber", "index"]])
250
+ end
251
+ end
252
+ end
253
+ context "doesn't match" do
254
+ [ "b/a/", "a/b", "b" ].each do |word|
255
+ it "'#{word}'" do
256
+ expect(@mapper.map_action(word)).to be_empty
257
+ end
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ end
264
+ end
@@ -0,0 +1,70 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+ require 'bunny'
5
+ require 'bunny_mock'
6
+ require 'json'
7
+
8
+ describe Magellan::Worker::Core do
9
+
10
+ describe "#run" do
11
+
12
+ context "set all env" do
13
+ before do
14
+ # テスト用のワーカー実行要求用メッセージ
15
+ @request_message = {
16
+ 'headers' => {
17
+ 'Title' => 'groovenauts-app',
18
+ 'Interface-Version' => '1.0.0',
19
+ 'Title-Runtime-Version'=> '1.0.0',
20
+ 'Reference-Id' =>'192.168.1.110',
21
+ 'Url' =>'http://magellan-clouds.com/worker/test',
22
+ 'Oauth-Requester-Id' =>'tarou',
23
+ 'Content-Type' =>'application/json',
24
+ },
25
+ 'body' => '',
26
+ 'options' => {
27
+ 'reply_ttl' => 1000
28
+ }
29
+ }
30
+
31
+ bunny = BunnyMock.new
32
+ channel = bunny.create_channel
33
+ exchange = bunny.exchange('test_exchange', type: :direct, durable: false, auto_delete: false)
34
+ queue = channel.queue("consumer1.sample_project.1.0.0.rails")
35
+ queue.bind(exchange)
36
+ exchange.publish(@request_message.to_json, routing_key: "1.0.0.rails", reply_to: 'reply_test', correlation_id: "correlation_id_sample")
37
+
38
+ allow(Bunny ).to receive(:new).and_return(bunny)
39
+ allow(bunny ).to receive(:create_channel).and_return(channel)
40
+ allow(channel).to receive(:queue).with('customer1.sample_project.1.0.0.rails', no_declare: true).and_return(queue)
41
+ allow(channel).to receive(:basic_ack).and_return(queue)
42
+
43
+ config = {}
44
+ config[:host] = '127.0.0.1'
45
+ config[:port] = '5672'
46
+ config[:vhost] = '/customer1.sample_project'
47
+ config[:rabbitmq_user] = 'customer1.sample_project'
48
+ config[:request_queue] = 'customer1.sample_project.1.0.0.rails'
49
+ config[:response_exchange] = 'customer1.sample_project.reply'
50
+ config[:rabbitmq_password] = 'pswd1'
51
+
52
+ allow(Magellan::Worker::Config).to receive(:load_config).and_return(config)
53
+
54
+ @executor = double('executor')
55
+ allow(Magellan::Worker::Executor).to receive(:new).and_return(@executor)
56
+ allow(@executor).to receive(:execute).and_return(nil)
57
+ end
58
+
59
+ subject{ Magellan::Worker::Core.new.initialize! }
60
+ it do
61
+ expect(Magellan::Worker::Config).to receive(:load_config).once
62
+ expect(@executor ).to receive(:execute).with('reply_test', 'correlation_id_sample', '1', @request_message).once
63
+ subject.run
64
+ expect(Magellan.logger ).to be_a(Logger)
65
+ end
66
+ end
67
+
68
+
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'magellan/worker'
3
+ require 'magellan/rails'
4
+ require 'magellan/subscriber'
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magellan-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuuki Noguchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bunny
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: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - otonakaoru@gmail.com
86
+ executables:
87
+ - magellan-rails
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/magellan-rails
98
+ - gems/magellan-publisher/.gitignore
99
+ - gems/magellan-publisher/Gemfile
100
+ - gems/magellan-publisher/Gemfile.lock
101
+ - gems/magellan-publisher/LICENSE.txt
102
+ - gems/magellan-publisher/README.md
103
+ - gems/magellan-publisher/Rakefile
104
+ - gems/magellan-publisher/lib/magellan/publisher.rb
105
+ - gems/magellan-publisher/lib/magellan/publisher/version.rb
106
+ - gems/magellan-publisher/magellan-publisher.gemspec
107
+ - gems/magellan-publisher/spec/magellan/publisher_spec.rb
108
+ - gems/magellan-publisher/spec/spec_helper.rb
109
+ - lib/generators/install_generator.rb
110
+ - lib/generators/templates/Magellan.yml
111
+ - lib/magellan.rb
112
+ - lib/magellan/extentions.rb
113
+ - lib/magellan/extentions/rails.rb
114
+ - lib/magellan/extentions/rails/engine.rb
115
+ - lib/magellan/rails.rb
116
+ - lib/magellan/rails/executor.rb
117
+ - lib/magellan/rails/railtie.rb
118
+ - lib/magellan/rails/request.rb
119
+ - lib/magellan/rails/response.rb
120
+ - lib/magellan/rails/version.rb
121
+ - lib/magellan/subscriber.rb
122
+ - lib/magellan/subscriber/base.rb
123
+ - lib/magellan/subscriber/executor.rb
124
+ - lib/magellan/subscriber/mapper.rb
125
+ - lib/magellan/subscriber/request.rb
126
+ - lib/magellan/subscriber/rspec.rb
127
+ - lib/magellan/subscriber/testing/integration.rb
128
+ - lib/magellan/worker.rb
129
+ - lib/magellan/worker/config.rb
130
+ - lib/magellan/worker/core.rb
131
+ - lib/magellan/worker/executor.rb
132
+ - magellan-rails.gemspec
133
+ - spec/magellan/rails/request_spec.rb
134
+ - spec/magellan/rails/response_spec.rb
135
+ - spec/magellan/subscriber/mapper_spec.rb
136
+ - spec/magellan/worker/core_spec.rb
137
+ - spec/spec_helper.rb
138
+ homepage: ''
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.2.2
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: ''
162
+ test_files:
163
+ - spec/magellan/rails/request_spec.rb
164
+ - spec/magellan/rails/response_spec.rb
165
+ - spec/magellan/subscriber/mapper_spec.rb
166
+ - spec/magellan/worker/core_spec.rb
167
+ - spec/spec_helper.rb