rpc4json 0.0.2.alpha → 0.0.2.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,13 +17,17 @@ module JSONRPC
17
17
  end
18
18
  end
19
19
 
20
- def initialize(method, params = nil)
20
+ def initialize(method, params = nil, uuid = true)
21
21
  assert_valid_arguements(method, params)
22
22
 
23
- @id = unique_request_id
23
+ @id = unique_request_id if uuid
24
24
  @jsonrpc = JSONRPC_VERSION
25
25
  @method = method
26
- @params = params if params
26
+ @params = params if params
27
+ end
28
+
29
+ def bytesize
30
+ @bytesize ||= to_json.bytesize
27
31
  end
28
32
 
29
33
  def to_hash
@@ -37,7 +41,7 @@ module JSONRPC
37
41
  alias :to_h :to_hash
38
42
 
39
43
  def to_json
40
- JSON.dump(to_hash)
44
+ @json ||= to_hash.to_json
41
45
  end
42
46
 
43
47
  protected
@@ -14,55 +14,33 @@ module JSONRPC
14
14
  class Client
15
15
  include ContentTypeParser
16
16
 
17
- USER_AGENT = "JSONRPC::Client (Ruby #{RUBY_VERSION})"
17
+ USER_AGENT = "JSONRPC::Client (Ruby #{RUBY_VERSION})".freeze
18
+
19
+ DEFAULT_HEADERS = {
20
+ 'User-Agent' => USER_AGENT,
21
+ 'Accept' => 'application/json',
22
+ 'Content-Type' => 'application/json; charset=utf-8'
23
+ }.freeze
18
24
 
19
25
  # Add additional HTTP headers to the request.
20
26
  attr_accessor :http_header_extra
21
27
 
