Class: Saorin::Response

Inherits:
Object
  • Object
show all
Includes:
Dumpable
Defined in:
lib/saorin/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dumpable

#to_json

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



12
13
14
15
16
17
# File 'lib/saorin/response.rb', line 12

def initialize(options = {})
  @version = options[:version] || Saorin::JSON_RPC_VERSION
  @result = options[:result]
  @error = options[:error]
  @id = options[:id]
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



10
11
12
# File 'lib/saorin/response.rb', line 10

def error
  @error
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/saorin/response.rb', line 10

def id
  @id
end

#resultObject

Returns the value of attribute result.



10
11
12
# File 'lib/saorin/response.rb', line 10

def result
  @result
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/saorin/response.rb', line 10

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



46
47
48
49
# File 'lib/saorin/response.rb', line 46

def self.from_hash(hash)
  raise Saorin::InvalidResponse unless hash.is_a?(::Hash)
  new Saorin::Utility.symbolized_keys(hash)
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/saorin/response.rb', line 19

def error?
  !!@error
end

#to_hObject



37
38
39
40
41
42
43
44
# File 'lib/saorin/response.rb', line 37

def to_h
  h = {}
  h['jsonrpc'] = @version
  h['result'] = @result unless error?
  h['error'] = @error if error?
  h['id'] = id
  h
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/saorin/response.rb', line 23

def valid?
  return false unless !(@result && @error)
  return false unless [String].any? { |type| @version.is_a? type }
  return false unless [Object].any? { |type| @result.is_a? type }
  return false unless [Saorin::Error, Hash, NilClass].any? { |type| @error.is_a? type }
  return false unless [String, Numeric, NilClass].any? { |type| @id.is_a? type }
  return false unless @version == JSON_RPC_VERSION
  true
end

#validateObject



33
34
35
# File 'lib/saorin/response.rb', line 33

def validate
  raise Saorin::InvalidResponse unless valid?
end