rodent 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 337e4f2fc38ca7b6be8c28ee796a13583f280e8f
4
- data.tar.gz: 96dbe13907fb4d55a692281a438cce0bbc617cf8
3
+ metadata.gz: 242d93035344222686b1756ce592417080f9631b
4
+ data.tar.gz: 40812bd7ee47172f5f3b8785e3f6ed42510fef9c
5
5
  SHA512:
6
- metadata.gz: e816bbd6e3b597a247f744e9aa60857b7e43631da59207a348f51b379a2195892b2b0010739923958cdd02024dcac26e452a7fd4bd55f246c17db07677c4ea73
7
- data.tar.gz: e14d6ae70fc3bbeedcb913452a74c631fc3411ca51fb0f3502fcc66e5771bae5f1807a468e4c7cdaeb32952b8c3c2db09c7b75f5a43b115bb03a4d1b108af044
6
+ metadata.gz: f86582528021e17f9754a258c20c74cdb1a55772b29f1d1d07f53de080c94c91698e25431f6d5f8dc5f3bde7508021adbac595f10dcf1445f4162f04e2af9fed
7
+ data.tar.gz: 1b50d4dfa8bb5ba18a5553440806f4388789fa3417c229bcd05c0a2b4e938b125b86a84950e6053fec89d6a05af163160ef17f27bf9d7bf7f6c5d2bd570c1682
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  Gemfile.lock
2
+ log/*.log
3
+ *.gem
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup :default, :test
4
+
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+
8
+ #Goliath.run_app_on_exit = false
9
+
10
+ task default: :spec
11
+
12
+ desc 'Run all specs'
13
+ RSpec::Core::RakeTask.new(:spec)
14
+
15
+ desc 'Run RSpec with code coverage'
16
+ task :cov do
17
+ ENV['COVERAGE'] ||= 'true'
18
+ Rake::Task['spec'].invoke
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'rodent'
2
+
3
+ class Customer
4
+ attr_accessor :name, :email
5
+
6
+ def initialize(options)
7
+ @name = options['name']
8
+ @email = options['email']
9
+ end
10
+
11
+ def as_json
12
+ {name: name, email: email}
13
+ end
14
+ end
15
+
16
+ class CustomersAPI < Rodent::Base
17
+ listen 'customers.create' do
18
+ self.status = 201
19
+ self.headers['API-Version'] = 'v1'
20
+
21
+ @customer = Customer.new(params)
22
+ @customer.as_json
23
+ end
24
+ end
data/examples/proxy.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rodent'
2
+
3
+ class ProxyApp < Goliath::API
4
+ plugin Rodent::Goliath::Plugin
5
+ use Rodent::Goliath::Middleware
6
+ use Goliath::Rack::Params
7
+
8
+ def response(env)
9
+ [201, {'Rodent-Proxy' => 'customers.create'}, MultiJson.dump(params)]
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  require 'bson'
2
+ require 'json'
2
3
  require 'multi_json'
3
4
  require 'amqp'
4
5
  require 'goliath'
@@ -19,7 +20,8 @@ module Rodent
19
20
  downstream_callback = proc do |status, headers, response|
20
21
  result_response = [status, headers, response]
21
22
  if proxy_type = headers.delete(@header_proxy_name)
22
- result_response = safely(env) { proxy_request(env, proxy_type, response.body.join, headers) }
23
+ body = response.respond_to?(:body) ? response.body.join : response
24
+ result_response = safely(env) { proxy_request(env, proxy_type, body, headers) }
23
25
  end
24
26
  async_callback.call(result_response) unless result_response == ::Goliath::Connection::AsyncResponse
25
27
  end
@@ -41,8 +43,8 @@ module Rodent
41
43
  consumer.consume do
42
44
  consumer.on_delivery do |metadata, payload|
43
45
  response = MultiJson.load(payload)
44
- response['Content-Length'] = response['body'].length.to_s
45
- response['Content-Type'] = 'application/json'
46
+ response['headers']['Content-Length'] = response['body'].length.to_s
47
+ response['headers']['Content-Type'] = 'application/json'
46
48
  async_callback.call([response['status'], headers.merge(response['headers']), response['body']])
47
49
  metadata.ack
48
50
  consumer.cancel
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require 'multi_json'
2
3
  require 'amqp'
3
4
 
@@ -17,15 +18,15 @@ module Rodent
17
18
  queue = channel.queue(@type, exclusive: true, auto_delete: true)
18
19
  queue.bind(channel.direct('rodent.requests'), routing_key: @type)
19
20
  queue.subscribe(ack: true) do |metadata, payload|
20
- self.params = MultiJson.load(payload)
21
- self.body = call
21
+ self.body = call(MultiJson.load(payload))
22
22
  channel.default_exchange.publish(MultiJson.dump(response), routing_key: metadata.reply_to, correlation_id: metadata.message_id)
23
23
  metadata.ack
24
24
  end
25
25
  end
26
26
  end
27
27
 
28
- def call
28
+ def call(params = {})
29
+ self.params = params
29
30
  self.status = 200
30
31
  self.headers = {}
31
32
  unless respond_to?(method_name)
@@ -1,3 +1,3 @@
1
1
  module Rodent
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/rodent.gemspec CHANGED
@@ -21,6 +21,13 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'goliath'
22
22
  s.add_dependency 'amqp'
23
23
  s.add_dependency 'em-synchrony'
24
+ s.add_dependency 'bson_ext'
25
+
26
+ s.add_development_dependency 'rake'
27
+ s.add_development_dependency 'rspec'
28
+ s.add_development_dependency 'em-http-request'
29
+ s.add_development_dependency 'rack-test'
30
+ s.add_development_dependency 'evented-spec'
24
31
 
25
32
  s.files = `git ls-files`.split("\n")
26
33
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../', 'examples/customers')
3
+
4
+ describe Rodent::Base do
5
+ include EventedSpec::AMQPSpec
6
+ include EventedSpec::SpecHelper
7
+
8
+ em_before { AMQP.cleanup_state }
9
+ em_after { AMQP.cleanup_state }
10
+
11
+ default_options({host: 'localhost', port: 5672})
12
+ default_timeout(10)
13
+
14
+ amqp_before do
15
+ @channel = AMQP::Channel.new
16
+ @channel.should be_open
17
+
18
+ @message_id = BSON::ObjectId.new.to_s
19
+
20
+ @replies_queue = @channel.queue(@message_id, exclusive: true, auto_delete: true)
21
+
22
+ @consumer = AMQP::Consumer.new(@channel, @replies_queue)
23
+
24
+ @consumer.consume do
25
+ @consumer.on_delivery do |metadata, payload|
26
+ @response = MultiJson.load(payload)
27
+ metadata.ack
28
+ @consumer.cancel
29
+ end
30
+ end
31
+
32
+ CustomersAPI.bind
33
+ end
34
+
35
+ after(:all) do
36
+ AMQP.cleanup_state
37
+ done
38
+ end
39
+
40
+ it 'should return right response' do
41
+ params = {name: 'Bob Marley', email: 'bob@example.com'}
42
+
43
+ @channel.direct('rodent.requests').publish(MultiJson.dump(params), routing_key: 'customers.create', message_id: @message_id, reply_to: @replies_queue.name)
44
+
45
+ done(0.5) do
46
+ @response.should_not be_nil
47
+ @response['status'].should == 201
48
+ @response['headers'].should == {'API-Version' => 'v1'}
49
+ @response['body'].should_not be_nil
50
+ MultiJson.load(@response['body']).should == {'name' => 'Bob Marley', 'email' => 'bob@example.com'}
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../', 'examples/proxy')
3
+
4
+ describe Rodent::Goliath::Middleware do
5
+ include EventedSpec::AMQPSpec
6
+ include EventedSpec::SpecHelper
7
+
8
+ em_before { AMQP.cleanup_state }
9
+ em_after { AMQP.cleanup_state }
10
+
11
+ default_options({host: 'localhost', port: 5672})
12
+ default_timeout(10)
13
+
14
+ amqp_before do
15
+ @channel = AMQP::Channel.new
16
+ @channel.should be_open
17
+
18
+ @queue = @channel.queue('customers.create', exclusive: true, auto_delete: true)
19
+ @queue.bind(@channel.direct('rodent.requests'), routing_key: 'customers.create')
20
+ @queue.subscribe(ack: true) do |metadata, payload|
21
+ json = {status: 201, headers: {}, body: payload}
22
+ @channel.default_exchange.publish(MultiJson.dump(json), routing_key: metadata.reply_to, correlation_id: metadata.message_id)
23
+ metadata.ack
24
+ end
25
+ end
26
+
27
+ after(:all) do
28
+ AMQP.cleanup_state
29
+ done
30
+ end
31
+
32
+ it 'should return right response' do
33
+ with_api(ProxyApp) do
34
+ params = {name: 'John Snow', email: 'john@example.com'}
35
+ post_request({path: '/customers', body: params}) do |req|
36
+ done do
37
+ req.response_header.status.should == 201
38
+ req.response_header['CONTENT_LENGTH'].should == MultiJson.dump(params).length.to_s
39
+ req.response_header['CONTENT_TYPE'].should == 'application/json'
40
+ body = JSON.parse(req.response)
41
+ body.should == {'name' => 'John Snow', 'email' => 'john@example.com'}
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:default, :test)
4
+
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'goliath/test_helper'
8
+ require 'evented-spec'
9
+
10
+ Goliath.env = :test
11
+
12
+ RSpec.configure do |config|
13
+ config.include Rack::Test::Methods
14
+ config.include Goliath::TestHelper
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Maschenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-07 00:00:00.000000000 Z
11
+ date: 2013-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -66,6 +66,90 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bson_ext
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: em-http-request
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-test
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: evented-spec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
69
153
  description: Framework for micro services
70
154
  email:
71
155
  - artem.maschenko@gmail.com
@@ -76,6 +160,9 @@ files:
76
160
  - .gitignore
77
161
  - Gemfile
78
162
  - LICENSE
163
+ - Rakefile
164
+ - examples/customers.rb
165
+ - examples/proxy.rb
79
166
  - lib/rodent.rb
80
167
  - lib/rodent/base.rb
81
168
  - lib/rodent/goliath/middleware.rb
@@ -85,6 +172,9 @@ files:
85
172
  - lib/rodent/test_helpers.rb
86
173
  - lib/rodent/version.rb
87
174
  - rodent.gemspec
175
+ - spec/rodent/base_spec.rb
176
+ - spec/rodent/middleware_spec.rb
177
+ - spec/spec_helper.rb
88
178
  homepage: https://github.com/kkdoo/rodent
89
179
  licenses:
90
180
  - MIT
@@ -109,5 +199,8 @@ rubygems_version: 2.0.3
109
199
  signing_key:
110
200
  specification_version: 4
111
201
  summary: Framework for micro services
112
- test_files: []
202
+ test_files:
203
+ - spec/rodent/base_spec.rb
204
+ - spec/rodent/middleware_spec.rb
205
+ - spec/spec_helper.rb
113
206
  has_rdoc: