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) ⇒ ClientHelper

Returns a new instance of ClientHelper.



15
16
17
18
19
# File 'lib/jimson/client.rb', line 15

def initialize(url)
  @url = url
  URI.parse(@url) # for the sake of validating the url
  @batch = []
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

#process_batch_response(responses) ⇒ Object



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

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

#process_call(sym, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jimson/client.rb', line 21

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

  begin
    data = JSON.parse(resp)
  rescue
    raise Jimson::ClientError::InvalidJSON.new(resp)
  end

  return process_single_response(data)
end

#process_single_response(data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jimson/client.rb', line 69

def process_single_response(data)
  raise Jimson::ClientError::InvalidResponse.new if !valid_response?(data)

  if !!data['error']
    code = data['error']['code']
    if Jimson::ServerError::CODES.keys.include?(code)
      raise Jimson::ServerError::CODES[code].new
    else
      raise Jimson::ClientError::UnknownServerError.new(code, data['error']['message'])
    end
  end

  return data['result']

  rescue Exception, StandardError
    raise Jimson::ClientError::InternalError.new($!)
end

#push_batch_request(request) ⇒ Object



112
113
114
115
116
117
# File 'lib/jimson/client.rb', line 112

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

#send_batchObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jimson/client.rb', line 119

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

  begin
    responses = JSON.parse(response)
  rescue
    raise Jimson::ClientError::InvalidJSON.new(json)
  end

  process_batch_response(responses)
  @batch = []
end

#send_batch_request(batch) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/jimson/client.rb', line 51

def send_batch_request(batch)
  post_data = batch.to_json
  resp = RestClient.post(@url, post_data, :content_type => 'application/json')
  if resp.nil? || resp.body.nil? || resp.body.empty?
    raise Jimson::ClientError::InvalidResponse.new
  end

  return resp.body
end

#send_single_request(method, args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jimson/client.rb', line 33

def send_single_request(method, args)
  post_data = {
                'jsonrpc' => JSON_RPC_VERSION,
                'method'  => method,
                'params'  => args,
                'id'      => self.class.make_id
              }.to_json
  resp = RestClient.post(@url, post_data, :content_type => 'application/json')
  if resp.nil? || resp.body.nil? || resp.body.empty?
    raise Jimson::ClientError::InvalidResponse.new
  end

  return resp.body

  rescue Exception, StandardError
    raise Jimson::ClientError::InternalError.new($!)
end

#valid_response?(data) ⇒ Boolean

Returns:

  • (Boolean)


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

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?(Fixnum) || !data['error']['message'].is_a?(String)
      return false
    end
  end

  return true
  
  rescue
    return false
end