22
28
  def initialize(host = nil, path = nil, port = nil, proxy_host = nil,
23
29
  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
-
30
+ set_connection(host, path, port, proxy_host, proxy_port, use_ssl, timeout)
31
+ set_authentication(user, password)
52
32
  # HTTP object for synchronous calls
53
33
  @http = new_http
54
34
  end
55
35
 
56
36
  def request_with_array(method, *args)
57
- request = Request.new(method, args).to_json
37
+ request = Request.new(method, args)
58
38
  result = do_rpc(request, false)
59
39
  Response.from_json(result, request)
60
40
  end
61
41
 
62
42
  def notify_with_array(method, *args)
63
- Request.new(method, args).tap do |request|
64
- do_rpc(request.to_json, false)
65
- end
43
+ Request.new(method, args).tap { |request| do_rpc(request, false) }
66
44
  end
67
45
 
68
46
  def proxy(prefix = nil, *args)
@@ -72,65 +50,51 @@ module JSONRPC
72
50
  private
73
51
 
74
52
  def do_rpc(request, async = false)
75
- headers, response = headers(request, async), nil
53
+ headers = headers(request, async)
54
+ @http_last_response = nil
76
55
 
77
56
  if async
78
- new_http.start {
79
- response = http.post2(@path, request, headers)
80
- }
57
+ new_http.start do
58
+ @http_last_response = http.post2(@path, request.to_json, headers)
59
+ end
81
60
  else
82
61
  # 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
62
  @http.start unless @http.started?
86
-
87
- response = @http.post2(@path, request, headers)
63
+ @http_last_response = @http.post2(@path, request.to_json, headers)
88
64
  end
89
65
 
90
- @http_last_response = response
91
-
92
- response.body.tap do |data|
93
- if response.code == '401'
66
+ @http_last_response.body.tap do |data|
67
+ if @http_last_response.code == '401'
94
68
  # 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}"
69
+ raise "Authorization failed.\nHTTP-Error: #{@http_last_response.code} #{@http_last_response.message}"
70
+ elsif @http_last_response.code[0, 1] != "2"
71
+ raise "HTTP-Error: #{@http_last_response.code} #{@http_last_response.message}"
98
72
  end
99
73
 
100
- content_type = parse_content_type(response['Content-Type']).first
74
+ content_type = parse_content_type(@http_last_response['Content-Type']).first
101
75
  if content_type != 'application/json'
102
76
  raise "Wrong content-type (received '#{content_type}' but expected 'application/json')"
103
77
  end
104
78
 
105
- expected = response['Content-Length'] || '<unknown>'
106
- if data.nil? || data.bytesize == 0 || expected != '<unknown>' && expected.to_i != data.bytesize && response['Transfer-Encoding'].nil?
79
+ expected = @http_last_response['Content-Length'] || '<unknown>'
80
+ if data.nil? || data.bytesize == 0 || expected != '<unknown>' && expected.to_i != data.bytesize && @http_last_response['Transfer-Encoding'].nil?
107
81
  raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
108
82
  end
109
83
 
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
84
+ set_cookies
118
85
  end
119
86
  end
120
87
 
121
88
  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
89
+ DEFAULT_HEADERS.dup.tap do |headers|
90
+ headers['Authorization'] = @auth unless @auth.nil?
91
+ headers['Cookie'] = @cookie if @cookie
92
+ headers.merge!({
93
+ 'Content-Length' => request.bytesize.to_s,
94
+ 'Connection' => (async ? 'close' : 'keep-alive')
95
+ })
96
+ headers.merge!(@http_header_extra) if @http_header_extra
97
+ end
134
98
  end
135
99
 
136
100
  def new_http
@@ -142,11 +106,43 @@ module JSONRPC
142
106
  end
143
107
  end
144
108
 
145
- def set_auth
146
- @auth = if !@user.nil?
147
- credentials = "#{@user}"
148
- credentials << ":#{@password}" unless @password.nil?
149
- @auth = 'Basic ' + [credentials].pack('m0')
109
+ def set_authentication(user, password)
110
+ @user, @password = user, password
111
+ @auth = unless @user.nil?
112
+ 'Basic ' + [[@user, @password].compact.join(':')].pack('m0')
113
+ end
114
+ end
115
+
116
+ def set_connection(host, path, port, proxy_host, proxy_port, use_ssl, timeout)
117
+ @host = host || 'localhost'
118
+ @path = path || '/RPC2'
119
+ @proxy_host = proxy_host
120
+ @proxy_port = proxy_port
121
+ @proxy_host ||= 'localhost' unless @proxy_port.nil?
122
+ @proxy_port ||= 8080 unless @proxy_host.nil?
123
+ @use_ssl = use_ssl || false
124
+ @timeout = timeout || 30
125
+
126
+ @port = if use_ssl
127
+ require 'net/https'
128
+ port || 443
129
+ else
130
+ port || 80
131
+ end
132
+
133
+ # convert ports to integers
134
+ @port = @port.to_i unless @port.nil?
135
+ @proxy_port = @proxy_port.to_i unless @proxy_port.nil?
136
+ end
137
+
138
+ def set_cookies
139
+ set_cookies = @http_last_response.get_fields('Set-Cookie')
140
+ if set_cookies && !set_cookies.empty?
141
+ require 'webrick/cookie'
142
+ @cookie = set_cookies.collect do |set_cookie|
143
+ cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
144
+ WEBrick::Cookie.new(cookie.name, cookie.value).to_s
145
+ end.join('; ')
150
146
  end
151
147
  end
152
148
  end
@@ -4,11 +4,8 @@ require 'jsonrpc/abstract_request'
4
4
 
5
5
  module JSONRPC
6
6
  class Notification < AbstractRequest
7
-
8
- protected
9
-
10
- def unique_request_id
11
- nil
7
+ def initialize(method, params = nil)
8
+ super(method, params, false)
12
9
  end
13
10
  end
14
11
  end
@@ -3,5 +3,9 @@
3
3
  require 'jsonrpc/abstract_request'
4
4
 
5
5
  module JSONRPC
6
- class Request < AbstractRequest; end
6
+ class Request < AbstractRequest
7
+ def initialize(method, params = nil)
8
+ super(method, params, true)
9
+ end
10
+ end
7
11
  end
@@ -1,11 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'json'
4
- require 'jsonrpc/abstract_request'
5
4
 
6
5
  module JSONRPC
7
6
  class Response
8
- attr_reader :request, :id, :jsonrpc, :result, :error
7
+ attr_reader :raw, :request
9
8
 
10
9
  class << self
11
10
  def from_json(json, request = nil)
@@ -14,24 +13,24 @@ module JSONRPC
14
13
  end
15
14
 
16
15
  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
16
  @request = request
17
+ @raw = response
23
18
  end
24
19
 
25
- def body
26
- success? ? result : error
20
+ [:id, :error, :jsonrpc, :result].each do |method|
21
+ define_method(method) { @raw[method.to_s] }
27
22
  end
28
23
 
29
24
  def error?
30
25
  !success?
31
26
  end
32
27
 
28
+ def result_or_error
29
+ success? ? result : error
30
+ end
31
+
33
32
  def success?
34
- @error.nil?
33
+ error.nil?
35
34
  end
36
35
  end
37
36
  end
@@ -12,7 +12,7 @@ module JSONRPC
12
12
  # The tiny number version.
13
13
  TINY = 2
14
14
  # The pre realese number version.
15
- PRE = :alpha
15
+ PRE = :alpha1
16
16
 
17
17
  # The complete number version.
18
18
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpc4json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.alpha
4
+ version: 0.0.2.alpha1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: