Class: Jimson::ClientHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/jimson/client.rb

Constant Summary collapse

JSON_RPC_VERSION =
'2.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}, namespace = nil, client_opts = {}) ⇒ ClientHelper

Returns a new instance of ClientHelper.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jimson/client.rb', line 15

def initialize(url, opts = {}, namespace = nil, client_opts = {})
  URI.parse(url) # for the sake of validating the url
  @url = url
  @opts = opts
  @opts[:id_type] ||= :int # Possible id types: :int, :string
  @namespace = namespace
  @client_opts = client_opts

  @batch = []
  @headers = opts.slice( * opts.keys - [:id_type] )
  @headers[:content_type] ||= 'application/json'
end

Class Method Details

.make_idObject



11
12
13
# File 'lib/jimson/client.rb', line 11

def self.make_id
  rand(10**12)
end

Instance Method Details

#format_post_id(id) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/jimson/client.rb', line 136

def format_post_id(id)
  if @opts[:id_type] == :string 
    id.to_s
  else
    id
  end
end

#process_batch_response(responses) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/jimson/client.rb', line 70

def process_batch_response(responses)
  responses.each do |resp|
    saved_response = @batch.map { |r| r[1] }.select { |r| r.id == resp['id'] }.first
    raise Client::Error::InvalidResponse.new if saved_response.nil?
    saved_response.populate!(resp)
  end
end

#process_call(sym, args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jimson/client.rb', line 28

def process_call(sym, args)
  resp = send_single_request(sym.to_s, args)

  begin
    data = MultiJson.decode(resp)
  rescue
    raise Client::Error::InvalidJSON.new(resp)
  end

  return process_single_response(data)

  rescue Exception, StandardError => e
    e.extend(Client::Error) unless e.is_a?(Client::Error)
    raise e
end

#process_single_response(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jimson/client.rb', line 78

def process_single_response(data)
  raise Client::Error::InvalidResponse.new(data) if !valid_response?(data)

  if !!data['error']
    code = data['error']['code']
    msg = data['error']['message']
    raise Client::Error::ServerError.new(code, msg)
  end

  return data['result']
end

#push_batch_request(request) ⇒ Object



115
116
117
118
119
120
# File 'lib/jimson/client.rb', line 115

def push_batch_request(request)
  request.id = self.class.make_id
  response = Response.new(request.id)
  @batch << [request, response]
  return response
end

#send_batchObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jimson/client.rb', line 122

def send_batch
  batch = @batch.map(&:first) # get the requests
  response = send_batch_request(batch)

  begin
    responses = MultiJson.decode(response)
  rescue
    raise Client::Error::InvalidJSON.new(response)
  end

  process_batch_response(responses)
  @batch = []
end

#send_batch_request(batch) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/jimson/client.rb', line 60

def send_batch_request(batch)
  post_data = MultiJson.encode(batch)
  resp = RestClient::Request.execute(@client_opts.merge(:method => :post, :url => @url, :payload => post_data, :headers => @headers))
  if resp.nil? || resp.body.nil? || resp.body.empty?
    raise Client::Error::InvalidResponse.new(resp)
  end

  return resp.body
end

#send_single_request(method, args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jimson/client.rb', line 44

def send_single_request(method, args)
  namespaced_method = @namespace.nil? ? method : "#@namespace#{method}"
  post_data = MultiJson.encode({
    'jsonrpc' => JSON_RPC_VERSION,
    'method'  => namespaced_method,
    'params'  => args,
    'id'      => format_post_id(self.class.make_id)
  })
  resp = RestClient::Request.execute(@client_opts.merge(:method => :post, :url => @url, :payload => post_data, :headers => @headers))
  if resp.nil? || resp.body.nil? || resp.body.empty?
    raise Client::Error::InvalidResponse.new(resp)
  end

  return resp.body
end

#valid_response?(data) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jimson/client.rb', line 90

def valid_response?(data)
  return false if !data.is_a?(Hash)

  return false if data['jsonrpc'] != JSON_RPC_VERSION

  return false if !data.has_key?('id')

  return false if data.has_key?('error') && data.has_key?('result')

  if data.has_key?('error')
    if !data['error'].is_a?(Hash) || !data['error'].has_key?('code') || !data['error'].has_key?('message')
      return false
    end

    if !data['error']['code'].is_a?(Integer) || !data['error']['message'].is_a?(String)
      return false
    end
  end

  return true

  rescue
    return false
end