Class: Saorin::Server::Rack

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/saorin/server/rack.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json'
}.freeze

Instance Attribute Summary collapse

Attributes included from Base

#allowed_methods, #handler, #options

Instance Method Summary collapse

Methods included from Base

#dispatch_request, #dispatch_request_with_trap_notification, #dump_response, #handle_request, #parse_request, #process_request

Methods included from Formatter

#default_formatter, #formatter

Constructor Details

#initialize(handler, options = {}) ⇒ Rack

Returns a new instance of Rack.



15
16
17
18
19
20
21
22
# File 'lib/saorin/server/rack.rb', line 15

def initialize(handler, options = {})
  super
  @server = ::Rack::Server.new({
    :app => self,
    :Host => options[:host],
    :Port => options[:port],
  }.merge(@options))
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



13
14
15
# File 'lib/saorin/server/rack.rb', line 13

def server
  @server
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
# File 'lib/saorin/server/rack.rb', line 34

def call(env)
  request = ::Rack::Request.new(env)
  response = ::Rack::Response.new([], 200, DEFAULT_HEADERS.dup)
  response.write process_request(request.body.read) if request.post?
  response.finish
end

#shutdownObject



28
29
30
31
32
# File 'lib/saorin/server/rack.rb', line 28

def shutdown
  if @server.server.respond_to?(:shutdown)
    @server.server.shutdown
  end
end

#start(&block) ⇒ Object



24
25
26
# File 'lib/saorin/server/rack.rb', line 24

def start(&block)
  @server.start &block
end