woah 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/woah/base.rb +32 -10
- data/lib/woah/version.rb +1 -1
- data/test/basic_verbs_test.rb +2 -2
- data/test/before_after_test.rb +2 -2
- data/test/cookie_test.rb +22 -3
- data/test/on_method_test.rb +9 -8
- data/test/redirect_test.rb +1 -1
- data/test/test_app.rb +12 -0
- 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: 7a7f6211c5dacf60338f865cd7face92231a0cf7e2b2c02301ccb416d5144c0e
|
4
|
+
data.tar.gz: 1c8df2df731ea7c0bb95616a779898ebd355b54da1ee2b01e7155f8a75ac9486
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1909f2ef0ea817668f647971dcca48aee227ca5c64f4754ffa4e1fe23f46513868b1219dde364973d1cc2cb3a8a94661a9eba13b54618586714242ff6461afc4
|
7
|
+
data.tar.gz: 53d7572bb39289c0c4d886ab26cb78bd0f09151bc997230dcbf49de29e6b7bc4c133822cf7197100bcd58e64d56b8daa020a0926c06c93ed8bf7e405b3be46bc
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Woah!
|
2
2
|
[![Build Status](https://travis-ci.org/knarka/woah.svg?branch=master)](https://travis-ci.org/knarka/woah)
|
3
|
-
[![Coverage Status](https://coveralls.io/repos/github/knarka/woah/badge.svg?branch=master)](https://coveralls.io/github/knarka/woah?branch=master)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/github/knarka/woah/badge.svg?branch=master&service=github)](https://coveralls.io/github/knarka/woah?branch=master)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/woah.svg)](https://badge.fury.io/rb/woah)
|
5
5
|
|
6
6
|
Woah! is an unobtrusive, (extremely) minimal web framework built on Rack.
|
@@ -124,10 +124,14 @@ class MyApp < Woah::Base
|
|
124
124
|
on '/set' do
|
125
125
|
cookie 'chunky', 'bacon'
|
126
126
|
end
|
127
|
+
|
128
|
+
on '/del' do
|
129
|
+
cookie 'chunky', :delete
|
130
|
+
end
|
127
131
|
end
|
128
132
|
```
|
129
133
|
|
130
|
-
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.
|
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 '/del' will delete the cookie again.
|
131
135
|
|
132
136
|
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:
|
133
137
|
|
data/lib/woah/base.rb
CHANGED
@@ -29,16 +29,22 @@ module Woah
|
|
29
29
|
|
30
30
|
@@after&.call
|
31
31
|
|
32
|
-
|
33
|
-
@@response[r] = @@override[r] unless @@override[r].nil?
|
34
|
-
end
|
32
|
+
override_values
|
35
33
|
|
36
34
|
# make sure we do not give nil bodies to the server
|
37
35
|
@@response[:body] ||= ''
|
36
|
+
@@response[:body] = [@@response[:body]]
|
38
37
|
|
39
38
|
@@response.values
|
40
39
|
end
|
41
40
|
|
41
|
+
# Applies user overrides
|
42
|
+
def override_values
|
43
|
+
%i[status headers body].each do |r|
|
44
|
+
@@response[r] = @@override[r] unless @@override[r].nil?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
42
48
|
# Resolves and executes a round
|
43
49
|
def resolve_route(method, path)
|
44
50
|
route = @@routes.select { |r| r.matches?(method, path) }[0]
|
@@ -63,12 +69,14 @@ module Woah
|
|
63
69
|
|
64
70
|
# Get this show on the road.
|
65
71
|
def run!(host = '0.0.0.0', port = 4422)
|
66
|
-
Rack::Handler.pick(%w[thin
|
72
|
+
Rack::Handler.pick(%w[thin puma]).run new, Host: host, Port: port
|
67
73
|
end
|
68
74
|
|
69
75
|
# Register new routes. The optional method argument can be used to specify a method.
|
70
76
|
def on(path, method = 'GET', &action)
|
71
|
-
|
77
|
+
unless %w[DELETE GET HEAD OPTIONS PATCH POST PUT].include? method
|
78
|
+
raise ArgumentError, 'Unknown method'
|
79
|
+
end
|
72
80
|
|
73
81
|
@@routes.push Route.new(path, method, &action)
|
74
82
|
end
|
@@ -96,24 +104,38 @@ module Woah
|
|
96
104
|
|
97
105
|
# Override an item in the response.
|
98
106
|
def set(item, content)
|
99
|
-
|
107
|
+
unless %i[status headers body].include? item
|
108
|
+
raise ArgumentError, "Unknown item #{item}, cannot override"
|
109
|
+
end
|
100
110
|
|
101
111
|
@@override[item] = content
|
102
112
|
end
|
103
113
|
|
104
114
|
# Set or read cookies
|
115
|
+
# Value should be either: nil, to read a cookie; a string, to set a cookie; or :delete, to
|
116
|
+
# delete a cookie
|
105
117
|
def cookie(key, value = nil)
|
118
|
+
# Read cookie
|
106
119
|
if value.nil?
|
107
|
-
# Read cookie
|
108
120
|
@@request.env['HTTP_COOKIE']&.split('; ')&.each do |c|
|
109
121
|
s = c.split('=')
|
110
122
|
return s[1] if s[0] == key
|
111
123
|
end
|
112
124
|
nil # if not found
|
113
|
-
|
114
|
-
|
125
|
+
|
126
|
+
# Delete cookie
|
127
|
+
elsif value == :delete
|
115
128
|
@@override[:headers] = {}
|
116
|
-
Rack::Utils.
|
129
|
+
Rack::Utils.delete_cookie_header! @@override[:headers], key
|
130
|
+
|
131
|
+
# Set cookie
|
132
|
+
elsif value.is_a? String
|
133
|
+
@@override[:headers] = {}
|
134
|
+
Rack::Utils.set_cookie_header! @@override[:headers], key, value
|
135
|
+
|
136
|
+
# Invalid argument
|
137
|
+
else
|
138
|
+
raise ArgumentError, 'Value should be either nil, :delete, or a string'
|
117
139
|
end
|
118
140
|
end
|
119
141
|
|
data/lib/woah/version.rb
CHANGED
data/test/basic_verbs_test.rb
CHANGED
@@ -11,7 +11,7 @@ class BasicVerbsTest < MiniTest::Test
|
|
11
11
|
response = TestApp.call @env
|
12
12
|
|
13
13
|
assert_equal 200, response[0]
|
14
|
-
assert_equal 'hi there!', response[2]
|
14
|
+
assert_equal 'hi there!', response[2][0]
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_post
|
@@ -20,7 +20,7 @@ class BasicVerbsTest < MiniTest::Test
|
|
20
20
|
response = TestApp.call @env
|
21
21
|
|
22
22
|
assert_equal 200, response[0]
|
23
|
-
assert_equal 'got post!', response[2]
|
23
|
+
assert_equal 'got post!', response[2][0]
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_get_404
|
data/test/before_after_test.rb
CHANGED
@@ -12,13 +12,13 @@ class BeforeAfterTest < MiniTest::Test
|
|
12
12
|
response = TestApp.call @env
|
13
13
|
|
14
14
|
assert_equal 200, response[0]
|
15
|
-
assert_equal 'chunky', response[2]
|
15
|
+
assert_equal 'chunky', response[2][0]
|
16
16
|
|
17
17
|
@env['REQUEST_URI'] = '/after'
|
18
18
|
|
19
19
|
response = TestApp.call @env
|
20
20
|
|
21
21
|
assert_equal 200, response[0]
|
22
|
-
assert_equal 'bacon', response[2]
|
22
|
+
assert_equal 'bacon', response[2][0]
|
23
23
|
end
|
24
24
|
end
|
data/test/cookie_test.rb
CHANGED
@@ -12,7 +12,7 @@ class CookieTest < MiniTest::Test
|
|
12
12
|
response = TestApp.call @env
|
13
13
|
|
14
14
|
assert_equal 200, response[0]
|
15
|
-
assert_equal 'something', response[2]
|
15
|
+
assert_equal 'something', response[2][0]
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_get_nonexistent_cookie
|
@@ -21,7 +21,7 @@ class CookieTest < MiniTest::Test
|
|
21
21
|
response = TestApp.call @env
|
22
22
|
|
23
23
|
assert_equal 200, response[0]
|
24
|
-
assert_equal '', response[2]
|
24
|
+
assert_equal '', response[2][0]
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_set_cookie
|
@@ -31,6 +31,25 @@ class CookieTest < MiniTest::Test
|
|
31
31
|
|
32
32
|
assert_equal 200, response[0]
|
33
33
|
assert_equal 'fruit=apple', response[1]['Set-Cookie']
|
34
|
-
assert_equal '', response[2]
|
34
|
+
assert_equal '', response[2][0]
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_set_illegal_cookie
|
38
|
+
@env['REQUEST_URI'] = '/set_illegal_cookie'
|
39
|
+
@env['REQUEST_METHOD'] = 'GET'
|
40
|
+
|
41
|
+
assert_raises ArgumentError do
|
42
|
+
TestApp.call @env
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_delete_cookie
|
47
|
+
@env['REQUEST_URI'] = '/delete_cookie'
|
48
|
+
@env['REQUEST_METHOD'] = 'GET'
|
49
|
+
|
50
|
+
response = TestApp.call @env
|
51
|
+
|
52
|
+
assert_equal 200, response[0]
|
53
|
+
assert response[1]['Set-Cookie'].include? 'max-age=0'
|
35
54
|
end
|
36
55
|
end
|
data/test/on_method_test.rb
CHANGED
@@ -16,7 +16,7 @@ class OnMethodTest < MiniTest::Test
|
|
16
16
|
response = TestApp.call @env
|
17
17
|
|
18
18
|
assert_equal 404, response[0]
|
19
|
-
assert_equal "it's a secret to everybody", response[2]
|
19
|
+
assert_equal "it's a secret to everybody", response[2][0]
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_regexp_path
|
@@ -46,11 +46,11 @@ class OnMethodTest < MiniTest::Test
|
|
46
46
|
response = TestApp.call @env
|
47
47
|
|
48
48
|
assert_equal 200, response[0]
|
49
|
-
assert_equal 'hi there, Charles', response[2]
|
49
|
+
assert_equal 'hi there, Charles', response[2][0]
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_illegal_method
|
53
|
-
assert_raises
|
53
|
+
assert_raises ArgumentError do
|
54
54
|
TestApp.on '/', 'BUBBLES' do
|
55
55
|
'oOooo oO oO'
|
56
56
|
end
|
@@ -64,10 +64,11 @@ class OnMethodTest < MiniTest::Test
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def test_illegal_set
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
@env['REQUEST_URI'] = '/nose'
|
68
|
+
@env['REQUEST_METHOD'] = 'GET'
|
69
|
+
|
70
|
+
assert_raises ArgumentError do
|
71
|
+
TestApp.call @env
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
@@ -77,6 +78,6 @@ class OnMethodTest < MiniTest::Test
|
|
77
78
|
response = TestApp.call @env
|
78
79
|
|
79
80
|
assert_equal 200, response[0]
|
80
|
-
assert_equal '/ip', response[2]
|
81
|
+
assert_equal '/ip', response[2][0]
|
81
82
|
end
|
82
83
|
end
|
data/test/redirect_test.rb
CHANGED
@@ -17,7 +17,7 @@ class RedirectTest < MiniTest::Test
|
|
17
17
|
response = TestApp.call @env
|
18
18
|
|
19
19
|
assert_equal 200, response[0]
|
20
|
-
assert_equal 'hi there!', response[2]
|
20
|
+
assert_equal 'hi there!', response[2][0]
|
21
21
|
assert_equal 'gone back', $something
|
22
22
|
end
|
23
23
|
# rubocop:enable Style/GlobalVars
|
data/test/test_app.rb
CHANGED
@@ -31,10 +31,22 @@ class TestApp < Woah::Base
|
|
31
31
|
cookie 'fruit', 'apple'
|
32
32
|
end
|
33
33
|
|
34
|
+
on '/set_illegal_cookie' do
|
35
|
+
cookie 'one', 1
|
36
|
+
end
|
37
|
+
|
38
|
+
on '/delete_cookie' do
|
39
|
+
cookie 'foo', :delete
|
40
|
+
end
|
41
|
+
|
34
42
|
on '/ip' do
|
35
43
|
request.env['REQUEST_URI']
|
36
44
|
end
|
37
45
|
|
46
|
+
TestApp.on '/nose' do
|
47
|
+
TestApp.set :nose, true
|
48
|
+
end
|
49
|
+
|
38
50
|
after do
|
39
51
|
@b = 'bacon'
|
40
52
|
end
|
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.
|
4
|
+
version: 1.2.0
|
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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|