flame 4.3.5 → 4.4.2
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/lib/flame/dispatcher.bak.rb +191 -0
- data/lib/flame/dispatcher.rb +17 -20
- data/lib/flame/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87cdb86491b333e614035b432365a68dd2fc6068
|
4
|
+
data.tar.gz: 9fd6f65c981f0a7a964716ed5f7947959bec9a5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e036d843f521289861fb56e6a1a918f8e80b503a08210b72a10a4dfaa4d8f2764d90ce7ba3d7696f180cd6e4297a5287f6020b4394d7396a6cfd53e1d07ddee
|
7
|
+
data.tar.gz: 8e3c7173e9bbd9a489fcc969346e20dfebe436d6538b5cee9a1af6c0289372e41d7ef2d46771227e3eb510b7cf8bbee8a731363e9a00067aa39db875e0d7c7bf
|
@@ -0,0 +1,191 @@
|
|
1
|
+
require 'gorilla-patch/hash'
|
2
|
+
|
3
|
+
require_relative 'cookies'
|
4
|
+
require_relative 'request'
|
5
|
+
require_relative 'response'
|
6
|
+
require_relative 'static'
|
7
|
+
|
8
|
+
module Flame
|
9
|
+
## Helpers for dispatch Flame::Application#call
|
10
|
+
class Dispatcher
|
11
|
+
attr_reader :request, :response
|
12
|
+
|
13
|
+
using GorillaPatch::HashExt
|
14
|
+
|
15
|
+
include Flame::Dispatcher::Static
|
16
|
+
|
17
|
+
## Initialize Dispatcher from Application#call
|
18
|
+
## @param app [Flame::Application] application object
|
19
|
+
## @param env Rack-environment object
|
20
|
+
def initialize(app, env)
|
21
|
+
@app = app
|
22
|
+
@env = env
|
23
|
+
@request = Flame::Request.new(env)
|
24
|
+
@response = Flame::Response.new
|
25
|
+
end
|
26
|
+
|
27
|
+
## Start of execution the request
|
28
|
+
def run!
|
29
|
+
catch :halt do
|
30
|
+
try_route ||
|
31
|
+
try_static ||
|
32
|
+
try_static(File.join(__dir__, '..', '..', 'public')) ||
|
33
|
+
not_found
|
34
|
+
end
|
35
|
+
response.write body
|
36
|
+
response.finish
|
37
|
+
end
|
38
|
+
|
39
|
+
## Acccess to the status of response
|
40
|
+
## @param value [Ineger, nil] integer value for new status
|
41
|
+
## @return [Integer] current status
|
42
|
+
## @example Set status value
|
43
|
+
## status 200
|
44
|
+
def status(value = nil)
|
45
|
+
response.status ||= 200
|
46
|
+
response.headers['X-Cascade'] = 'pass' if value == 404
|
47
|
+
value ? response.status = value : response.status
|
48
|
+
end
|
49
|
+
|
50
|
+
## Acccess to the body of response
|
51
|
+
## @param value [String, nil] string value for new body
|
52
|
+
## @return [String] current body
|
53
|
+
## @example Set body value
|
54
|
+
## body 'Hello World!'
|
55
|
+
def body(value = nil)
|
56
|
+
value ? @body = value : @body ||= ''
|
57
|
+
end
|
58
|
+
|
59
|
+
## Parameters of the request
|
60
|
+
def params
|
61
|
+
@params ||= request.params.merge(request.params.keys_to_sym(deep: true))
|
62
|
+
end
|
63
|
+
|
64
|
+
## Session object as Hash
|
65
|
+
def session
|
66
|
+
request.session
|
67
|
+
end
|
68
|
+
|
69
|
+
## Cookies object as Hash
|
70
|
+
def cookies
|
71
|
+
@cookies ||= Cookies.new(request.cookies, response)
|
72
|
+
end
|
73
|
+
|
74
|
+
## Application-config object as Hash
|
75
|
+
def config
|
76
|
+
@app.config
|
77
|
+
end
|
78
|
+
|
79
|
+
## Access to Content-Type header of response
|
80
|
+
def content_type(ext = nil)
|
81
|
+
return response[Rack::CONTENT_TYPE] unless ext
|
82
|
+
response[Rack::CONTENT_TYPE] = Rack::Mime.mime_type(ext)
|
83
|
+
end
|
84
|
+
|
85
|
+
## Build a path to the given controller and action, with any expected params
|
86
|
+
##
|
87
|
+
## @param ctrl [Flame::Controller] class of controller
|
88
|
+
## @param action [Symbol] method of controller
|
89
|
+
## @param args [Hash] parameters for method of controller
|
90
|
+
## @return [String] path for requested method, controller and parameters
|
91
|
+
## @example Path for `show(id)` method of `ArticlesController` with `id: 2`
|
92
|
+
## path_to ArticlesController, :show, id: 2 # => "/articles/show/2"
|
93
|
+
def path_to(ctrl, action = :index, args = {})
|
94
|
+
route = @app.class.router.find_route(controller: ctrl, action: action)
|
95
|
+
raise Errors::RouteNotFoundError.new(ctrl, action) unless route
|
96
|
+
path = route.assign_arguments(args)
|
97
|
+
path.empty? ? '/' : path
|
98
|
+
end
|
99
|
+
|
100
|
+
## Interrupt the execution of route, and set new optional data
|
101
|
+
## (otherwise using existing)
|
102
|
+
## @param new_status_or_body [Integer, String]
|
103
|
+
## set new HTTP status code or new body
|
104
|
+
## @param new_body [String] set new body
|
105
|
+
## @param new_headers [String] merge new headers
|
106
|
+
## @example Halt with 500, no change body
|
107
|
+
## halt 500
|
108
|
+
## @example Halt with 404, render template
|
109
|
+
## halt 404, render('errors/404')
|
110
|
+
## @example Halt with 200, set new headers
|
111
|
+
## halt 200, 'Cats!', 'Content-Type' => 'animal/cat'
|
112
|
+
def halt(new_status_or_body = nil, new_body = nil, new_headers = {})
|
113
|
+
case new_status_or_body
|
114
|
+
when String then new_body = new_status_or_body
|
115
|
+
when Integer then status new_status_or_body
|
116
|
+
end
|
117
|
+
# new_status.is_a?(String) ? () : (status new_status)
|
118
|
+
new_body = default_body if new_body.nil? && body.empty?
|
119
|
+
body new_body if new_body
|
120
|
+
response.headers.merge!(new_headers)
|
121
|
+
throw :halt
|
122
|
+
end
|
123
|
+
|
124
|
+
## Add error's backtrace to @env['rack.errors'] (terminal or file)
|
125
|
+
## @param error [Exception] exception for class, message and backtrace
|
126
|
+
def dump_error(error)
|
127
|
+
@error_message = [
|
128
|
+
"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " \
|
129
|
+
"#{error.class} - #{error.message}:",
|
130
|
+
*error.backtrace
|
131
|
+
].join("\n\t")
|
132
|
+
@env['rack.errors'].puts(@error_message)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
## Generate default body of error page
|
138
|
+
def default_body
|
139
|
+
## Return nil if must be no body for current HTTP status
|
140
|
+
return if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
|
141
|
+
response.headers[Rack::CONTENT_TYPE] = 'text/html'
|
142
|
+
"<h1>#{Rack::Utils::HTTP_STATUS_CODES[status]}</h1>"
|
143
|
+
end
|
144
|
+
|
145
|
+
## Find route and try execute it
|
146
|
+
def try_route
|
147
|
+
route = @app.class.router.find_route(
|
148
|
+
method: request.http_method,
|
149
|
+
path_parts: request.path_parts
|
150
|
+
)
|
151
|
+
return nil unless route
|
152
|
+
execute_route(route)
|
153
|
+
end
|
154
|
+
|
155
|
+
## Execute route
|
156
|
+
## @param route [Flame::Route] route that must be executed
|
157
|
+
def execute_route(route)
|
158
|
+
status 200
|
159
|
+
params.merge!(route.arguments(request.path_parts))
|
160
|
+
# route.execute(self)
|
161
|
+
route_exec(route)
|
162
|
+
rescue => exception
|
163
|
+
# p 'rescue from dispatcher'
|
164
|
+
dump_error(exception) unless @error_message
|
165
|
+
halt 500
|
166
|
+
|
167
|
+
# p 're raise exception from dispatcher'
|
168
|
+
# raise exception
|
169
|
+
end
|
170
|
+
|
171
|
+
## Generate a response if the route is not found
|
172
|
+
def not_found
|
173
|
+
# p 'not found from dispatcher'
|
174
|
+
## Change the status of response to 404
|
175
|
+
status 404
|
176
|
+
## Find the nearest route by the parts of requested path
|
177
|
+
route = @app.router.find_nearest_route(request.path_parts)
|
178
|
+
## Halt with default body if the route not found
|
179
|
+
## or it's `not_found` method not defined
|
180
|
+
return halt unless route && route.controller.method_defined?(:not_found)
|
181
|
+
## Execute `not_found` method for the founded route
|
182
|
+
route_exec(route, :not_found)
|
183
|
+
end
|
184
|
+
|
185
|
+
## Create controller object and execute method
|
186
|
+
def route_exec(route, method = nil)
|
187
|
+
method ||= route.action
|
188
|
+
route.controller.new(self).send(:execute, method)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
data/lib/flame/dispatcher.rb
CHANGED
@@ -30,7 +30,7 @@ module Flame
|
|
30
30
|
try_route ||
|
31
31
|
try_static ||
|
32
32
|
try_static(File.join(__dir__, '..', '..', 'public')) ||
|
33
|
-
|
33
|
+
halt(404)
|
34
34
|
end
|
35
35
|
response.write body
|
36
36
|
response.finish
|
@@ -115,8 +115,8 @@ module Flame
|
|
115
115
|
when Integer then status new_status_or_body
|
116
116
|
end
|
117
117
|
# new_status.is_a?(String) ? () : (status new_status)
|
118
|
-
new_body = default_body if new_body.nil? && body.empty?
|
119
118
|
body new_body if new_body
|
119
|
+
default_body_of_nearest_route if body.empty?
|
120
120
|
response.headers.merge!(new_headers)
|
121
121
|
throw :halt
|
122
122
|
end
|
@@ -132,16 +132,14 @@ module Flame
|
|
132
132
|
@env['rack.errors'].puts(@error_message)
|
133
133
|
end
|
134
134
|
|
135
|
-
private
|
136
|
-
|
137
135
|
## Generate default body of error page
|
138
136
|
def default_body
|
139
|
-
|
140
|
-
return if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
|
141
|
-
response.headers[Rack::CONTENT_TYPE] = 'text/html'
|
137
|
+
# response.headers[Rack::CONTENT_TYPE] = 'text/html'
|
142
138
|
"<h1>#{Rack::Utils::HTTP_STATUS_CODES[status]}</h1>"
|
143
139
|
end
|
144
140
|
|
141
|
+
private
|
142
|
+
|
145
143
|
## Find route and try execute it
|
146
144
|
def try_route
|
147
145
|
route = @app.class.router.find_route(
|
@@ -168,23 +166,22 @@ module Flame
|
|
168
166
|
# raise exception
|
169
167
|
end
|
170
168
|
|
171
|
-
## Generate a
|
172
|
-
def
|
173
|
-
|
174
|
-
|
175
|
-
status 404
|
169
|
+
## Generate a default body of nearest route
|
170
|
+
def default_body_of_nearest_route
|
171
|
+
## Return nil if must be no body for current HTTP status
|
172
|
+
return if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
|
176
173
|
## Find the nearest route by the parts of requested path
|
177
174
|
route = @app.router.find_nearest_route(request.path_parts)
|
178
|
-
##
|
179
|
-
## or it's `
|
180
|
-
return
|
181
|
-
## Execute `
|
182
|
-
route_exec(route, :
|
175
|
+
## Return nil if the route not found
|
176
|
+
## or it's `default_body` method not defined
|
177
|
+
return default_body unless route
|
178
|
+
## Execute `default_body` method for the founded route
|
179
|
+
route_exec(route, :default_body)
|
180
|
+
body default_body if body.empty?
|
183
181
|
end
|
184
182
|
|
185
|
-
## Create controller object and execute method
|
186
|
-
def route_exec(route, method =
|
187
|
-
method ||= route.action
|
183
|
+
## Create controller object and execute method with arguments
|
184
|
+
def route_exec(route, method = route.action)
|
188
185
|
route.controller.new(self).send(:execute, method)
|
189
186
|
end
|
190
187
|
end
|
data/lib/flame/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Popov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/flame/application.rb
|
93
93
|
- lib/flame/controller.rb
|
94
94
|
- lib/flame/cookies.rb
|
95
|
+
- lib/flame/dispatcher.bak.rb
|
95
96
|
- lib/flame/dispatcher.rb
|
96
97
|
- lib/flame/errors.rb
|
97
98
|
- lib/flame/render.rb
|