bezel-app 0.0.6 → 0.0.7
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/bezel-app.gemspec +1 -1
- data/lib/controller_base.rb +55 -54
- data/lib/flash.rb +22 -21
- data/lib/router.rb +26 -24
- data/lib/server_connection.rb +20 -0
- data/lib/session.rb +18 -16
- data/lib/show_exceptions.rb +18 -16
- data/lib/static_assets.rb +32 -30
- data/template/db/seeds.rb +2 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7eb7d3e03695a324803eb0be6125841bbb5d17a5
|
4
|
+
data.tar.gz: 66ab72c4309df61a07d3126f5844ed261f3edbf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57368d376b440714338edfd82730e4f4e52869b308cd9c202ea1f25f8facf132b7236f7bee92d0323898eba7d3ea7ab746b7bce1320aa36bb4dcd32c97f63984
|
7
|
+
data.tar.gz: 3b367b6cf877d6e02d6081da992a56b6f7d4b8d757062c5941fc4d19efda5f541547b2589d732313be3420275a8c618ce0ecfb62c22de04d7d9d5ef68a9eafcf
|
data/bezel-app.gemspec
CHANGED
data/lib/controller_base.rb
CHANGED
@@ -3,76 +3,77 @@ require 'active_support/core_ext'
|
|
3
3
|
require 'erb'
|
4
4
|
require_relative './session'
|
5
5
|
require_relative './flash'
|
6
|
+
module Bezel
|
7
|
+
class ControllerBase
|
8
|
+
attr_reader :req, :res, :params, :flash
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def self.protect_from_forgery
|
11
|
-
@@csrf_auth = true
|
12
|
-
end
|
10
|
+
def self.protect_from_forgery
|
11
|
+
@@csrf_auth = true
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def initialize(req, res, route_params = {})
|
15
|
+
@req = req
|
16
|
+
@res = res
|
17
|
+
@params = req.params.merge(route_params)
|
18
|
+
@flash = Flash.new(req)
|
19
|
+
@params['authenticity_token'] ||= SecureRandom.base64
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def form_authenticity_token
|
23
|
+
@res.set_cookie('authenticity_token',@params['authenticity_token'])
|
24
|
+
@params['authenticity_token']
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def check_authenticity_token(token = "")
|
28
|
+
@params['authenticity_token'] == token
|
29
|
+
end
|
30
30
|
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
def already_built_response?
|
33
|
+
!!@already_built_response
|
34
|
+
end
|
35
35
|
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def redirect_to(url)
|
38
|
+
raise 'You cannot call render more than once' if already_built_response?
|
39
|
+
@res.status = 302
|
40
|
+
@res['Location'] = url
|
41
|
+
@already_built_response = true
|
42
42
|
|
43
|
-
|
44
|
-
|
43
|
+
session.store_session(@res)
|
44
|
+
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
def render_content(content, content_type)
|
47
|
+
raise 'You cannot call render more than once' if already_built_response?
|
48
|
+
@res['Content-Type'] = content_type
|
49
|
+
@res.write(content)
|
50
|
+
@already_built_response = true
|
51
51
|
|
52
|
-
|
53
|
-
|
52
|
+
session.store_session(@res)
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def render(template_name)
|
56
|
+
file_name = "views/"
|
57
|
+
file_name += "#{self.class.to_s.underscore}/"
|
58
|
+
file_name += "#{template_name}.html.erb"
|
59
|
+
content = ERB.new(File.read(file_name)).result(binding)
|
60
60
|
|
61
|
-
|
62
|
-
|
61
|
+
render_content(content, "text/html")
|
62
|
+
end
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
def session
|
65
|
+
@session ||= Session.new(@req)
|
66
|
+
end
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
def invoke_action(name)
|
69
|
+
if @@csrf_auth && @req.request_method != "GET"
|
70
|
+
unless check_authenticity_token(@req.cookies['authenticity_token'])
|
71
|
+
raise "Invalid authenticity token"
|
72
|
+
end
|
72
73
|
end
|
73
|
-
end
|
74
74
|
|
75
|
-
|
76
|
-
|
75
|
+
send(name)
|
76
|
+
render(name) unless already_built_response?
|
77
|
+
end
|
77
78
|
end
|
78
79
|
end
|
data/lib/flash.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
require 'json'
|
2
|
-
|
3
|
-
class Flash
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module Bezel
|
3
|
+
class Flash
|
4
|
+
def initialize(req)
|
5
|
+
if req.cookies['_bezel_flash']
|
6
|
+
@flash = JSON.parse(req.cookies['_bezel_flash'])
|
7
|
+
else
|
8
|
+
@flash = {}
|
9
|
+
end
|
10
|
+
@flash_now = {}
|
9
11
|
end
|
10
|
-
@flash_now = {}
|
11
|
-
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
def []=(key,val)
|
14
|
+
@flash[key] = val
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def [](key)
|
18
|
+
return @flash_now[key] if @flash_now[key]
|
19
|
+
@flash[key.to_s]
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def store_flash(res)
|
23
|
+
res.set_cookie('_bezel_flash', value: @flash.to_json, path: '/')
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
def now
|
27
|
+
@flash_now
|
28
|
+
end
|
28
29
|
end
|
29
30
|
end
|
data/lib/router.rb
CHANGED
@@ -23,38 +23,40 @@ class Route
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
module Bezel
|
27
|
+
class Router
|
28
|
+
attr_reader :routes
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def initialize
|
31
|
+
@routes = []
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def add_route(pattern, method, controller_class, action_name)
|
35
|
+
@routes << Route.new(pattern, method, controller_class, action_name)
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
def draw(&proc)
|
39
|
+
self.instance_eval(&proc)
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
+
[:get, :post, :put, :delete].each do |http_method|
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
define_method(http_method) do |pattern, controller_class, method|
|
45
|
+
add_route(pattern, http_method, controller_class, method)
|
46
|
+
end
|
45
47
|
end
|
46
|
-
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
def match(req)
|
50
|
+
@routes.each do |route|
|
51
|
+
return route if route.matches?(req)
|
52
|
+
end
|
53
|
+
nil
|
51
54
|
end
|
52
|
-
nil
|
53
|
-
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
def run(req, res)
|
57
|
+
route = match(req)
|
58
|
+
return res.status = 404 unless route
|
59
|
+
route.run(req,res)
|
60
|
+
end
|
59
61
|
end
|
60
62
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require './config/routes'
|
3
|
+
|
4
|
+
module Bezel
|
5
|
+
class ServerConnection
|
6
|
+
def self.start
|
7
|
+
app = proc do |env|
|
8
|
+
req = Rack::Request.new(env)
|
9
|
+
res = Rack::Response.new
|
10
|
+
ROUTER.run(req, res)
|
11
|
+
res.finish
|
12
|
+
end
|
13
|
+
|
14
|
+
Rack::Server.start(
|
15
|
+
app: app,
|
16
|
+
Port: 3000
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/session.rb
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Bezel
|
4
|
+
class Session
|
5
|
+
def initialize(req)
|
6
|
+
if req.cookies['_']
|
7
|
+
@cookie = JSON.parse(req.cookies['_bezel'])
|
8
|
+
else
|
9
|
+
@cookie = {}
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def [](key)
|
14
|
+
@cookie[key]
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def []=(key, val)
|
18
|
+
@cookie[key] = val
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def store_session(res)
|
22
|
+
json_cookie = @cookie.to_json
|
23
|
+
res.set_cookie('_bezel', value: json_cookie, path: '/' )
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/show_exceptions.rb
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
require 'erb'
|
2
|
-
|
3
|
-
|
2
|
+
module Bezel
|
3
|
+
class ShowExceptions
|
4
|
+
attr_reader :app
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def call(env)
|
11
|
+
begin
|
12
|
+
app.call(env)
|
13
|
+
rescue Exception => e
|
14
|
+
render_exception(e)
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
+
private
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def render_exception(e)
|
21
|
+
response = Rack::Response.new([], 500, 'Content_Type'=> 'text/html')
|
22
|
+
response.write(e.message)
|
23
|
+
response.finish
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/static_assets.rb
CHANGED
@@ -1,36 +1,38 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
file_path = "." + env['PATH_INFO']
|
8
|
-
res = Rack::Response.new
|
9
|
-
extension = /\.(\w*$)/.match(file_path)[1]
|
10
|
-
begin
|
11
|
-
res['Content-Type'] = set_content_type(extension)
|
12
|
-
content = File.read(file_path)
|
13
|
-
res.write(content)
|
14
|
-
rescue
|
15
|
-
res.status = 404
|
1
|
+
module Bezel
|
2
|
+
class StaticAssets
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
16
5
|
end
|
17
6
|
|
18
|
-
|
7
|
+
def call(env)
|
8
|
+
file_path = "." + env['PATH_INFO']
|
9
|
+
res = Rack::Response.new
|
10
|
+
extension = /\.(\w*$)/.match(file_path)[1]
|
11
|
+
begin
|
12
|
+
res['Content-Type'] = set_content_type(extension)
|
13
|
+
content = File.read(file_path)
|
14
|
+
res.write(content)
|
15
|
+
rescue
|
16
|
+
res.status = 404
|
17
|
+
end
|
18
|
+
|
19
|
+
res.finish
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
21
22
|
|
22
|
-
private
|
23
|
-
def set_content_type(extension)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
23
|
+
private
|
24
|
+
def set_content_type(extension)
|
25
|
+
case extension
|
26
|
+
when "txt"
|
27
|
+
"text/plain"
|
28
|
+
when "jpg"
|
29
|
+
"image/jpeg"
|
30
|
+
when "zip"
|
31
|
+
"application/zip"
|
32
|
+
when "png"
|
33
|
+
"image/png"
|
34
|
+
else
|
35
|
+
raise 'extension not supported'
|
36
|
+
end
|
35
37
|
end
|
36
38
|
end
|
data/template/db/seeds.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bezel-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keith Thompson
|
@@ -172,6 +172,7 @@ files:
|
|
172
172
|
- lib/db_connection.rb
|
173
173
|
- lib/flash.rb
|
174
174
|
- lib/router.rb
|
175
|
+
- lib/server_connection.rb
|
175
176
|
- lib/session.rb
|
176
177
|
- lib/show_exceptions.rb
|
177
178
|
- lib/static_assets.rb
|