yaframework 0.4.6 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +3 -1
- data/examples/README.md +29 -5
- data/examples/before_after_hooks.rb +20 -0
- data/examples/content_types.rb +2 -1
- data/examples/cookies.rb +5 -1
- data/examples/error_handling.rb +2 -2
- data/examples/halts_and_redirects.rb +1 -1
- data/examples/rendering/app.rb +10 -0
- data/examples/rendering/config.ru +3 -0
- data/examples/rendering/views/index.erb +0 -0
- data/examples/rendering/views/layout.erb +0 -0
- data/lib/yaframework.rb +1 -0
- data/lib/yaframework/base.rb +54 -8
- data/lib/yaframework/render.rb +48 -0
- data/lib/yaframework/version.rb +1 -1
- data/yaframework.gemspec +2 -1
- metadata +21 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 374739919ee6fdbc46ee3fc95f5604c162a358d21d593b6709cefa1add1af42a
|
4
|
+
data.tar.gz: 1a021b3a32f2358f5444759b30316e6670cd300121a265e95787889e58fdcc70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdca19d28dfdbc2e980357242811bda91e685b383f859e19b0aafdb2e9f66d69eab5c2201f5a9da751648bf6b667b33ffef6a494555a6258bd4dd36ed4efc914
|
7
|
+
data.tar.gz: 2029fec4b50c8c9039f7b4449c7ad09198a07e4f979cd36b5b197e8523846c5a3155038b4e7aa8d13e012397003b3822a1f630ceecf2d941f5c412f86f726114
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## [0.5.0] - 2021-07-25
|
2
|
+
|
3
|
+
- Templates rendering added
|
4
|
+
|
5
|
+
## [0.4.9] - 2021-07-25
|
6
|
+
|
7
|
+
- Some useful aliases added
|
8
|
+
|
9
|
+
## [0.4.8] - 2021-07-25
|
10
|
+
|
11
|
+
- Before/after hooks added
|
12
|
+
|
13
|
+
## [0.4.7] - 2021-07-25
|
14
|
+
|
15
|
+
- Handling 404 error fixed
|
16
|
+
|
1
17
|
## [0.4.6] - 2021-07-25
|
2
18
|
|
3
19
|
- Content duplication fixed
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
yaframework (0.
|
4
|
+
yaframework (0.5.0)
|
5
5
|
rack (~> 2.2, >= 2.2.3)
|
6
|
+
tilt (~> 2.0)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
@@ -29,6 +30,7 @@ GEM
|
|
29
30
|
rubocop-ast (1.7.0)
|
30
31
|
parser (>= 3.0.1.1)
|
31
32
|
ruby-progressbar (1.11.0)
|
33
|
+
tilt (2.0.10)
|
32
34
|
unicode-display_width (2.0.0)
|
33
35
|
|
34
36
|
PLATFORMS
|
data/examples/README.md
CHANGED
@@ -2,21 +2,46 @@
|
|
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
|
+
|
40
|
+
8. [**Hooks**](https://github.com/maxbarsukov/yaframework/blob/master/examples/before_after_hooks.rb)
|
41
|
+
|
42
|
+
How to use `before` and `after` hooks
|
43
|
+
|
44
|
+
|
20
45
|
## Installation
|
21
46
|
|
22
47
|
Clone this repo and go to this folder.
|
@@ -25,4 +50,3 @@ Then, run `bundle install` to install this gem.
|
|
25
50
|
## Run
|
26
51
|
|
27
52
|
Run with `ruby hello-world.rb` or any other file and view at http://localhost:4567
|
28
|
-
|
@@ -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/content_types.rb
CHANGED
@@ -8,7 +8,8 @@ app.get "/" do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
app.get "/html" do
|
11
|
-
response.html "This is <b>HTML</b>, where you cat use some <small>tags</small
|
11
|
+
response.html "This is <b>HTML</b>, where you cat use some <small>tags</small>.
|
12
|
+
<br/>JSON <a href=\"/json\">here</a>,
|
12
13
|
<br/>Plain text <a href=\"/text\">here</a>"
|
13
14
|
end
|
14
15
|
|
data/examples/cookies.rb
CHANGED
@@ -5,8 +5,12 @@ app = Yaframework::Application
|
|
5
5
|
|
6
6
|
app.get "/" do
|
7
7
|
response.set_cookie("foo", "bar")
|
8
|
-
|
8
|
+
"Your cookies, sir: #{response["Set-Cookie"]}.<br/>Go <a href=\"/delete\">here</a> to delete them"
|
9
9
|
end
|
10
10
|
|
11
|
+
app.get "/delete" do
|
12
|
+
response.delete_cookie("foo")
|
13
|
+
"Your cookies, sir: #{response["Set-Cookie"]}"
|
14
|
+
end
|
11
15
|
|
12
16
|
app.listen(4567)
|
data/examples/error_handling.rb
CHANGED
@@ -4,11 +4,11 @@ require "yaframework"
|
|
4
4
|
app = Yaframework::Application
|
5
5
|
|
6
6
|
app.get "/" do
|
7
|
-
"This is the root, go somewhere else"
|
7
|
+
"This is the root, go somewhere else..<br/>Maybe <a href=\"/asdfg\">here</a>, idk"
|
8
8
|
end
|
9
9
|
|
10
10
|
app.handle 404 do
|
11
|
-
"Hey, I just handled a 404 error!"
|
11
|
+
"Hey, I just handled a 404 error!<br/>The dude from the previous page deceived you!"
|
12
12
|
end
|
13
13
|
|
14
14
|
app.handle 500 do
|
File without changes
|
File without changes
|
data/lib/yaframework.rb
CHANGED
data/lib/yaframework/base.rb
CHANGED
@@ -1,13 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "rack"
|
4
|
+
require_relative "../yaframework/render"
|
4
5
|
|
5
6
|
module Yaframework
|
6
7
|
class Base
|
7
|
-
|
8
|
+
include Yaframework::Render
|
9
|
+
|
10
|
+
attr_reader :routes, :request, :response, :env, :settings
|
11
|
+
alias req request
|
12
|
+
alias res response
|
8
13
|
|
9
14
|
def initialize
|
10
15
|
@routes = Hash.new([])
|
16
|
+
@before_hooks = []
|
17
|
+
@after_hooks = []
|
18
|
+
@settings = {}
|
11
19
|
@inbox = {}
|
12
20
|
end
|
13
21
|
|
@@ -28,6 +36,18 @@ module Yaframework
|
|
28
36
|
throw :halt, response
|
29
37
|
end
|
30
38
|
|
39
|
+
def before(&block)
|
40
|
+
@before_hooks << block
|
41
|
+
end
|
42
|
+
|
43
|
+
def after(&block)
|
44
|
+
@after_hooks << block
|
45
|
+
end
|
46
|
+
|
47
|
+
def params
|
48
|
+
request.params
|
49
|
+
end
|
50
|
+
|
31
51
|
def handle(status, &block)
|
32
52
|
@inbox[status] = block
|
33
53
|
end
|
@@ -55,17 +75,18 @@ module Yaframework
|
|
55
75
|
end
|
56
76
|
|
57
77
|
def route_eval
|
78
|
+
route = find_route
|
79
|
+
response.status = 404 unless route
|
80
|
+
|
58
81
|
if @inbox[response.status]
|
59
|
-
response.write
|
82
|
+
response.write exec(@inbox[response.status])
|
60
83
|
return response.finish
|
61
84
|
end
|
62
85
|
|
63
|
-
|
64
|
-
if route
|
65
|
-
|
66
|
-
|
67
|
-
response.status = 404
|
68
|
-
end
|
86
|
+
exec_before_hooks
|
87
|
+
response.write exec(route[:handler]) if route
|
88
|
+
exec_after_hooks
|
89
|
+
|
69
90
|
response.finish
|
70
91
|
end
|
71
92
|
|
@@ -82,7 +103,32 @@ module Yaframework
|
|
82
103
|
end
|
83
104
|
route
|
84
105
|
end
|
106
|
+
|
107
|
+
def exec(action)
|
108
|
+
if action.respond_to? :to_sym
|
109
|
+
send(action)
|
110
|
+
else
|
111
|
+
instance_exec(&action)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def exec_before_hooks
|
116
|
+
exec_hooks @before_hooks
|
117
|
+
end
|
118
|
+
|
119
|
+
def exec_after_hooks
|
120
|
+
exec_hooks @after_hooks
|
121
|
+
end
|
122
|
+
|
123
|
+
def exec_hooks(hooks)
|
124
|
+
return true if hooks.nil?
|
125
|
+
|
126
|
+
hooks.each do |hook|
|
127
|
+
return false if exec(hook) == false
|
128
|
+
end
|
129
|
+
end
|
85
130
|
end
|
86
131
|
|
87
132
|
Application = Base.new
|
133
|
+
Application.setup
|
88
134
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "tilt"
|
4
|
+
|
5
|
+
module Yaframework
|
6
|
+
module Render
|
7
|
+
def setup(settings = {})
|
8
|
+
@settings = settings
|
9
|
+
@settings[:template_engine] ||= "erb"
|
10
|
+
@settings[:layout] ||= "layout"
|
11
|
+
@settings[:views] ||= File.expand_path("views", File.dirname($PROGRAM_NAME))
|
12
|
+
@settings[:options] ||= {
|
13
|
+
default_encoding: Encoding.default_external
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(template, locals = {}, layout = @settings[:layout])
|
18
|
+
res.headers["Content-Type"] ||= "text/html; charset=utf-8"
|
19
|
+
res.write(view(template, locals, layout))
|
20
|
+
end
|
21
|
+
|
22
|
+
def view(template, locals = {}, layout = @settings[:layout])
|
23
|
+
partial(layout, locals.merge(content: partial(template, locals)))
|
24
|
+
end
|
25
|
+
|
26
|
+
def partial(template, locals = {})
|
27
|
+
_render(template_path(template), locals, @settings[:options])
|
28
|
+
end
|
29
|
+
|
30
|
+
def template_path(template)
|
31
|
+
dir = @settings[:views]
|
32
|
+
ext = @settings[:template_engine]
|
33
|
+
File.join(dir, "#{template}.#{ext}")
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def _render(template, locals = {}, options = {}, &block)
|
39
|
+
_cache.fetch(template) {
|
40
|
+
Tilt.new(template, 1, options.merge(outvar: "@_output"))
|
41
|
+
}.render(self, locals, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def _cache
|
45
|
+
Thread.current[:_cache] ||= Tilt::Cache.new
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/yaframework/version.rb
CHANGED
data/yaframework.gemspec
CHANGED
@@ -33,5 +33,6 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_development_dependency "rake", "~> 13.0"
|
34
34
|
spec.add_development_dependency "rubocop", "~> 1.7"
|
35
35
|
|
36
|
-
spec.
|
36
|
+
spec.add_dependency "rack", "~> 2.2", ">= 2.2.3"
|
37
|
+
spec.add_dependency "tilt", "~> 2.0"
|
37
38
|
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
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxbarsukov
|
@@ -86,6 +86,20 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 2.2.3
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: tilt
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '2.0'
|
96
|
+
type: :runtime
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '2.0'
|
89
103
|
description: What could be better than one more Sinatra?
|
90
104
|
email:
|
91
105
|
- maximbarsukov@bk.ru
|
@@ -107,6 +121,7 @@ files:
|
|
107
121
|
- bin/setup
|
108
122
|
- examples/Gemfile
|
109
123
|
- examples/README.md
|
124
|
+
- examples/before_after_hooks.rb
|
110
125
|
- examples/content_types.rb
|
111
126
|
- examples/cookies.rb
|
112
127
|
- examples/error_handling.rb
|
@@ -114,8 +129,13 @@ files:
|
|
114
129
|
- examples/headers.rb
|
115
130
|
- examples/hello_world.rb
|
116
131
|
- examples/params.rb
|
132
|
+
- examples/rendering/app.rb
|
133
|
+
- examples/rendering/config.ru
|
134
|
+
- examples/rendering/views/index.erb
|
135
|
+
- examples/rendering/views/layout.erb
|
117
136
|
- lib/yaframework.rb
|
118
137
|
- lib/yaframework/base.rb
|
138
|
+
- lib/yaframework/render.rb
|
119
139
|
- lib/yaframework/request.rb
|
120
140
|
- lib/yaframework/response.rb
|
121
141
|
- lib/yaframework/version.rb
|