yaframework 0.4.7 → 0.4.8

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: 4aec9734b89fbd8a9c87fb63506f49f53df83c0f141efd612d5cdceb9d4d8c64
4
- data.tar.gz: 8d43e4889c14798d20400ae36bfa9c6b76797bf7348eac539d85c690dcf250a3
3
+ metadata.gz: 63212eea1f540904cec647bd269e65ed74c5f17569d211c5e2df1cacf5e906eb
4
+ data.tar.gz: c0d422d3fbcf9f05fb3110062730cfbfa6e943aff65112c2c5262188c79189bf
5
5
  SHA512:
6
- metadata.gz: 00af9a420fd2edffe78d77d6a2ecfc81973cbadb9543424153a6a5e689409a06d4c98a5ff54e7ddbda2a122cbe3bcd2d5b563968f8820738c6f21747269201d7
7
- data.tar.gz: ba55916ef3b9776ab8711e82af51406b5d4e00b6991acf04418fc1526f6a0aa803757b9744296180843cf201534f4ed14292ac2f738a92d00bb8d5ad11bf6925
6
+ metadata.gz: 4c03d0a6d9a2e8a0b59403f4814a79f32169f06984f40a0f62f41d1ed03e3cea55ee7cb53e677d557de3763dbf1e3846152429026983d59b3871e3d813fad60f
7
+ data.tar.gz: 9f07579ed44407f75a5431f5c65d33c3f7824c133e672a0ed2278f8033307c37c2b8456eff10a10c8864ad128f69f289a4450bc0143e2d4fb9b34a7c5916953d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.4.8] - 2021-07-25
2
+
3
+ - Before/after hooks added
4
+
1
5
  ## [0.4.7] - 2021-07-25
2
6
 
3
7
  - Handling 404 error fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaframework (0.4.7)
4
+ yaframework (0.4.8)
5
5
  rack (~> 2.2, >= 2.2.3)
6
6
 
7
7
  GEM
data/examples/README.md CHANGED
@@ -2,21 +2,45 @@
2
2
 
3
3
  Here are some examples of applications created with Yaframework:
4
4
 
5
- 1. [**Hello World**](https://github.com/maxbarsukov/yaframework/tree/master/examples/hello_world.rb)
5
+ 1. [**Hello World**](https://github.com/maxbarsukov/yaframework/blob/master/examples/hello_world.rb)
6
6
 
7
- A simple application that displays "Hello world" to you. Go to `http://localhost:4567/` and see `Hello World`!
7
+ A simple application that displays "Hello world" to you.
8
8
 
9
9
 
10
- 2. [**Params**](https://github.com/maxbarsukov/yaframework/tree/master/examples/params.rb)
10
+ 2. [**Params**](https://github.com/maxbarsukov/yaframework/blob/master/examples/params.rb)
11
11
 
12
12
  Shows how the application parameters can be used
13
13
 
14
14
 
15
- 3. [**Halts and Redirects**](https://github.com/maxbarsukov/yaframework/tree/master/examples/halts_and_redirects.rb)
15
+ 3. [**Halts and Redirects**](https://github.com/maxbarsukov/yaframework/blob/master/examples/halts_and_redirects.rb)
16
16
 
17
17
  Shows how to use halts and redirects
18
18
 
19
19
 
20
+ 4. [**Headers**](https://github.com/maxbarsukov/yaframework/blob/master/examples/headers.rb)
21
+
22
+ How to add and view headers.
23
+
24
+
25
+ 5. [**Error handling**](https://github.com/maxbarsukov/yaframework/blob/master/examples/error_handling.rb)
26
+
27
+ How to handle errors
28
+
29
+
30
+ 6. [**Content Types**](https://github.com/maxbarsukov/yaframework/blob/master/examples/content_types.rb)
31
+
32
+ Using json, html, or plain text
33
+
34
+
35
+ 7. [**Cookies**](https://github.com/maxbarsukov/yaframework/blob/master/examples/cookies.rb)
36
+
37
+ Adding and deleting cookies
38
+
39
+ 8. [**Hooks**](https://github.com/maxbarsukov/yaframework/blob/master/examples/before_after_hooks.rb)
40
+
41
+ How to use `before` and `after` hooks
42
+
43
+
20
44
  ## Installation
21
45
 
22
46
  Clone this repo and go to this folder.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.before do
7
+ @page_updates ||= 0
8
+ @page_updates += 1
9
+ end
10
+
11
+ app.after do
12
+ puts "Some log info: #{response.body}"
13
+ response.write "<br>Refresh the page and your next number will be #{@page_updates + 1}!"
14
+ end
15
+
16
+ app.get "/" do
17
+ "Hello, your num is: #{@page_updates}"
18
+ end
19
+
20
+ app.listen(4567)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../lib/yaframework"
3
+ require "yaframework"
4
4
  app = Yaframework::Application
5
5
 
6
6
  app.get "/" do
@@ -8,7 +8,7 @@ app.get "/" do
8
8
  end
9
9
 
10
10
  app.get "/hello" do
11
- "Hello world!"
11
+ "Hi, you were redirected here from the root page. You can go <a href=\"/error\"></a> to get a 401 error"
12
12
  end
13
13
 
14
14
  app.get "/error" do
@@ -8,6 +8,8 @@ module Yaframework
8
8
 
9
9
  def initialize
10
10
  @routes = Hash.new([])
11
+ @before_hooks = []
12
+ @after_hooks = []
11
13
  @inbox = {}
12
14
  end
13
15
 
@@ -28,6 +30,14 @@ module Yaframework
28
30
  throw :halt, response
29
31
  end
30
32
 
33
+ def before(&block)
34
+ @before_hooks << block
35
+ end
36
+
37
+ def after(&block)
38
+ @after_hooks << block
39
+ end
40
+
31
41
  def handle(status, &block)
32
42
  @inbox[status] = block
33
43
  end
@@ -59,11 +69,14 @@ module Yaframework
59
69
  response.status = 404 unless route
60
70
 
61
71
  if @inbox[response.status]
62
- response.write instance_eval(&@inbox[response.status])
72
+ response.write exec(@inbox[response.status])
63
73
  return response.finish
64
74
  end
65
75
 
66
- response.write instance_eval(&route[:handler]) if route
76
+ exec_before_hooks
77
+ response.write exec(route[:handler]) if route
78
+ exec_after_hooks
79
+
67
80
  response.finish
68
81
  end
69
82
 
@@ -80,6 +93,30 @@ module Yaframework
80
93
  end
81
94
  route
82
95
  end
96
+
97
+ def exec(action)
98
+ if action.respond_to? :to_sym
99
+ send(action)
100
+ else
101
+ instance_exec(&action)
102
+ end
103
+ end
104
+
105
+ def exec_before_hooks
106
+ exec_hooks @before_hooks
107
+ end
108
+
109
+ def exec_after_hooks
110
+ exec_hooks @after_hooks
111
+ end
112
+
113
+ def exec_hooks(hooks)
114
+ return true if hooks.nil?
115
+
116
+ hooks.each do |hook|
117
+ return false if exec(hook) == false
118
+ end
119
+ end
83
120
  end
84
121
 
85
122
  Application = Base.new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.4.7"
4
+ VERSION = "0.4.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaframework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxbarsukov
@@ -107,6 +107,7 @@ files:
107
107
  - bin/setup
108
108
  - examples/Gemfile
109
109
  - examples/README.md
110
+ - examples/before_after_hooks.rb
110
111
  - examples/content_types.rb
111
112
  - examples/cookies.rb
112
113
  - examples/error_handling.rb