Class: RJR::Result

Inherits:
Object show all
Defined in:
lib/rjr/result.rb

Overview

JSON-RPC Result Representation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Result

RJR result intializer

Parameters:

  • args (Hash) (defaults to: {})

    options to set on result

Options Hash (args):

  • :result (Object)

    result of json-rpc method handler if successfully returned

  • :error_code (Integer)

    code corresponding to json-rpc error if problem occured during request invocation

  • :error_msg (String)

    message corresponding to json-rpc error if problem occured during request invocation

  • :error_class (Class)

    class of error raised (if any) during request invocation (this is extra metadata beyond standard json-rpc)



34
35
36
37
38
39
40
41
42
# File 'lib/rjr/result.rb', line 34

def initialize(args = {})
  @result        = args[:result]      || args['result']
  @error_code    = args[:error_code]  || args['error_code']
  @error_msg     = args[:error_msg]   || args['error_msg']
  @error_class   = args[:error_class] || args['error_class']

  @success       =  @error_code.nil?
  @failed        = !@error_code.nil?
end

Instance Attribute Details

#error_classObject

Class of error raised (if any) during request invocation (this is extra metadata beyond standard json-rpc)



26
27
28
# File 'lib/rjr/result.rb', line 26

def error_class
  @error_class
end

#error_codeObject

Code corresponding to json-rpc error if problem occured during request invocation



20
21
22
# File 'lib/rjr/result.rb', line 20

def error_code
  @error_code
end

#error_msgObject

Message corresponding to json-rpc error if problem occured during request invocation



23
24
25
# File 'lib/rjr/result.rb', line 23

def error_msg
  @error_msg
end

#failedObject

Boolean indicating if request failed in some manner



14
15
16
# File 'lib/rjr/result.rb', line 14

def failed
  @failed
end

#resultObject

Return value of the json-rpc call if successful



17
18
19
# File 'lib/rjr/result.rb', line 17

def result
  @result
end

#successObject

Boolean indicating if request was successfully invoked



11
12
13
# File 'lib/rjr/result.rb', line 11

def success
  @success
end

Class Method Details

.invalid_requestObject

JSON-RPC -32600 / Invalid Request



63
64
65
66
# File 'lib/rjr/result.rb', line 63

def self.invalid_request
   return Result.new(:error_code => -32600,
                     :error_msg => 'Invalid Request')
end

.method_not_found(name) ⇒ Object

JSON-RPC -32602 / Method not found



69
70
71
72
# File 'lib/rjr/result.rb', line 69

def self.method_not_found(name)
   return Result.new(:error_code => -32602,
                     :error_msg => "Method '#{name}' not found")
end

Instance Method Details

#==(other) ⇒ Object

Compare Result against other result, returning true if both correspond to equivalent json-rpc results else false



46
47
48
49
50
51
52
53
# File 'lib/rjr/result.rb', line 46

def ==(other)
  @success     == other.success    &&
  @failed      == other.failed     &&
  @result      == other.result     &&
  @error_code  == other.error_code &&
  @error_msg   == other.error_msg  &&
  @error_class == other.error_class
end

#to_sObject

Convert Response to human consumable string



56
57
58
# File 'lib/rjr/result.rb', line 56

def to_s
  "#{@success} #{@result} #{@error_code} #{@error_msg} #{@error_class}"
end