Sha256: 145b9c71209f2c08f77dfddc0e74e54f658773af329ab71411d23e35d8dae190

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'sanford-protocol'

module Sanford; end
module Sanford::Protocol

  class Request

    def self.parse(body)
      self.new(body['name'], body['params'])
    end

    attr_reader :name, :params

    def initialize(name, params)
      self.validate!(name, params)
      @name   = name.to_s
      @params = params
    end

    def to_hash
      { 'name'   => name,
        'params' => Sanford::Protocol::StringifyParams.new(params)
      }
    end

    def to_s; name; end

    def inspect
      reference = '0x0%x' % (self.object_id << 1)
      "#<#{self.class}:#{reference}"\
      " @name=#{name.inspect}"\
      " @params=#{params.inspect}>"
    end

    def ==(other)
      if other.kind_of?(self.class)
        self.to_hash == other.to_hash
      else
        super
      end
    end

    protected

    def validate!(name, params)
      problem = if !name
        "The request doesn't contain a name."
      elsif !params.kind_of?(::Hash)
        "The request's params are not valid."
      end
      raise(InvalidError, problem) if problem
    end

    InvalidError = Class.new(ArgumentError)

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sanford-protocol-0.12.0 lib/sanford-protocol/request.rb
sanford-protocol-0.11.0 lib/sanford-protocol/request.rb