class Woah::Base
Base
for apps.
Public Class Methods
Takes a block that will be executed after every route.
# File lib/woah/base.rb, line 100 def after(&action) @@after = action end
Takes a block that will be executed before every route.
# File lib/woah/base.rb, line 95 def before(&action) @@before = action end
Forwards to a new instance's call
method.
# File lib/woah/base.rb, line 70 def call(env) new.call env end
Returns the value of class attribute match.
# File lib/woah/base.rb, line 148 def match @@match end
# File lib/woah/base.rb, line 10 def initialize @@override = {} @@match = nil @@request = nil @@response = nil end
Register new routes. The optional method argument can be used to specify a method. @param path [String, Regexp] the path to respond to @param method [String] the HTTP method to use @raise [ArgumentError] if `method` is not a valid HTTP method
# 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 another route. @param path [String, Regexp] the path to redirect to @param method [String] the HTTP method to use @return [String] the redirect's body
# 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
Returns the value of class attribute request.
# File lib/woah/base.rb, line 153 def request @@request end
Get this show on the road. @param host [String] the host to use @param port [Integer] the port to use
# 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
Override an item in the response. @param item [:status, :headers, :body] the item to be overriden @param content the content to override the item with @raise [ArgumentError] if item is outside the range of accepted values
# 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
Private Class Methods
Public Instance Methods
Answer the phone. Finds a relevant route for the parameters in env, and builds a response.
# 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
Applies user overrides.
# 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
Resolves and executes a round. @param path [String, Regexp] the path to respond to @param method [String] the HTTP method to use @return [Hash] the route's response
# 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