woah 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2d82c3f80800e51ad922187916a7d818af057d76b16ab42ba98be5429a2f27b
4
- data.tar.gz: c3b6f727f5d2e9e2bd4a45af2b7f2409f10fb47ee1bb0c145db17eec3727bf5d
3
+ metadata.gz: b373c366351b1f33c918ffa6cc3ee146f8c9754cebbe829fa2af1e68cdddb0bc
4
+ data.tar.gz: 96ccbe51c94b82e599921e4cd5e3e6d39b952ee3b04a11e6fab86d38cd34753d
5
5
  SHA512:
6
- metadata.gz: 1cedfb0abbfffeb861369f8885aa80fa1fa0bcd58ad9847489b27e083b2736189eae8912e66399092c8b70029695cecb42b83e6ead64e538890eeabcb56c7d4d
7
- data.tar.gz: dd7d5428fffcd5328751a86443d7624040f33319d5d48390c69ffeb8315f23a3f53d98c89d647e805e1ce022c95129420ace8ac9fb6f12e66c9919684cc4f98c
6
+ metadata.gz: 0663f9e05e1e27a8ba09dee02bbfe623377b95c6806100d576debc0cec2587e1983b1ad77e952162b63f65f64f0cd959df7b21fc60a9aae52e5e5ca165debcb1
7
+ data.tar.gz: 48b773cc5248397bf2b0393251e9adda278a7165a9e82effa127c8e0ce6b8de1a811d444bf2efeb9a85864f4fc9baa244c8f012c96021924a526b8d4f424aba6
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Coverage Status](https://coveralls.io/repos/github/knarka/woah/badge.svg?branch=master)](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
- Woah! is a minimal web framework built on Rack. It's primary design goal is to be unobtrusive, and to just let you do your thing, dude.
6
+ Woah! is an unobtrusive, (extremely) minimal web framework built on Rack.
7
7
 
8
8
  ## Installation
9
9
  `gem install woah`
@@ -54,7 +54,7 @@ class MyApp < Woah::Base
54
54
  end
55
55
  ```
56
56
 
57
- There's two new blocks here: `before` and `after`. They do things before and after the relevant route gets executed (duh!). This example will increment a counter everytime a page is hit, regardless of what page it is.
57
+ There's two new blocks here: `before` and `after`. They do things before and after the relevant route gets executed. This example will increment a counter everytime a page is hit, regardless of what page it is.
58
58
 
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
 
@@ -87,7 +87,7 @@ Of course, sometimes you want routes to be flexible, and to catch more than one
87
87
  require 'woah'
88
88
 
89
89
  class MyApp < Woah::Base
90
- on %{^/greet/(\w+)$} do
90
+ on %r{^/greet/(\w+)$} do
91
91
  "oh, hello, I didn't see you there #{match[1]}"
92
92
  end
93
93
  end
@@ -95,6 +95,40 @@ end
95
95
 
96
96
  Now, visiting `/greet/Socrates` will greet you with your own name. Wonderful. By the way, you may have noticed we're using `%r{}` to delimit our regex, 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
97
 
98
+ Redirects are possible as well:
99
+
100
+ ```ruby
101
+ require 'woah'
102
+
103
+ class MyApp < Woah::Base
104
+ on '/' do
105
+ redirect_to '/landing'
106
+ end
107
+
108
+ on '/landing' do
109
+ 'welcome'
110
+ end
111
+ end
112
+ ```
113
+
114
+ So are cookies:
115
+
116
+ ```ruby
117
+ require 'woah'
118
+
119
+ class MyApp < Woah::Base
120
+ on '/' do
121
+ cookie['chunky'] || 'no cookie set'
122
+ end
123
+
124
+ on '/set' do
125
+ cookie['chunky'] = 'bacon'
126
+ end
127
+ end
128
+ ```
129
+
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.
131
+
98
132
  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:
99
133
 
100
134
  ```ruby
data/lib/woah.rb CHANGED
@@ -7,6 +7,5 @@ module Woah
7
7
  end
8
8
 
9
9
  require_relative 'woah/base'
10
- require_relative 'woah/request'
11
10
  require_relative 'woah/route'
12
11
  require_relative 'woah/version'
data/lib/woah/base.rb CHANGED
@@ -9,22 +9,34 @@ module Woah
9
9
  @@request = nil
10
10
  @@routes = []
11
11
 
12
- def call(env)
12
+ def initialize
13
13
  @@override = {}
14
14
  @@match_data = nil
15
- @@request = Request.new env
15
+ @@response = nil
16
+ end
17
+
18
+ # Answer the phone.
19
+ # Finds a relevant route for the parameters in env,
20
+ # and builds a response.
21
+ def call(env)
22
+ initialize
23
+
24
+ @@request = Rack::Request.new env
16
25
 
17
26
  @@before&.call
18
27
 
19
- response = resolve_route env['REQUEST_METHOD'], env['REQUEST_URI']
28
+ @@response = resolve_route env['REQUEST_METHOD'], env['REQUEST_URI']
20
29
 
21
30
  @@after&.call
22
31
 
23
32
  %i[status headers body].each do |r|
24
- response[r] = @@override[r] unless @@override[r].nil?
33
+ @@response[r] = @@override[r] unless @@override[r].nil?
25
34
  end
26
35
 
27
- response.values
36
+ # make sure we do not give nil bodies to the server
37
+ @@response[:body] ||= ''
38
+
39
+ @@response.values
28
40
  end
29
41
 
30
42
  # Resolves and executes a round
@@ -45,18 +57,15 @@ module Woah
45
57
  end
46
58
 
47
59
  class << self
60
+ def call(env)
61
+ new.call env
62
+ end
63
+
48
64
  # Get this show on the road.
49
65
  def run!(host = '0.0.0.0', port = 4422)
50
66
  Rack::Handler.pick(%w[thin webrick]).run new, Host: host, Port: port
51
67
  end
52
68
 
53
- # Answer the phone.
54
- # Finds a relevant route for the parameters in env,
55
- # and builds a response.
56
- def call(env)
57
- new.call env
58
- end
59
-
60
69
  # Register new routes. The optional method argument can be used to specify a method.
61
70
  def on(path, method = 'GET', &action)
62
71
  raise 'unknown method' unless %w[DELETE GET HEAD OPTIONS PATCH POST PUT].include? method
@@ -92,6 +101,22 @@ module Woah
92
101
  @@override[item] = content
93
102
  end
94
103
 
104
+ # Set or read cookies
105
+ def cookie(key, value = nil)
106
+ if value.nil?
107
+ # Read cookie
108
+ @@request.env['HTTP_COOKIE']&.split('; ')&.each do |c|
109
+ s = c.split('=')
110
+ return s[1] if s[0] == key
111
+ end
112
+ nil # if not found
113
+ else
114
+ # Set cookie
115
+ @@override[:headers] = {}
116
+ Rack::Utils.set_cookie_header!(@@override[:headers], key, value)
117
+ end
118
+ end
119
+
95
120
  # Get match data from Regexp routes.
96
121
  def match
97
122
  @@match_data
data/lib/woah/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Woah
4
4
  # Woah!'s current version.
5
- VERSION = '1.0.3'
5
+ VERSION = '1.1.0'
6
6
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CookieTest < MiniTest::Test
4
+ def setup
5
+ @env = {}
6
+ end
7
+
8
+ def test_get_cookie
9
+ @env['REQUEST_URI'] = '/get_cookie'
10
+ @env['REQUEST_METHOD'] = 'GET'
11
+ @env['HTTP_COOKIE'] = 'test=something'
12
+ response = TestApp.call @env
13
+
14
+ assert_equal 200, response[0]
15
+ assert_equal 'something', response[2]
16
+ end
17
+
18
+ def test_get_nonexistent_cookie
19
+ @env['REQUEST_URI'] = '/get_cookie'
20
+ @env['REQUEST_METHOD'] = 'GET'
21
+ response = TestApp.call @env
22
+
23
+ assert_equal 200, response[0]
24
+ assert_equal '', response[2]
25
+ end
26
+
27
+ def test_set_cookie
28
+ @env['REQUEST_URI'] = '/set_cookie'
29
+ @env['REQUEST_METHOD'] = 'GET'
30
+ response = TestApp.call @env
31
+
32
+ assert_equal 200, response[0]
33
+ assert_equal 'fruit=apple', response[1]['Set-Cookie']
34
+ assert_equal '', response[2]
35
+ end
36
+ end
@@ -70,4 +70,13 @@ class OnMethodTest < MiniTest::Test
70
70
  end
71
71
  end
72
72
  end
73
+
74
+ def test_request_object
75
+ @env['REQUEST_URI'] = '/ip'
76
+ @env['REQUEST_METHOD'] = 'GET'
77
+ response = TestApp.call @env
78
+
79
+ assert_equal 200, response[0]
80
+ assert_equal '/ip', response[2]
81
+ end
73
82
  end
data/test/test_app.rb CHANGED
@@ -23,6 +23,18 @@ class TestApp < Woah::Base
23
23
  @b
24
24
  end
25
25
 
26
+ on '/get_cookie' do
27
+ cookie 'test'
28
+ end
29
+
30
+ on '/set_cookie' do
31
+ cookie 'fruit', 'apple'
32
+ end
33
+
34
+ on '/ip' do
35
+ request.env['REQUEST_URI']
36
+ end
37
+
26
38
  after do
27
39
  @b = 'bacon'
28
40
  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.0.3
4
+ version: 1.1.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-06-25 00:00:00.000000000 Z
11
+ date: 2018-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -35,11 +35,11 @@ files:
35
35
  - README.md
36
36
  - lib/woah.rb
37
37
  - lib/woah/base.rb
38
- - lib/woah/request.rb
39
38
  - lib/woah/route.rb
40
39
  - lib/woah/version.rb
41
40
  - test/basic_verbs_test.rb
42
41
  - test/before_after_test.rb
42
+ - test/cookie_test.rb
43
43
  - test/on_method_test.rb
44
44
  - test/redirect_test.rb
45
45
  - test/test_app.rb
data/lib/woah/request.rb DELETED
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Woah
4
- # Extend Rack's Request object
5
- class Request < Rack::Request
6
- end
7
- end