Class: Rumid::Request Abstract

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/rumid/request.rb

Overview

This class is abstract.

Abstract class, representing Request. Should be extended to specify protocol.

Examples:

class Foo < Request
  read_wire do |io|
    io.readline
  end

  format_request do |raw|
    {route: raw[1], data: raw[2]}
  end

  validate_request do |raw|
    raw[4] != raw[5]
  end

  validate_request do |raw|
    raw[1]+raw[2] != raw[3]
  end
end

Constant Summary collapse

@@wire_handler =
Note:

Proc requires [IO]

Raw data from IO getter block

nil
@@wire_parser =
Note:

Proc requires [String] raw data

Raw data to request hash parser.

nil
@@request_validators =
Note:

Proc requires [String] raw data

Array(Proc)

validation procs array

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_request(&block) ⇒ Object

Note:

block requires requires [String] raw data

Initialize raw data to request hash parser

Parameters:

  • block (Proc)

    representing raw data to request hash parser


47
48
49
# File 'lib/rumid/request.rb', line 47

def self.format_request(&block)
  @@wire_parser = block
end

.read_wire(&block) ⇒ Object

Note:

block requires requires [IO]

Initalize raw data from IO getter

Parameters:

  • block (Proc)

    representing raw data from IO getter


40
41
42
# File 'lib/rumid/request.rb', line 40

def self.read_wire(&block)
  @@wire_handler = block
end

.validate_request(&block) ⇒ Object

Note:

Proc requires [Stirng] raw data

Note:

Proc returns [Boolean] request validness

Initialize new request validator

Parameters:

  • block (Proc)

    represent request validator


64
65
66
# File 'lib/rumid/request.rb', line 64

def self.validate_request(&block)
  @@request_validators << block
end

Instance Method Details

#get_request(io) ⇒ Object

Note:

Typicaly you should not call this method

Method gets only valid requests

Parameters:

  • io (IO)

    representing raw data to request hash parser


54
55
56
57
58
# File 'lib/rumid/request.rb', line 54

def get_request(io)
  raw = @@wire_handler.call(io)
  throw NotValidRequestError if @@request_validators.all?{|block| block.call(raw)}
  @@wire_parser.call(raw)
end