Class: Woah::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/woah/base.rb

Overview

Base for apps

Constant Summary collapse

@@before =
nil
@@after =
nil
@@routes =
{}

Class Method Summary collapse

Class Method Details

.after(&action) ⇒ Object

things to do after handling the routes



39
40
41
# File 'lib/woah/base.rb', line 39

def self.after(&action)
  @@after = action
end

.before(&action) ⇒ Object

things to do before handling the routes



34
35
36
# File 'lib/woah/base.rb', line 34

def self.before(&action)
  @@before = action
end

.call(env) ⇒ Object

answer the phone



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/woah/base.rb', line 11

def self.call(env)
  route = @@routes[env['REQUEST_URI']]
  @@override = {}

  @@before&.call

  return [404, { 'Content-Type' => 'text/html; charset=utf-8' }, 'not found L:'] if route.nil?

  response = route.execute
  i[status headers body].each do |r|
    response[r] = @@override[r] unless @@override[r].nil?
  end
  @@after&.call

  response.values
end

.on(path, &action) ⇒ Object

register new routes



29
30
31
# File 'lib/woah/base.rb', line 29

def self.on(path, &action)
  @@routes[path] = Route.new(path, 'GET', &action)
end

.run!(port = 4422) ⇒ Object

get this show on the road



52
53
54
# File 'lib/woah/base.rb', line 52

def self.run!(port = 4422)
  Rack::Handler.pick(%w[thin webrick]).run new, Port: port
end

.set(item, content) ⇒ Object

override an item in the response



44
45
46
47
48
49
# File 'lib/woah/base.rb', line 44

def self.set(item, content)
  unless i[status headers body].include? item
    raise 'unknown item ' + item.to_s + ', cannot override'
  end
  @@override[item] = content
end