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

Instance Method Summary collapse

Constructor Details

#initializeBase

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

Set or read cookies. Depending on the type of ‘value`, respectively reads, deletes, or sets a cookie.

Parameters:

  • key (String)

    the name of the cookie

  • value (nil, :delete, String) (defaults to: nil)


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/woah/base.rb', line 135

def cookie(key, value = nil)
  if value.nil?
    read_cookie key
  elsif value == :delete
    del_cookie key
  elsif value.is_a? String
    set_cookie key, value
  else
    raise ArgumentError, 'Value should be nil, :delete, or a string'
  end
end

.matchObject

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.

Parameters:

  • path (String, Regexp)

    the path to respond to

  • method (String) (defaults to: 'GET')

    the HTTP method to use

Raises:

  • (ArgumentError)

    if ‘method` is not a valid HTTP 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.

Parameters:

  • path (String, Regexp)

    the path to redirect to

  • method (String) (defaults to: 'GET')

    the HTTP method to use

Returns:

  • (String)

    the redirect’s body



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

.requestObject

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.

Parameters:

  • host (String) (defaults to: '0.0.0.0')

    the host to use

  • port (Integer) (defaults to: 4422)

    the port to use



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.

Parameters:

  • item (:status, :headers, :body)

    the item to be overriden

  • content

    the content to override the item with

Raises:

  • (ArgumentError)

    if item is outside the range of accepted values



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_valuesObject

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.

Parameters:

  • path (String, Regexp)

    the path to respond to

  • method (String)

    the HTTP method to use

Returns:

  • (Hash)

    the route’s response



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