Class: Saorin::Request
- Inherits:
-
Object
- Object
- Saorin::Request
- Includes:
- Dumpable
- Defined in:
- lib/saorin/request.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(method, params, options = {}) ⇒ Request
constructor
A new instance of Request.
- #notify? ⇒ Boolean
- #to_h ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Methods included from Dumpable
Constructor Details
#initialize(method, params, options = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 |
# File 'lib/saorin/request.rb', line 12 def initialize(method, params, = {}) @version = [:version] || Saorin::JSON_RPC_VERSION @method = method @params = params @id = [:id] @notify = !.has_key?(:id) end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/saorin/request.rb', line 10 def id @id end |
#method ⇒ Object
Returns the value of attribute method.
10 11 12 |
# File 'lib/saorin/request.rb', line 10 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/saorin/request.rb', line 10 def params @params end |
#version ⇒ Object
Returns the value of attribute version.
10 11 12 |
# File 'lib/saorin/request.rb', line 10 def version @version end |
Class Method Details
.from_hash(hash) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/saorin/request.rb', line 56 def self.from_hash(hash) raise Saorin::InvalidRequest unless hash.is_a?(::Hash) = hash.dup method = .delete('method') params = .delete('params') new method, params, Saorin::Utility.symbolized_keys() end |
.symbolized_keys(hash) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/saorin/request.rb', line 48 def self.symbolized_keys(hash) hash.each do |k, v| if k.is_a? ::String hash[k.to_sym] = v end end end |
Instance Method Details
#notify? ⇒ Boolean
20 21 22 |
# File 'lib/saorin/request.rb', line 20 def notify? @notify end |
#to_h ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/saorin/request.rb', line 39 def to_h h = {} h['jsonrpc'] = @version h['method'] = @method h['params'] = @params if @params && !@params.empty? h['id'] = @id unless notify? h end |
#valid? ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/saorin/request.rb', line 24 def valid? return false unless @method && @version return false unless [String].any? { |type| @version.is_a? type } return false unless [String].any? { |type| @method.is_a? type } return false unless [Hash, Array, NilClass].any? { |type| @params.is_a? type } return false unless [String, Numeric, NilClass].any? { |type| @id.is_a? type } return false unless @version == JSON_RPC_VERSION return false unless !@method.start_with?('.') true end |
#validate ⇒ Object
35 36 37 |
# File 'lib/saorin/request.rb', line 35 def validate raise Saorin::InvalidRequest unless valid? end |