json-rpc2rest 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/json-rpc2rest.rb +50 -0
- data/spec/json-rpc2rest_spec.rb +85 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0dfebc592bfacf40c0991ebe2b6683f50c6a70e2
|
4
|
+
data.tar.gz: 00c4a5615992acbfa2caf6e6474d74ce514ab067
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6da9431dddee9f6fd7951ba2357fe251fd0f717ef7f0bbabadfae8be4b533a668cf78dfebaf155ae75f205c18df43a09774d375cef908a0988351c0e8f47c2c0
|
7
|
+
data.tar.gz: 4f34e693b794727ebae17cb6d523159d46c8693b0ca49d01c6cc13baa6de29db8f1fe61ba402f02ae47dfe9b7893acea981c85547321eb6737107b662030e388
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class JsonRpc2Rest
|
4
|
+
def initialize(app, options={})
|
5
|
+
@app = app
|
6
|
+
@field = options[:field] || 'method'
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
init_call(env)
|
11
|
+
|
12
|
+
change_path(new_path) if json_rpc_present?
|
13
|
+
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def init_call(env)
|
20
|
+
@env = env
|
21
|
+
@path = nil
|
22
|
+
|
23
|
+
req_body = Rack::Request.new(env).body.read
|
24
|
+
if env['REQUEST_METHOD'] == 'POST' && req_body.present?
|
25
|
+
# @params = @env['rack.request.form_hash']
|
26
|
+
@params = JSON.parse(req_body)
|
27
|
+
else
|
28
|
+
@params ||= {}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def json_rpc_present?
|
33
|
+
@params && @params[@field]
|
34
|
+
end
|
35
|
+
|
36
|
+
def path
|
37
|
+
@path ||= @env['PATH_INFO'].chomp('/')
|
38
|
+
end
|
39
|
+
|
40
|
+
def new_path
|
41
|
+
path << '/' + @params[@field]
|
42
|
+
end
|
43
|
+
|
44
|
+
def change_path(path)
|
45
|
+
original_path = @env['PATH_INFO']
|
46
|
+
@env['PATH_INFO'] = path
|
47
|
+
@env['REQUEST_URI'].sub!(original_path, path)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'json-rpc2rest'
|
3
|
+
|
4
|
+
shared_examples_for 'change env' do
|
5
|
+
it 'should change PATH_INFO and REQUEST_URI' do
|
6
|
+
expect(app).to receive(:call).with(result_env)
|
7
|
+
obj.call(env)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
shared_examples_for "don't change env" do
|
12
|
+
it 'should change PATH_INFO and REQUEST_URI' do
|
13
|
+
expect(app).to receive(:call).with(env)
|
14
|
+
obj.call(env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe JsonRpc2Rest do
|
19
|
+
let(:obj) { described_class.new(app) }
|
20
|
+
let(:app) { double('app') }
|
21
|
+
|
22
|
+
it 'takes a backend and returns a middleware component' do
|
23
|
+
expect(obj).to respond_to(:call)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'takes an options Hash' do
|
27
|
+
expect { described_class.new(app, {}) }.
|
28
|
+
not_to raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when json-rpc params' do
|
32
|
+
let(:params) { {'method'=>'get_posts_list', 'data' => [], 'id'=>'2'} }
|
33
|
+
let(:env) {
|
34
|
+
{'REQUEST_METHOD' => 'POST',
|
35
|
+
'rack.request.form_hash' => params,
|
36
|
+
'PATH_INFO' => '/api',
|
37
|
+
'REQUEST_URI' => '/api?page=2'
|
38
|
+
}
|
39
|
+
}
|
40
|
+
let(:result_env) {
|
41
|
+
env.merge({
|
42
|
+
'PATH_INFO' => '/api/get_posts_list',
|
43
|
+
'REQUEST_URI' => '/api/get_posts_list?page=2'
|
44
|
+
})
|
45
|
+
}
|
46
|
+
|
47
|
+
it_should_behave_like 'change env'
|
48
|
+
|
49
|
+
context 'when PATH_INFO with trailing slash' do
|
50
|
+
let(:env) {
|
51
|
+
{'REQUEST_METHOD' => 'POST',
|
52
|
+
'rack.request.form_hash' => params,
|
53
|
+
'PATH_INFO' => '/api/',
|
54
|
+
'REQUEST_URI' => '/api/?page=2'
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
it_should_behave_like 'change env'
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when method is not defined in params' do
|
62
|
+
let(:params) { {'data' => [], 'id'=>'2'} }
|
63
|
+
|
64
|
+
it_should_behave_like "don't change env"
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when method field changed' do
|
68
|
+
let(:params) { {'requestMethod'=>'get_posts_list', 'data' => [], 'id'=>'2'} }
|
69
|
+
let(:obj) { described_class.new(app, {field: 'requestMethod'}) }
|
70
|
+
it_should_behave_like 'change env'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'when get request' do
|
75
|
+
let(:env) {
|
76
|
+
{'REQUEST_METHOD' => 'GET',
|
77
|
+
'PATH_INFO' => '/api',
|
78
|
+
'REQUEST_URI' => '/api?page=2'
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
it_should_behave_like "don't change env"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-rpc2rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ilya Lavrov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Json-Rpc to Rest middleware.
|
14
|
+
email: dzjuck@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/json-rpc2rest.rb
|
20
|
+
- spec/json-rpc2rest_spec.rb
|
21
|
+
homepage: http://github.com/dzjuck/json-rpc2rest
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.4.6
|
42
|
+
signing_key:
|
43
|
+
specification_version: 2
|
44
|
+
summary: Json-Rpc to Rest middleware.
|
45
|
+
test_files:
|
46
|
+
- spec/json-rpc2rest_spec.rb
|