Class: Miron::Handler::WEBrick

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/miron/handlers/webrick.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, mironfile) ⇒ WEBrick

Returns a new instance of WEBrick


22
23
24
25
# File 'lib/miron/handlers/webrick.rb', line 22

def initialize(server, mironfile)
  super server
  @mironfile = mironfile
end

Class Method Details

.run(mironfile, options = {}) {|@server| ... } ⇒ Object

Yields:

  • (@server)

8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/miron/handlers/webrick.rb', line 8

def self.run(mironfile, options = {})
  options[:BindAddress] = options['host']
  options[:Port] = options['port']
  @server = ::WEBrick::HTTPServer.new(options)
  @server.mount('/', Miron::Handler::WEBrick, mironfile)
  yield @server if block_given?

  begin
    @server.start
  rescue Interrupt
    puts '* Shutting down...'
  end
end

Instance Method Details

#parse_input_body(webrick_request, miron_request) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/miron/handlers/webrick.rb', line 39

def parse_input_body(webrick_request, miron_request)
  if webrick_request.body.to_s.nil? || webrick_request.body.to_s.empty?
    miron_request['HTTP_BODY'] = ''
  else
    miron_request['HTTP_BODY'] = ::MultiJson.load(webrick_request.body.to_s)
  end
end

#service(webrick_request, webrick_response) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miron/handlers/webrick.rb', line 27

def service(webrick_request, webrick_response)
  miron_request = webrick_request.meta_vars
  parse_input_body(webrick_request, miron_request)
  miron_response = Miron::RequestFetcher.new(miron_request, @mironfile).fetch_response

  webrick_response.status = miron_response.http_status
  webrick_response.body << miron_response.body
  miron_response.headers.each { |k, v| webrick_response[k] = v }
  miron_response.cookies.each { |k, v| webrick_response.cookies << "#{k}=#{v}" }
  miron_response.body.close if miron_response.body.respond_to?(:close)
end

#shutdownObject


47
48
49
# File 'lib/miron/handlers/webrick.rb', line 47

def shutdown
  @server.shutdown
end