Class: RJR::Result

Inherits:
Object show all
Defined in:
lib/rjr/dispatcher.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

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)



40
41
42
43
44
45
46
47
48
# File 'lib/rjr/dispatcher.rb', line 40

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)



32
33
34
# File 'lib/rjr/dispatcher.rb', line 32

def error_class
  @error_class
end

#error_codeObject

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



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

def error_code
  @error_code
end

#error_msgObject

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



29
30
31
# File 'lib/rjr/dispatcher.rb', line 29

def error_msg
  @error_msg
end

#failedObject

Boolean indicating if request failed in some manner



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

def failed
  @failed
end

#resultObject

Return value of the json-rpc call if successful



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

def result
  @result
end

#successObject

Boolean indicating if request was successfully invoked



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

def success
  @success
end

Class Method Details

.invalid_requestObject

JSON-RPC -32600 / Invalid Request



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

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



75
76
77
78
# File 'lib/rjr/dispatcher.rb', line 75

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



52
53
54
55
56
57
58
59
# File 'lib/rjr/dispatcher.rb', line 52

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



62
63
64
# File 'lib/rjr/dispatcher.rb', line 62

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