class Rumid::Server

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

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

Public Class Methods

new() click to toggle source
# File lib/rumid/server.rb, line 34
def initialize
  @responsed = false
end
show_routes() click to toggle source

@return [Hash] hash with Keys - routes, Values - Procs for handling requests

# File lib/rumid/server.rb, line 49
def self.show_routes
  @@routes
end

Private Class Methods

handle_request_with(request_handler_class) click to toggle source

Initializing [Request] class to handle requests @param request_handler_class [Class] klass representing Request specified by protocol @example

handle_request_with FooRequest

@note request_handler_class extends [Request]

# 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) click to toggle source

Initialising new route @param route [String] representing route string @param block [Proc] representing route handler @example

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

@note Proc requires [{Symbol=>String}] request hash @note Proc returns [{Symbol=>String}] response hash

# File lib/rumid/server.rb, line 82
def self.route(route, &block)
  @@routes[Regexp.new(route)] = block
end
wrap_response(&block) click to toggle source

Initializing how to pack responces to send back by protocol @param block [Proc] representing respose wrapper @example

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

@note Proc requires [{Symbol=>String}] response hash @note Proc returns [String] raw packet

# File lib/rumid/server.rb, line 94
def self.wrap_response(&block)
  @@response_wrapper = block
end

Public Instance Methods

serve(io) click to toggle source

Main server function. Do all work from getting raw request to sending raw response @note Typicaly you should not call this method @param io [IO] I/O stream for getting requests and sending responces

# File lib/rumid/server.rb, line 42
def serve(io)
  @io = io
  request_hash = get_request
  route_handle(request_hash)
end

Private Instance Methods

get_request() click to toggle source

Method reading data from I/O stream @note Typicaly you should not call this method

# 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) click to toggle source

Method sending response. Checks response uniquness @param response [String] response raw string

# 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) click to toggle source

Method which sends request direct roated handler @note Typicaly you should not call this method @param request_hash [{Symbol=>Proc}] request hash

# 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