json_rpc_middleware 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +21 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/json_rpc_middleware.gemspec +27 -0
- data/lib/json_rpc.rb +9 -0
- data/lib/json_rpc/handler.rb +76 -0
- data/lib/json_rpc/invalid_parameters_error.rb +5 -0
- data/lib/json_rpc/version.rb +3 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f15c8cdb1dd108a3c3bca83931a7e28d29384afb
|
4
|
+
data.tar.gz: 9fbac518cf03e65549d17886b09c45ec4f8e589f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0a54ebd477e1f5bb7f0363552bc95cb1b90587398a7b717cc374927a054363d289b667bab616ac8e123283b5c99769dfdee9c5679dff16e629f6b976809fd17
|
7
|
+
data.tar.gz: 5067754e9483531460c17779ff1a043edef02ba5d3e597de646a91b10bba1727f0e593170015f93e5cc2301a4b582d6bebe73241b390d74d8817ebb8dd85faad
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 tbueno
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# JSON RPC Middleware
|
2
|
+
|
3
|
+
This is a simple JSON-RPC middleware to help you creating sinatra micro-services
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
TODO: Write usage instructions here
|
9
|
+
|
10
|
+
## Development
|
11
|
+
|
12
|
+
|
13
|
+
## Contributing
|
14
|
+
|
15
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/allyapp/json_rpc_middleware
|
16
|
+
|
17
|
+
|
18
|
+
## License
|
19
|
+
|
20
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
21
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "json_rpc_middleware"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'json_rpc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json_rpc_middleware"
|
8
|
+
spec.version = JsonRpc::VERSION
|
9
|
+
spec.authors = ["tbueno"]
|
10
|
+
spec.email = ["developers@allyapp.com.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{JSON-RPC Sinatra middleware}
|
13
|
+
spec.description = %q{Middleware to handle json-rpc methods in Sinatra apps}
|
14
|
+
spec.homepage = "https://github.com/allyapp/json_rpc_middleware"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
24
|
+
spec.add_development_dependency "sinatra", "~> 1.4"
|
25
|
+
spec.add_development_dependency "rack-test", "~> 0.6"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
data/lib/json_rpc.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative "invalid_parameters_error"
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module JsonRpc
|
5
|
+
class Handler
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
@env = env
|
12
|
+
unless valid_rpc_method?
|
13
|
+
status, headers, body = @app.call(env)
|
14
|
+
return [status, headers, body]
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
request_object
|
19
|
+
@env["rack.logger"].info(request_object)
|
20
|
+
@app.send request_method, request_params, request_id
|
21
|
+
rescue NoMethodError
|
22
|
+
raise if @app.available_methods.include?(request_method.to_sym)
|
23
|
+
error request_id, -32_601, "Method not found"
|
24
|
+
rescue JsonRpc::InvalidParametersError
|
25
|
+
error request_id, -32_700, "Invalid params"
|
26
|
+
rescue JSON::ParserError
|
27
|
+
error nil, -32_700, "Parse error"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_with_error(request_id:, code:, message:)
|
32
|
+
error = {
|
33
|
+
id: request_id,
|
34
|
+
jsonrpc: "2.0",
|
35
|
+
error: {
|
36
|
+
code: code,
|
37
|
+
message: message
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
[200, { "Content-type" => "application/json" }, error.to_json]
|
42
|
+
end
|
43
|
+
|
44
|
+
def error(id, code, message)
|
45
|
+
respond_with_error request_id: id, code: code, message: message
|
46
|
+
end
|
47
|
+
|
48
|
+
def request_object
|
49
|
+
parse_request_object
|
50
|
+
end
|
51
|
+
|
52
|
+
def valid_rpc_method?
|
53
|
+
@env["REQUEST_METHOD"] == "POST" && @env["PATH_INFO"] == "/api"
|
54
|
+
end
|
55
|
+
|
56
|
+
def request_method
|
57
|
+
request_object["method"]
|
58
|
+
end
|
59
|
+
|
60
|
+
def request_params
|
61
|
+
request_object.fetch("params") do
|
62
|
+
{}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def request_id
|
67
|
+
request_object.fetch("id")
|
68
|
+
end
|
69
|
+
|
70
|
+
def parse_request_object
|
71
|
+
@env["rack.input"].rewind
|
72
|
+
JSON[@env["rack.input"].read]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_rpc_middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tbueno
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sinatra
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description: Middleware to handle json-rpc methods in Sinatra apps
|
84
|
+
email:
|
85
|
+
- developers@allyapp.com.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- json_rpc_middleware.gemspec
|
99
|
+
- lib/json_rpc.rb
|
100
|
+
- lib/json_rpc/handler.rb
|
101
|
+
- lib/json_rpc/invalid_parameters_error.rb
|
102
|
+
- lib/json_rpc/version.rb
|
103
|
+
homepage: https://github.com/allyapp/json_rpc_middleware
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.4.5.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: JSON-RPC Sinatra middleware
|
127
|
+
test_files: []
|