woah 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -21
- data/lib/woah/base.rb +30 -14
- data/lib/woah/route.rb +10 -6
- data/lib/woah/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9301cad20ebaf896c239f25ade186b723f9f01a09493557590fbaff7da1bb610
|
4
|
+
data.tar.gz: 7165d88451d5d03d397e38602818ac2d972b7b4384e6cbaa97dd1419024c9c7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa9663c853c588d9c2bf66f2b2be756233e4b5a76e7d795fe65fdec9adfc1aa0f3619e76693db42a70ab49162aaaa5446b7966114f64438caf7c4fd550d5452e
|
7
|
+
data.tar.gz: b562426f30b5b5a0aa0123a0e34fbfe5ed196d59a8e46dc683e48833dd1f9ae55e828b033fca637329ca9c00921382e9aa64ce612904364ef72658a9dbd247b8
|
data/README.md
CHANGED
@@ -9,20 +9,22 @@ Woah! is an unobtrusive, (extremely) minimal web framework built on Rack.
|
|
9
9
|
`gem install woah`
|
10
10
|
|
11
11
|
## What do I do with it???
|
12
|
-
|
12
|
+
You're gonna want to extend Woah::Base, which will be your app's, er, base.
|
13
13
|
|
14
14
|
```ruby
|
15
15
|
require 'woah'
|
16
16
|
|
17
17
|
class MyApp < Woah::Base
|
18
18
|
end
|
19
|
+
|
20
|
+
MyApp.run!
|
19
21
|
```
|
20
22
|
|
21
|
-
|
23
|
+
You now have an app that serves 404 errors on every route. Not very useful, so let's extend that.
|
22
24
|
|
23
|
-
|
24
|
-
require 'woah'
|
25
|
+
By the way, the lines `require 'woah'` and `MyApp.run!` will always be necessary, but to keep this file a little neater, they'll be omitted in the next examples. Just pretend they're there.
|
25
26
|
|
27
|
+
```ruby
|
26
28
|
class MyApp < Woah::Base
|
27
29
|
on '/hello' do
|
28
30
|
"hey, what's up"
|
@@ -30,11 +32,9 @@ class MyApp < Woah::Base
|
|
30
32
|
end
|
31
33
|
```
|
32
34
|
|
33
|
-
When someone stumbles upon `/hello` now, they'll be greeted properly. Nice.
|
35
|
+
When someone stumbles upon `/hello` now, they'll be greeted properly. Nice. These blocks of code are called **routes**. There's more types of blocks than just routes though. Check this out:
|
34
36
|
|
35
37
|
```ruby
|
36
|
-
require 'woah'
|
37
|
-
|
38
38
|
class MyApp < Woah::Base
|
39
39
|
before do
|
40
40
|
@@num ||= 1
|
@@ -59,8 +59,6 @@ There's two new blocks here: `before` and `after`. They do things before and aft
|
|
59
59
|
Of course, getting pages isn't everything you can do on the Internet. There's other HTTP verbs as well, like POST. Behold:
|
60
60
|
|
61
61
|
```ruby
|
62
|
-
require 'woah'
|
63
|
-
|
64
62
|
class MyApp < Woah::Base
|
65
63
|
before do
|
66
64
|
@@content ||=
|
@@ -84,8 +82,6 @@ As soon as you click the button on `/`, the message on the page will transform.
|
|
84
82
|
Of course, sometimes you want routes to be flexible, and to catch more than one expression. For this, you can use regular expressions instead of strings as your routes, just like this:
|
85
83
|
|
86
84
|
```ruby
|
87
|
-
require 'woah'
|
88
|
-
|
89
85
|
class MyApp < Woah::Base
|
90
86
|
on %r{^/greet/(\w+)$} do
|
91
87
|
"oh, hello, I didn't see you there #{match[1]}"
|
@@ -93,13 +89,11 @@ class MyApp < Woah::Base
|
|
93
89
|
end
|
94
90
|
```
|
95
91
|
|
96
|
-
Now, visiting `/greet/Socrates` will greet you with your own name. Wonderful. By the way, you may have noticed
|
92
|
+
Now, visiting `/greet/Socrates` will greet you with your own name. Wonderful. By the way, you may have noticed the regex here is delimited by `%r{}`, instead of the more common `//`. This is because of how common slashes are in routes, so it's recommended to use this syntax. You can use slashes to delimit your regex though, if you like. I won't judge you.
|
97
93
|
|
98
94
|
Redirects are possible as well:
|
99
95
|
|
100
96
|
```ruby
|
101
|
-
require 'woah'
|
102
|
-
|
103
97
|
class MyApp < Woah::Base
|
104
98
|
on '/' do
|
105
99
|
redirect_to '/landing'
|
@@ -114,8 +108,6 @@ end
|
|
114
108
|
So are cookies:
|
115
109
|
|
116
110
|
```ruby
|
117
|
-
require 'woah'
|
118
|
-
|
119
111
|
class MyApp < Woah::Base
|
120
112
|
on '/' do
|
121
113
|
cookie 'chunky' || 'no cookie set'
|
@@ -131,13 +123,11 @@ class MyApp < Woah::Base
|
|
131
123
|
end
|
132
124
|
```
|
133
125
|
|
134
|
-
Upon first visiting, this page will tell you there's no cookie set. After visiting `/set` however, it'll display `bacon`, as that is now the content of the `chunky` cookie. Visiting
|
126
|
+
Upon first visiting, this page will tell you there's no cookie set. After visiting `/set` however, it'll display `bacon`, as that is now the content of the `chunky` cookie. Visiting `/del` will delete the cookie again.
|
135
127
|
|
136
128
|
We're nearing the end of this little guide already, I'm afraid. However, there's still one more trick you need to see. Look, sometimes, you might disagree with the things Woah! thinks up for you. That's why you can override everything Woah! is about to send, if you so please. Por exemplo:
|
137
129
|
|
138
130
|
```ruby
|
139
|
-
require_relative 'lib/woah'
|
140
|
-
|
141
131
|
class MyApp < Woah::Base
|
142
132
|
on '/' do
|
143
133
|
'(insert super secret information)'
|
@@ -155,8 +145,6 @@ class MyApp < Woah::Base
|
|
155
145
|
end
|
156
146
|
end
|
157
147
|
end
|
158
|
-
|
159
|
-
MyApp.run!
|
160
148
|
```
|
161
149
|
|
162
150
|
That's all. Have fun!
|
data/lib/woah/base.rb
CHANGED
@@ -5,13 +5,13 @@ module Woah
|
|
5
5
|
class Base
|
6
6
|
@@before = nil
|
7
7
|
@@after = nil
|
8
|
-
@@
|
8
|
+
@@match = nil
|
9
9
|
@@request = nil
|
10
10
|
@@routes = []
|
11
11
|
|
12
12
|
def initialize
|
13
13
|
@@override = {}
|
14
|
-
@@
|
14
|
+
@@match = nil
|
15
15
|
@@response = nil
|
16
16
|
end
|
17
17
|
|
@@ -25,7 +25,7 @@ module Woah
|
|
25
25
|
|
26
26
|
@@before&.call
|
27
27
|
|
28
|
-
@@response = resolve_route env['
|
28
|
+
@@response = resolve_route env['REQUEST_URI'], env['REQUEST_METHOD']
|
29
29
|
|
30
30
|
@@after&.call
|
31
31
|
|
@@ -46,7 +46,10 @@ module Woah
|
|
46
46
|
end
|
47
47
|
|
48
48
|
# Resolves and executes a round
|
49
|
-
|
49
|
+
# @param path [String, Regexp] the path to respond to
|
50
|
+
# @param method [String] the HHTP method to use
|
51
|
+
# @return [Hash] the route's response
|
52
|
+
def resolve_route(path, method)
|
50
53
|
route = @@routes.select { |r| r.matches?(method, path) }[0]
|
51
54
|
|
52
55
|
if route.nil?
|
@@ -57,22 +60,28 @@ module Woah
|
|
57
60
|
}
|
58
61
|
end
|
59
62
|
|
60
|
-
@@
|
63
|
+
@@match = route.match if route.match
|
61
64
|
|
62
65
|
route.execute
|
63
66
|
end
|
64
67
|
|
65
68
|
class << self
|
69
|
+
# Forwards to a new instance's #call method
|
66
70
|
def call(env)
|
67
71
|
new.call env
|
68
72
|
end
|
69
73
|
|
70
74
|
# Get this show on the road.
|
75
|
+
# @host [String] the host to use
|
76
|
+
# @port [Integer] the port to use
|
71
77
|
def run!(host = '0.0.0.0', port = 4422)
|
72
78
|
Rack::Handler.pick(%w[thin puma]).run new, Host: host, Port: port
|
73
79
|
end
|
74
80
|
|
75
|
-
# Register new routes. The optional method argument can be used to specify a method
|
81
|
+
# Register new routes. The optional method argument can be used to specify a method
|
82
|
+
# @param path [String, Regexp] the path to respond to
|
83
|
+
# @param method [String] the HHTP method to use
|
84
|
+
# @raise [ArgumentError] if `method` is not a valid HTTP method
|
76
85
|
def on(path, method = 'GET', &action)
|
77
86
|
unless %w[DELETE GET HEAD OPTIONS PATCH POST PUT].include? method
|
78
87
|
raise ArgumentError, 'Unknown method'
|
@@ -81,19 +90,22 @@ module Woah
|
|
81
90
|
@@routes.push Route.new(path, method, &action)
|
82
91
|
end
|
83
92
|
|
84
|
-
#
|
93
|
+
# Takes a block that will be executed after every route
|
85
94
|
def before(&action)
|
86
95
|
@@before = action
|
87
96
|
end
|
88
97
|
|
89
|
-
#
|
98
|
+
# Takes a block that will be executed after every route
|
90
99
|
def after(&action)
|
91
100
|
@@after = action
|
92
101
|
end
|
93
102
|
|
94
103
|
# Redirect to another route.
|
104
|
+
# @param path [String, Regexp] the path to redirect to
|
105
|
+
# @param method [String] the HTTP method to use
|
106
|
+
# @return [String] the redirect's body
|
95
107
|
def redirect_to(path, method = 'GET')
|
96
|
-
result = new.resolve_route
|
108
|
+
result = new.resolve_route path, method
|
97
109
|
|
98
110
|
%i[status headers].each do |r|
|
99
111
|
set r, result[r]
|
@@ -103,6 +115,9 @@ module Woah
|
|
103
115
|
end
|
104
116
|
|
105
117
|
# Override an item in the response.
|
118
|
+
# @param item [:status, :headers, :body] the item to be overriden
|
119
|
+
# @param content the content to override the item with
|
120
|
+
# @raise [ArgumentError] if item is outside the range of accepted values
|
106
121
|
def set(item, content)
|
107
122
|
unless %i[status headers body].include? item
|
108
123
|
raise ArgumentError, "Unknown item #{item}, cannot override"
|
@@ -112,8 +127,9 @@ module Woah
|
|
112
127
|
end
|
113
128
|
|
114
129
|
# Set or read cookies
|
115
|
-
#
|
116
|
-
#
|
130
|
+
# Depending on the type of `value`, respectively reads, deletes, or sets a cookie
|
131
|
+
# @param key [String] the name of the cookie
|
132
|
+
# @param value [nil, :delete, String]
|
117
133
|
def cookie(key, value = nil)
|
118
134
|
# Read cookie
|
119
135
|
if value.nil?
|
@@ -139,12 +155,12 @@ module Woah
|
|
139
155
|
end
|
140
156
|
end
|
141
157
|
|
142
|
-
#
|
158
|
+
# Returns the value of class attribute match
|
143
159
|
def match
|
144
|
-
@@
|
160
|
+
@@match
|
145
161
|
end
|
146
162
|
|
147
|
-
#
|
163
|
+
# Returns the value of class attribute request
|
148
164
|
def request
|
149
165
|
@@request
|
150
166
|
end
|
data/lib/woah/route.rb
CHANGED
@@ -1,30 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Woah
|
4
|
-
#
|
4
|
+
# A Woah! routes
|
5
5
|
class Route
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :match
|
7
7
|
|
8
8
|
def initialize(path, method, &action)
|
9
9
|
raise 'only strings and regexps are valid paths' unless [String, Regexp].include? path.class
|
10
10
|
@path = path
|
11
11
|
@method = method
|
12
12
|
@action = action
|
13
|
-
@
|
13
|
+
@match = nil
|
14
14
|
end
|
15
15
|
|
16
|
-
#
|
16
|
+
# Checks if a given route is the same as this one
|
17
|
+
# @param path [String, Regexp] the path to redirect to
|
18
|
+
# @param method [String] the HTTP method to use
|
19
|
+
# @return [Boolean] true if given method and path match this route
|
17
20
|
def matches?(method, path)
|
18
21
|
case @path
|
19
22
|
when String
|
20
23
|
@method == method && @path == path
|
21
24
|
when Regexp
|
22
|
-
@
|
23
|
-
@method == method && @
|
25
|
+
@match = @path.match path
|
26
|
+
@method == method && @match
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
27
30
|
# Execute this route's actions.
|
31
|
+
# @return [Hash] the route's response
|
28
32
|
def execute
|
29
33
|
status = 200
|
30
34
|
headers = { 'Content-Type' => 'text/html; charset=utf-8' }
|
data/lib/woah/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: woah
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- knarka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|