class Saorin::Request

Attributes

id[RW]
method[RW]
params[RW]
version[RW]

Public Class Methods

from_hash(hash) click to toggle source
# File lib/saorin/request.rb, line 55
def self.from_hash(hash)
  raise Saorin::InvalidRequest unless hash.is_a?(::Hash)
  options = hash.dup
  method = options.delete('method')
  params = options.delete('params')
  new method, params, Saorin::Utility.symbolized_keys(options)
end
new(method, params, options = {}) click to toggle source
# File lib/saorin/request.rb, line 11
def initialize(method, params, options = {})
  @version = options[:version] || Saorin::JSON_RPC_VERSION
  @method = method
  @params = params
  @id = options[:id]
  @notify = !options.has_key?(:id)
end
symbolized_keys(hash) click to toggle source
# File lib/saorin/request.rb, line 47
def self.symbolized_keys(hash)
  hash.each do |k, v|
    if k.is_a? ::String
      hash[k.to_sym] = v
    end
  end
end

Public Instance Methods

notify?() click to toggle source
# File lib/saorin/request.rb, line 19
def notify?
  @notify
end
to_h() click to toggle source
# File lib/saorin/request.rb, line 38
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?() click to toggle source
# File lib/saorin/request.rb, line 23
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() click to toggle source
# File lib/saorin/request.rb, line 34
def validate
  raise Saorin::InvalidRequest unless valid?
end