Class: DummyOscar::App

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file, library:) ⇒ App

Returns a new instance of App.



18
19
20
21
# File 'lib/dummy_oscar/app.rb', line 18

def initialize(config_file, library:)
  @router = DummyOscar::Router.new
  parse_config_file(config_file, library: library)
end

Class Method Details

.build(config_file, library:) ⇒ Object



13
14
15
# File 'lib/dummy_oscar/app.rb', line 13

def build(config_file, library:)
  new(config_file, library: library)
end

Instance Method Details

#app(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dummy_oscar/app.rb', line 23

def app(env)
  input = "'#{env['rack.input'].read}'"
  $stdout.puts "Started #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]} #{input}"

  route = @router.find(path: env["PATH_INFO"], method: env["REQUEST_METHOD"])
  if route
    headers = {}
    headers["Content-Type"] = route.response["content_type"] if route.response["content_type"]
    return [route.response["status_code"], headers, [route.response["body"].to_s]]
  end

  [404, {}, ["Not found"]]
end