Class: Saorin::Response
- Inherits:
-
Object
- Object
- Saorin::Response
- Includes:
- Dumpable
- Defined in:
- lib/saorin/response.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#id ⇒ Object
Returns the value of attribute id.
-
#result ⇒ Object
Returns the value of attribute result.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(options = {}) ⇒ Response
constructor
A new instance of Response.
- #to_h ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Methods included from Dumpable
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( = {}) @version = [:version] || Saorin::JSON_RPC_VERSION @result = [:result] @error = [:error] @id = [:id] end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
10 11 12 |
# File 'lib/saorin/response.rb', line 10 def error @error end |
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/saorin/response.rb', line 10 def id @id end |
#result ⇒ Object
Returns the value of attribute result.
10 11 12 |
# File 'lib/saorin/response.rb', line 10 def result @result end |
#version ⇒ Object
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
19 20 21 |
# File 'lib/saorin/response.rb', line 19 def error? !!@error end |
#to_h ⇒ Object
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
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 |
#validate ⇒ Object
33 34 35 |
# File 'lib/saorin/response.rb', line 33 def validate raise Saorin::InvalidResponse unless valid? end |