yaframework 0.4.7 → 0.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/examples/README.md +28 -4
- data/examples/before_after_hooks.rb +20 -0
- data/examples/error_handling.rb +1 -1
- data/examples/halts_and_redirects.rb +1 -1
- data/lib/yaframework/base.rb +39 -2
- data/lib/yaframework/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63212eea1f540904cec647bd269e65ed74c5f17569d211c5e2df1cacf5e906eb
|
4
|
+
data.tar.gz: c0d422d3fbcf9f05fb3110062730cfbfa6e943aff65112c2c5262188c79189bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c03d0a6d9a2e8a0b59403f4814a79f32169f06984f40a0f62f41d1ed03e3cea55ee7cb53e677d557de3763dbf1e3846152429026983d59b3871e3d813fad60f
|
7
|
+
data.tar.gz: 9f07579ed44407f75a5431f5c65d33c3f7824c133e672a0ed2278f8033307c37c2b8456eff10a10c8864ad128f69f289a4450bc0143e2d4fb9b34a7c5916953d
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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/
|
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.
|
7
|
+
A simple application that displays "Hello world" to you.
|
8
8
|
|
9
9
|
|
10
|
-
2. [**Params**](https://github.com/maxbarsukov/yaframework/
|
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/
|
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)
|
data/examples/error_handling.rb
CHANGED
data/lib/yaframework/base.rb
CHANGED
@@ -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
|
72
|
+
response.write exec(@inbox[response.status])
|
63
73
|
return response.finish
|
64
74
|
end
|
65
75
|
|
66
|
-
|
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
|
data/lib/yaframework/version.rb
CHANGED
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.
|
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
|