Class: Woah::Base
- Inherits:
-
Object
- Object
- Woah::Base
- Defined in:
- lib/woah/base.rb
Overview
Base for apps.
Constant Summary collapse
- @@before =
nil- @@after =
nil- @@routes =
[]
Class Method Summary collapse
-
.after(&action) ⇒ Object
Takes a block that will be executed after every route.
-
.before(&action) ⇒ Object
Takes a block that will be executed before every route.
-
.call(env) ⇒ Object
Forwards to a new instance’s #call method.
-
.cookie(key, value = nil) ⇒ Object
Set or read cookies.
-
.match ⇒ Object
Returns the value of class attribute match.
-
.on(path, method = 'GET', &action) ⇒ Object
Register new routes.
-
.redirect_to(path, method = 'GET') ⇒ String
Redirect to another route.
-
.request ⇒ Object
Returns the value of class attribute request.
-
.run!(host = '0.0.0.0', port = 4422) ⇒ Object
Get this show on the road.
-
.set(item, content) ⇒ Object
Override an item in the response.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Answer the phone.
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#override_values ⇒ Object
Applies user overrides.
-
#resolve_route(path, method) ⇒ Hash
Resolves and executes a round.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 |
# File 'lib/woah/base.rb', line 10 def initialize @@override = {} @@match = nil @@request = nil @@response = nil end |
Class Method Details
.after(&action) ⇒ Object
Takes a block that will be executed after every route.
100 101 102 |
# File 'lib/woah/base.rb', line 100 def after(&action) @@after = action end |
.before(&action) ⇒ Object
Takes a block that will be executed before every route.
95 96 97 |
# File 'lib/woah/base.rb', line 95 def before(&action) @@before = action end |
.call(env) ⇒ Object
Forwards to a new instance’s #call method.
70 71 72 |
# File 'lib/woah/base.rb', line 70 def call(env) new.call env end |
.cookie(key, value = nil) ⇒ Object
Set or read cookies. Depending on the type of ‘value`, respectively reads, deletes, or sets a cookie.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/woah/base.rb', line 135 def (key, value = nil) if value.nil? key elsif value == :delete key elsif value.is_a? String key, value else raise ArgumentError, 'Value should be nil, :delete, or a string' end end |
.match ⇒ Object
Returns the value of class attribute match.
148 149 150 |
# File 'lib/woah/base.rb', line 148 def match @@match end |
.on(path, method = 'GET', &action) ⇒ Object
Register new routes. The optional method argument can be used to specify a method.
86 87 88 89 90 91 92 |
# File 'lib/woah/base.rb', line 86 def on(path, method = 'GET', &action) unless %w[DELETE GET HEAD OPTIONS PATCH POST PUT].include? method raise ArgumentError, 'Unknown method' end @@routes.push Route.new(path, method, &action) end |
.redirect_to(path, method = 'GET') ⇒ String
Redirect to another route.
108 109 110 111 112 113 114 115 116 |
# File 'lib/woah/base.rb', line 108 def redirect_to(path, method = 'GET') result = new.resolve_route path, method i[status headers].each do |r| set r, result[r] end result[:body] end |
.request ⇒ Object
Returns the value of class attribute request.
153 154 155 |
# File 'lib/woah/base.rb', line 153 def request @@request end |
.run!(host = '0.0.0.0', port = 4422) ⇒ Object
Get this show on the road.
77 78 79 |
# File 'lib/woah/base.rb', line 77 def run!(host = '0.0.0.0', port = 4422) Rack::Handler.pick(%w[thin puma]).run new, Host: host, Port: port end |
.set(item, content) ⇒ Object
Override an item in the response.
122 123 124 125 126 127 128 |
# File 'lib/woah/base.rb', line 122 def set(item, content) unless i[status headers body].include? item raise ArgumentError, "Unknown item #{item}, cannot override" end @@override[item] = content end |
Instance Method Details
#call(env) ⇒ Object
Answer the phone. Finds a relevant route for the parameters in env, and builds a response.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/woah/base.rb', line 20 def call(env) initialize @@request = Rack::Request.new env @@before&.call @@response = resolve_route env['REQUEST_URI'], env['REQUEST_METHOD'] @@after&.call override_values # make sure we do not give nil bodies to the server @@response[:body] ||= '' @@response[:body] = [@@response[:body]] @@response.values end |
#override_values ⇒ Object
Applies user overrides.
41 42 43 44 45 46 |
# File 'lib/woah/base.rb', line 41 def override_values i[status headers body].each do |r| o = @@override[r] @@response[r] = o unless o.nil? end end |
#resolve_route(path, method) ⇒ Hash
Resolves and executes a round.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/woah/base.rb', line 52 def resolve_route(path, method) route = @@routes.select { |r| r.matches?(method, path) }[0] if route.nil? return { status: 404, headers: { 'Content-Type' => 'text/html; charset=utf-8' }, body: 'not found L:' } end @@match = route.match if route.match route.execute end |