jrpc 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02a37ef8c074646479c26dd113cf7245f5989412
4
+ data.tar.gz: 337a198aa49dc1e34561cd5941e5c65f63e00c97
5
+ SHA512:
6
+ metadata.gz: 189d7c573b2373bcb16ca7c2fe8e06c18e1827fa7aaa4fef73d984f04230a75653ef93ee80785f464c227f8d0a360dbe2add18376f3ff03f267e5413aba66c39
7
+ data.tar.gz: 0355449f1477898dbf19c22ed2930b8ab239fb619948d952bdb27f48adc2ebfdeff4364bb4e60dfa61d0d8dd7621cb2abf2ecd3da9c56e6b4a84b91ebe0eb935
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jrpc.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Denis Talakevich
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.
@@ -0,0 +1,33 @@
1
+ # JRPC
2
+
3
+ JSON RPC TCP client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jrpc'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jrpc
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jrpc.
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
33
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jrpc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'jrpc'
8
+ spec.version = JRPC::VERSION
9
+ spec.authors = ['Denis Talakevich']
10
+ spec.email = ['senid231@gmail.com']
11
+
12
+ spec.summary = 'JSON RPC client'
13
+ spec.description = 'JSON RPC client over TCP'
14
+ spec.homepage = 'https://github.com/senid231/jrpc'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'netstring'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,18 @@
1
+ require 'jrpc/version'
2
+ require 'jrpc/netstring_tcp_socket'
3
+ require 'jrpc/base_client'
4
+ require 'jrpc/tcp_client'
5
+ require 'jrpc/error/error'
6
+ require 'jrpc/error/client_error'
7
+ require 'jrpc/error/server_error'
8
+ require 'jrpc/error/internal_error'
9
+ require 'jrpc/error/internal_server_error'
10
+ require 'jrpc/error/invalid_params'
11
+ require 'jrpc/error/invalid_request'
12
+ require 'jrpc/error/method_not_found'
13
+ require 'jrpc/error/parse_error'
14
+ require 'jrpc/error/unknown_error'
15
+
16
+ module JRPC
17
+ JSON_RPC_VERSION = '2.0'
18
+ end
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+ module JRPC
3
+ class BaseClient
4
+
5
+ def method_missing(method, *params)
6
+ invoke_request(method, *params)
7
+ end
8
+
9
+ def invoke_request(method, *params)
10
+ request = create_message(method, params)
11
+ id = generate_id
12
+ request['id'] = id
13
+
14
+ response = send_request request.to_json
15
+ response = JSON.parse response
16
+
17
+ validate_response(response, id)
18
+ parse_error(response['error']) if response.has_key?('error')
19
+
20
+ response['result']
21
+ end
22
+
23
+ def invoke_notification(method, *params)
24
+ send_notification create_message(method, params).to_json
25
+ nil
26
+ end
27
+
28
+ private
29
+
30
+ def validate_response(response, id)
31
+ raise ClientError.new('Wrong response structure') unless response.is_a?(Hash)
32
+ raise ClientError.new('Wrong version') if response['jsonrpc'] != JRPC::JSON_RPC_VERSION
33
+ raise ClientError.new("ID response mismatch. expected #{id.inspect} got #{response['id'].inspect}") if id != response['id']
34
+ end
35
+
36
+ def parse_error(error)
37
+ case error['code']
38
+ when -32700
39
+ raise ParseError.new(error['message'])
40
+ when -32600
41
+ raise InvalidRequest.new(error['message'])
42
+ when -32601
43
+ raise MethodNotFound.new(error['message'])
44
+ when -32602
45
+ raise InvalidParams.new(error['message'])
46
+ when -32603
47
+ raise InternalError.new(error['message'])
48
+ when -32099..-32000
49
+ raise InternalServerError.new(error['message'], error['code'])
50
+ else
51
+ raise UnknownError.new(error['message'], error['code'])
52
+ end
53
+ end
54
+
55
+ def send_request(json)
56
+ raise NotImplementedError
57
+ end
58
+
59
+ def send_notification(json)
60
+ raise NotImplementedError
61
+ end
62
+
63
+ def generate_id
64
+ (0...10).map { ('a'..'z').to_a[rand(26)] }.join
65
+ end
66
+
67
+ def create_message(method, params)
68
+ message = {
69
+ 'jsonrpc' => JSON_RPC_VERSION,
70
+ 'method' => method
71
+ }
72
+ message['params'] = params unless params.empty?
73
+ message
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ module JRPC
2
+ class ClientError < Error
3
+
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module JRPC
2
+ class Error < RuntimeError
3
+
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module JRPC
2
+ class InternalError < ServerError
3
+
4
+ def initialize(message)
5
+ super(message, -32603)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module JRPC
2
+ class InternalServerError < ServerError
3
+
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module JRPC
2
+ class InvalidParams < ServerError
3
+
4
+ def initialize(message)
5
+ super(message, -32602)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module JRPC
2
+ class InvalidRequest < ServerError
3
+
4
+ def initialize(message)
5
+ super(message, -32600)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module JRPC
2
+ class MethodNotFound < ServerError
3
+
4
+ def initialize(message)
5
+ super(message, -32601)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module JRPC
2
+ class ParseError < ServerError
3
+
4
+ def initialize(message)
5
+ super(message, -32700)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module JRPC
2
+ class ServerError < Error
3
+ attr_reader :code
4
+
5
+ def initialize(message, code)
6
+ @code = code
7
+ super(message)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module JRPC
2
+ class UnknownError < ServerError
3
+
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'socket'
2
+ require 'netstring'
3
+ module JRPC
4
+ class NetstringTcpSocket < ::TCPSocket
5
+ def send_string(request)
6
+ send Netstring.dump(request.to_s), 0
7
+ end
8
+
9
+ def receive_string
10
+ length = get_msg_length
11
+ response = recv(length)
12
+ raise ClientError.new('invalid response. missed comma as terminator') if response[-1] != ','
13
+ response.chomp(',')
14
+ end
15
+
16
+ def set_timeout(timeout)
17
+ seconds = Integer(timeout)
18
+ microseconds = Integer((timeout-seconds)*1_000_000)
19
+ packed_structure = [seconds, microseconds].pack('l_2') # structure with the number of seconds and microseconds
20
+ setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, packed_structure)
21
+ end
22
+
23
+ private
24
+
25
+ def get_msg_length
26
+ length = ''
27
+ while true do
28
+ character = recv(1)
29
+ break if character == ':'
30
+ length += character
31
+ end
32
+
33
+ Integer(length)+1
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ module JRPC
2
+ class TcpClient < BaseClient
3
+ attr_reader :hostname, :port, :namespace, :options
4
+
5
+ def initialize(options = {})
6
+ @hostname = options.delete(:hostname)
7
+ @port = options.delete(:port)
8
+ @namespace = options.delete(:namespace).to_s
9
+ @options = options
10
+ @socket = NetstringTcpSocket.new(@hostname, @port)
11
+ @socket.set_timeout @options.fetch(:timeout, 5)
12
+ end
13
+
14
+ def close
15
+ @socket.close
16
+ end
17
+
18
+
19
+ private
20
+
21
+ def create_message(method, params)
22
+ super("#{namespace}#{method}", params)
23
+ end
24
+
25
+ def send_request(request)
26
+ @socket.send_string(request)
27
+ @socket.receive_string
28
+ end
29
+
30
+ def send_notification(request)
31
+ @socket.send_string(request)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module JRPC
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jrpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Talakevich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: netstring
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: JSON RPC client over TCP
70
+ email:
71
+ - senid231@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - jrpc.gemspec
84
+ - lib/jrpc.rb
85
+ - lib/jrpc/base_client.rb
86
+ - lib/jrpc/error/client_error.rb
87
+ - lib/jrpc/error/error.rb
88
+ - lib/jrpc/error/internal_error.rb
89
+ - lib/jrpc/error/internal_server_error.rb
90
+ - lib/jrpc/error/invalid_params.rb
91
+ - lib/jrpc/error/invalid_request.rb
92
+ - lib/jrpc/error/method_not_found.rb
93
+ - lib/jrpc/error/parse_error.rb
94
+ - lib/jrpc/error/server_error.rb
95
+ - lib/jrpc/error/unknown_error.rb
96
+ - lib/jrpc/netstring_tcp_socket.rb
97
+ - lib/jrpc/tcp_client.rb
98
+ - lib/jrpc/version.rb
99
+ homepage: https://github.com/senid231/jrpc
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.8
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: JSON RPC client
123
+ test_files: []