rpc4json 0.0.1.alpha → 0.0.2.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # JSONRPC
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/rpc4json.png)](http://badge.fury.io/rb/rpc4json) [![Dependency Status](https://gemnasium.com/GRoguelon/JSONRPC.png)](https://gemnasium.com/GRoguelon/JSONRPC)
4
+
3
5
  An Ruby implementation of the JSON-RPC 2.0 specification.
4
6
 
5
7
  ## Installation
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'json'
4
+ require 'securerandom'
5
+
6
+ module JSONRPC
7
+ class AbstractRequest
8
+ attr_reader :id, :jsonrpc, :method, :params
9
+
10
+ JSONRPC_VERSION = '2.0'
11
+
12
+ class << self
13
+ private :new
14
+
15
+ def inherited(subclass)
16
+ subclass.public_class_method(:new)
17
+ end
18
+ end
19
+
20
+ def initialize(method, params = nil)
21
+ assert_valid_arguements(method, params)
22
+
23
+ @id = unique_request_id
24
+ @jsonrpc = JSONRPC_VERSION
25
+ @method = method
26
+ @params = params if params
27
+ end
28
+
29
+ def to_hash
30
+ @request ||= {
31
+ :jsonrpc => jsonrpc,
32
+ :method => method,
33
+ :params => params,
34
+ :id => id
35
+ }.delete_if { |_, value| value.nil? }
36
+ end
37
+ alias :to_h :to_hash
38
+
39
+ def to_json
40
+ JSON.dump(to_hash)
41
+ end
42
+
43
+ protected
44
+
45
+ def assert_valid_arguements(method, params)
46
+ if method.start_with?('rpc.')
47
+ raise ArgumentError, 'the method argument cannot begin by `rpc.`'
48
+ elsif params && !(params.is_a?(Hash) || params.is_a?(Array))
49
+ raise ArgumentError, 'the params argument must be an Array or a Hash.'
50
+ end
51
+ end
52
+
53
+ def unique_request_id
54
+ SecureRandom.uuid
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'net/http'
4
+ require 'securerandom'
5
+
6
+ module JSONRPC
7
+ class Client
8
+ class Proxy
9
+ def initialize(server, prefix, args = [], meth = :call, delim ='.')
10
+ @server = server
11
+ @prefix = prefix ? prefix + delim : ''
12
+ @args = args
13
+ @meth = meth
14
+ end
15
+
16
+ def method_missing(mid, *args)
17
+ pre = @prefix + mid.to_s
18
+ arg = @args + args
19
+ @server.send(@meth, pre, *arg)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,10 +1,153 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'net/http'
4
+ require 'securerandom'
5
+ require 'jsonrpc/client/proxy'
6
+ require 'jsonrpc/content_type_parser'
7
+ require 'jsonrpc/request'
8
+ require 'jsonrpc/response'
9
+
3
10
  module JSONRPC
4
11
  # This class provides a way to dial with JSON-RPC server.
5
12
  #
6
13
  # @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
7
14
  class Client
15
+ include ContentTypeParser
16
+
17
+ USER_AGENT = "JSONRPC::Client (Ruby #{RUBY_VERSION})"
18
+
19
+ # Add additional HTTP headers to the request.
20
+ attr_accessor :http_header_extra
21
+
22
+ def initialize(host = nil, path = nil, port = nil, proxy_host = nil,
23
+ proxy_port = nil, user = nil, password = nil, use_ssl = nil, timeout = nil)
24
+ @http_header_extra = nil
25
+ @http_last_response = nil
26
+ @cookie = nil
27
+
28
+ @host = host || 'localhost'
29
+ @path = path || '/RPC2'
30
+ @proxy_host = proxy_host
31
+ @proxy_port = proxy_port
32
+ @proxy_host ||= 'localhost' unless @proxy_port.nil?
33
+ @proxy_port ||= 8080 unless @proxy_host.nil?
34
+ @use_ssl = use_ssl || false
35
+ @timeout = timeout || 30
36
+
37
+ @port = if use_ssl
38
+ require 'net/https'
39
+ port || 443
40
+ else
41
+ port || 80
42
+ end
43
+
44
+ @user, @password = user, password
45
+
46
+ set_auth
47
+
48
+ # convert ports to integers
49
+ @port = @port.to_i unless @port.nil?
50
+ @proxy_port = @proxy_port.to_i unless @proxy_port.nil?
51
+
52
+ # HTTP object for synchronous calls
53
+ @http = new_http
54
+ end
55
+
56
+ def request_with_array(method, *args)
57
+ request = Request.new(method, args).to_json
58
+ result = do_rpc(request, false)
59
+ Response.from_json(result, request)
60
+ end
61
+
62
+ def notify_with_array(method, *args)
63
+ Request.new(method, args).tap do |request|
64
+ do_rpc(request.to_json, false)
65
+ end
66
+ end
67
+
68
+ def proxy(prefix = nil, *args)
69
+ Proxy.new(self, prefix, args, :request_with_array)
70
+ end
71
+
72
+ private
73
+
74
+ def do_rpc(request, async = false)
75
+ headers, response = headers(request, async), nil
76
+
77
+ if async
78
+ new_http.start {
79
+ response = http.post2(@path, request, headers)
80
+ }
81
+ else
82
+ # reuse the HTTP object for each call => connection alive is possible
83
+ # we must start connection explicitely first time so that http.request
84
+ # does not assume that we don't want keepalive
85
+ @http.start unless @http.started?
86
+
87
+ response = @http.post2(@path, request, headers)
88
+ end
89
+
90
+ @http_last_response = response
91
+
92
+ response.body.tap do |data|
93
+ if response.code == '401'
94
+ # Authorization Required
95
+ raise "Authorization failed.\nHTTP-Error: #{response.code} #{response.message}"
96
+ elsif response.code[0, 1] != "2"
97
+ raise "HTTP-Error: #{response.code} #{response.message}"
98
+ end
99
+
100
+ content_type = parse_content_type(response['Content-Type']).first
101
+ if content_type != 'application/json'
102
+ raise "Wrong content-type (received '#{content_type}' but expected 'application/json')"
103
+ end
104
+
105
+ expected = response['Content-Length'] || '<unknown>'
106
+ if data.nil? || data.bytesize == 0 || expected != '<unknown>' && expected.to_i != data.bytesize && response['Transfer-Encoding'].nil?
107
+ raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
108
+ end
109
+
110
+ set_cookies = response.get_fields('Set-Cookie')
111
+ if set_cookies && !set_cookies.empty?
112
+ require 'webrick/cookie'
113
+ @cookie = set_cookies.collect do |set_cookie|
114
+ cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
115
+ WEBrick::Cookie.new(cookie.name, cookie.value).to_s
116
+ end.join('; ')
117
+ end
118
+ end
119
+ end
120
+
121
+ def headers(request, async)
122
+ headers = {
123
+ 'User-Agent' => USER_AGENT,
124
+ 'Accept' => 'application/json',
125
+ 'Content-Type' => 'application/json; charset=utf-8',
126
+ 'Content-Length' => request.bytesize.to_s,
127
+ 'Connection' => (async ? 'close' : 'keep-alive')
128
+ }
129
+
130
+ headers['Cookie'] = @cookie if @cookie
131
+ headers.merge!(@http_header_extra) if @http_header_extra
132
+ headers['Authorization'] = @auth unless @auth.nil?
133
+ headers
134
+ end
135
+
136
+ def new_http
137
+ Net::HTTP.new(@host, @port, @proxy_host, @proxy_port).tap do |http|
138
+ http.set_debug_output($stdout)
139
+ http.use_ssl = @use_ssl if @use_ssl
140
+ http.read_timeout = @timeout
141
+ http.open_timeout = @timeout
142
+ end
143
+ end
8
144
 
145
+ def set_auth
146
+ @auth = if !@user.nil?
147
+ credentials = "#{@user}"
148
+ credentials << ":#{@password}" unless @password.nil?
149
+ @auth = 'Basic ' + [credentials].pack('m0')
150
+ end
151
+ end
9
152
  end
10
153
  end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ module JSONRPC
4
+ module ContentTypeParser
5
+ def parse_content_type(str)
6
+ a, *b = str.split(';')
7
+ return a.strip.downcase, *b
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'jsonrpc/abstract_request'
4
+
5
+ module JSONRPC
6
+ class Notification < AbstractRequest
7
+
8
+ protected
9
+
10
+ def unique_request_id
11
+ nil
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ require 'jsonrpc/abstract_request'
4
+
5
+ module JSONRPC
6
+ class Request < AbstractRequest; end
7
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'json'
4
+ require 'jsonrpc/abstract_request'
5
+
6
+ module JSONRPC
7
+ class Response
8
+ attr_reader :request, :id, :jsonrpc, :result, :error
9
+
10
+ class << self
11
+ def from_json(json, request = nil)
12
+ new(JSON.parse(json), request)
13
+ end
14
+ end
15
+
16
+ def initialize(response, request = nil)
17
+ @raw = response
18
+ @id = response['id']
19
+ @jsonrpc = response['jsonrpc']
20
+ @result = response['result']
21
+ @error = response['error']
22
+ @request = request
23
+ end
24
+
25
+ def body
26
+ success? ? result : error
27
+ end
28
+
29
+ def error?
30
+ !success?
31
+ end
32
+
33
+ def success?
34
+ @error.nil?
35
+ end
36
+ end
37
+ end
@@ -10,7 +10,7 @@ module JSONRPC
10
10
  # The minor number version.
11
11
  MINOR = 0
12
12
  # The tiny number version.
13
- TINY = 1
13
+ TINY = 2
14
14
  # The pre realese number version.
15
15
  PRE = :alpha
16
16
 
data/lib/jsonrpc.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'jsonrpc/client'
4
+ require 'jsonrpc/notification'
5
+ require 'jsonrpc/request'
6
+ require 'jsonrpc/response'
4
7
  require 'jsonrpc/version'
5
8
 
6
9
  # This is the root class of the JSONRPC gem.
data/rpc4json.gemspec CHANGED
@@ -22,13 +22,10 @@ Gem::Specification.new do |gem|
22
22
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
23
  gem.require_paths = %w{lib}
24
24
 
25
- gem.cert_chain = %w{certs/jsonrpc.pem}
26
- gem.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') if $0 =~/gem\z/
27
-
28
25
  gem.add_development_dependency 'bundler', '~> 1.5'
29
26
  gem.add_development_dependency 'rake'
30
27
 
31
28
  # Documentation
32
- gem.add_development_dependency 'yard', '~> 0.8.7.3'
29
+ gem.add_development_dependency 'yard', '~> 0.8.7.3'
33
30
  gem.add_development_dependency 'redcarpet', '~> 3.0.0'
34
31
  end
metadata CHANGED
@@ -1,46 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpc4json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.2.alpha
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
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c0bm9FdzdjQ0F3RUFBYU9CaWpDQmh6
29
- QUpCZ05WSFJNRUFqQUFNQXNHQTFVZApEd1FFQXdJRXNEQWRCZ05WSFE0RUZn
30
- UVV1b0syZDFuTnAwQW9kV3hwLzVwTnE2MS9STEl3SmdZRFZSMFJCQjh3CkhZ
31
- RWJaMlZ2Wm1aeVpYa3VjbTluZFdWc2IyNUFaMjFoYVd3dVkyOXRNQ1lHQTFV
32
- ZEVnUWZNQjJCRzJkbGIyWm0KY21WNUxuSnZaM1ZsYkc5dVFHZHRZV2xzTG1O
33
- dmJUQU5CZ2txaGtpRzl3MEJBUVVGQUFPQ0FRRUFrSGI1YkRPUgpKU1dJNlpm
34
- OCtKRkFFdGFUTmN5TWJaYStNQzFvTFg1Z2hVaXRNWFdGNlpFdGVNeGZzanh6
35
- VjVacUJuSEVHUlV5CkE3ZXBTS2ErUmNvdGZxU1BBc3gwdmt4bVl0NHo0Mm1o
36
- KzlFUXpHKzJ5cGhEZlI1ZDdvaU00UDRjVjN5ZVNPVE8KNGlVcGpya2JjNWRV
37
- VlVydVlTTUFBVFJ2WkE3YVp2Qm0zcGJEU01zYzVvTkdWdU5RTEtQU3RHeURq
38
- aWQxZHVWSwpEdmFFY2s2U1FkY2FkeVZ1Vm5sTVZoek8vSTZpdkIrOTJKbXVh
39
- TWl4ZG1OcTUzWlNzNS9HbithWHkvakZBcXhOCjdyRzNmK2N5VXcxeDgwSXZZ
40
- cWQzRytGRWxEaFRZT0Qxd1UzMzU2TlczUm1tcGxRQ1JCNXhJSzdHdGk2Uk93
41
- bG0KbTFWcDZCSFR0WnFTU3c9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0t
42
- Cg==
43
- date: 2013-12-30 00:00:00.000000000 Z
11
+ cert_chain: []
12
+ date: 2014-10-28 00:00:00.000000000 Z
44
13
  dependencies:
45
14
  - !ruby/object:Gem::Dependency
46
15
  name: bundler
@@ -117,9 +86,14 @@ files:
117
86
  - LICENSE.txt
118
87
  - README.md
119
88
  - Rakefile
120
- - certs/jsonrpc.pem
121
89
  - lib/jsonrpc.rb
90
+ - lib/jsonrpc/abstract_request.rb
122
91
  - lib/jsonrpc/client.rb
92
+ - lib/jsonrpc/client/proxy.rb
93
+ - lib/jsonrpc/content_type_parser.rb
94
+ - lib/jsonrpc/notification.rb
95
+ - lib/jsonrpc/request.rb
96
+ - lib/jsonrpc/response.rb
123
97
  - lib/jsonrpc/version.rb
124
98
  - lib/rpc4json.rb
125
99
  - rpc4json.gemspec
@@ -144,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
118
  version: 1.3.1
145
119
  requirements: []
146
120
  rubyforge_project:
147
- rubygems_version: 1.8.23
121
+ rubygems_version: 1.8.23.2
148
122
  signing_key:
149
123
  specification_version: 3
150
124
  summary: An Ruby implementation of the JSON-RPC 2.0 specification.
data/certs/jsonrpc.pem DELETED
@@ -1,22 +0,0 @@
1
- -----BEGIN CERTIFICATE-----
2
- MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRowGAYDVQQDDBFnZW9m
3
- ZnJleS5yb2d1ZWxvbjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPy
4
- LGQBGRYDY29tMB4XDTEzMTIzMDA5NTkwNFoXDTE0MTIzMDA5NTkwNFowSDEaMBgG
5
- A1UEAwwRZ2VvZmZyZXkucm9ndWVsb24xFTATBgoJkiaJk/IsZAEZFgVnbWFpbDET
6
- MBEGCgmSJomT8ixkARkWA2NvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
7
- ggEBAK0QVh/CMYfyrZYsUVXu6exazFQSAb7vRFVZ8UKRCpvPxqS1b1BmL+2sivGw
8
- IifcPOQ3OPgV24TWyb/c1b6aNJb3YDcy3VJ2Q+mAO/ZVNbku4+Qt+WgOkYsXM/q4
9
- dCbQhsi5b2DZnJ2hO1pB5dKzB/8qmH5/7AY+m+HeOJfDCVbdyU9+uDwFROQzyzzh
10
- +nkgW3/9VAotyGUK5qgbAZItQx/xo+gn2RW5oJ/VA9Uu6DT2qmHQxdeG7Dzf1ebg
11
- o8kOYllGz64q8NqrZ7IwsqjO4LNOJbSxghWD9kNRzljg/4UL4Lau8fedKIRMuNzH
12
- u81clg7riAlh7Wnp3Ax74noEw7cCAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
13
- DwQEAwIEsDAdBgNVHQ4EFgQUuoK2d1nNp0AodWxp/5pNq61/RLIwJgYDVR0RBB8w
14
- HYEbZ2VvZmZyZXkucm9ndWVsb25AZ21haWwuY29tMCYGA1UdEgQfMB2BG2dlb2Zm
15
- cmV5LnJvZ3VlbG9uQGdtYWlsLmNvbTANBgkqhkiG9w0BAQUFAAOCAQEAkHb5bDOR
16
- JSWI6Zf8+JFAEtaTNcyMbZa+MC1oLX5ghUitMXWF6ZEteMxfsjxzV5ZqBnHEGRUy
17
- A7epSKa+RcotfqSPAsx0vkxmYt4z42mh+9EQzG+2yphDfR5d7oiM4P4cV3yeSOTO
18
- 4iUpjrkbc5dUVUruYSMAATRvZA7aZvBm3pbDSMsc5oNGVuNQLKPStGyDjid1duVK
19
- DvaEck6SQdcadyVuVnlMVhzO/I6ivB+92JmuaMixdmNq53ZSs5/Gn+aXy/jFAqxN
20
- 7rG3f+cyUw1x80IvYqd3G+FElDhTYOD1wU3356NW3RmmplQCRB5xIK7Gti6ROwlm
21
- m1Vp6BHTtZqSSw==
22
- -----END CERTIFICATE-----
data.tar.gz.sig DELETED
Binary file
metadata.gz.sig DELETED
@@ -1,3 +0,0 @@
1
- .��q]��b?��b����g�
2
- 'f!�B"�p�~�K�x�|�{o��Z�uΜ;�Om�8P�j�VX9� ��3x?W9��! B�x��F���L�&�bxCd =L�ն�P=˹,uz����ZD��f3����@��]
3
- �r�9k��k��F7�6P#�:�9�%�E�!