Class: Rumid::Server Abstract

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

Overview

This class is abstract.

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

Examples:

class Foo < Server
  handle_request_with(BarRequest)

  wrap_response do |response|
    "Responce is: #{responce[:route]}|#{responce[:data]}"
  end

  route "foo" do |request|
    {route: "foo", data: 1}
  end

  route "bar" do |request|
    {route: "bar", data: 2}
  end
end

Constant Summary collapse

@@routes =

See Also:

{}
@@request_handler =

See Also:

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server


34
35
36
# File 'lib/rumid/server.rb', line 34

def initialize
  @responsed = false
end

Class Method Details

.handle_request_with(request_handler_class) ⇒ Object (private)

Note:

request_handler_class extends [Request]

Initializing [Request] class to handle requests

Examples:

handle_request_with FooRequest

Parameters:

  • request_handler_class (Class)

    klass representing Request specified by protocol


103
104
105
# File 'lib/rumid/server.rb', line 103

def self.handle_request_with(request_handler_class)
  @@request_handler = request_handler_class.new
end

.route(route, &block) ⇒ Object (private)

Note:

Proc requires [Symbol=>String] request hash

Note:

Proc returns [Symbol=>String] response hash

Initialising new route

Examples:

route "foo" do |request|
  {route: "foo", data: 1}
end

Parameters:

  • route (String)

    representing route string

  • block (Proc)

    representing route handler


82
83
84
# File 'lib/rumid/server.rb', line 82

def self.route(route, &block)
  @@routes[Regexp.new(route)] = block
end

.show_routesHash

Returns hash with Keys - routes, Values - Procs for handling requests

Returns:

  • (Hash)

    hash with Keys - routes, Values - Procs for handling requests


49
50
51
# File 'lib/rumid/server.rb', line 49

def self.show_routes
  @@routes
end

.wrap_response(&block) ⇒ Object (private)

Note:

Proc requires [Symbol=>String] response hash

Note:

Proc returns [String] raw packet

Initializing how to pack responces to send back by protocol

Examples:

wrap_response do |response|
  "Responce is: #{responce[:route]}|#{responce[:data]}"
end

Parameters:

  • block (Proc)

    representing respose wrapper


94
95
96
# File 'lib/rumid/server.rb', line 94

def self.wrap_response(&block)
  @@response_wrapper = block
end

Instance Method Details

#get_requestObject (private)

Note:

Typicaly you should not call this method

Method reading data from I/O stream

Raises:

  • (NotImplementedError)

57
58
59
60
# File 'lib/rumid/server.rb', line 57

def get_request
  raise NotImplementedError, "Request handler is not initialized." if @@request_handler.nil?
  @@request_handler.get_request(@io)
end

#response_with(response) ⇒ Object (private)

Method sending response. Checks response uniquness

Parameters:

  • response (String)

    response raw string


109
110
111
112
113
114
115
116
# File 'lib/rumid/server.rb', line 109

def response_with(response)
  if @responsed == true then
    warn "Trying to response with:\n  #{response}\nfor already responsed request!"
    return
  end
  @io.write(response)
  @responsed = true
end

#route_handle(request_hash) ⇒ Object (private)

Note:

Typicaly you should not call this method

Method which sends request direct roated handler

Parameters:

  • request_hash ({Symbol=>Proc})

    request hash


65
66
67
68
69
70
# File 'lib/rumid/server.rb', line 65

def route_handle(request_hash)
  @@routes.each do |route,method|
    return response_with(@@response_wrapper.call(method.call(request_hash))) unless (route =~ request_hash[:route]).nil?
  end
  warn "This request doesn't match any route."
end

#serve(io) ⇒ Object

Note:

Typicaly you should not call this method

Main server function. Do all work from getting raw request to sending raw response

Parameters:

  • io (IO)

    I/O stream for getting requests and sending responces


42
43
44
45
46
# File 'lib/rumid/server.rb', line 42

def serve(io)
  @io = io
  request_hash = get_request
  route_handle(request_hash)
end