Class: Hi::App

Inherits:
Object
  • Object
show all
Defined in:
lib/hi/app.rb

Overview

Main app; http server that prints requests as they come in

Constant Summary collapse

DEFAULT_PORT =
3000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = nil, logger = Hi::Logger.new) ⇒ App

Returns a new instance of App.



11
12
13
14
# File 'lib/hi/app.rb', line 11

def initialize(port = nil, logger = Hi::Logger.new)
  @port = (port = port.to_i) > 0 ? port : DEFAULT_PORT
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/hi/app.rb', line 7

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/hi/app.rb', line 7

def port
  @port
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/hi/app.rb', line 16

def call(env)
  request = Hi::Request.new(env).to_h

  log "#{request[:request_method]} #{request[:url]} (#{Time.now})"
  log request

  [ 200, { 'Content-Type' => 'text/plain' }, ['hi'] ]
end

#log(message) ⇒ Object



25
26
27
# File 'lib/hi/app.rb', line 25

def log(message)
  logger.log message
end