rack-app 5.4.0 → 5.5.1
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/VERSION +1 -1
- data/lib/rack/app/constants.rb +15 -3
- data/lib/rack/app/endpoint.rb +4 -0
- data/lib/rack/app/endpoint/builder.rb +1 -1
- data/lib/rack/app/endpoint/config.rb +5 -2
- data/lib/rack/app/endpoint/executor.rb +8 -4
- data/lib/rack/app/middlewares.rb +1 -0
- data/lib/rack/app/middlewares/configuration.rb +19 -14
- data/lib/rack/app/middlewares/configuration/handler_setter.rb +22 -0
- data/lib/rack/app/middlewares/configuration/serializer_setter.rb +21 -0
- data/lib/rack/app/middlewares/path_info_cutter.rb +15 -0
- data/lib/rack/app/params.rb +20 -15
- data/lib/rack/app/request_configurator.rb +17 -7
- data/lib/rack/app/router.rb +1 -0
- data/lib/rack/app/router/base.rb +28 -9
- data/lib/rack/app/router/dynamic.rb +14 -3
- data/lib/rack/app/router/not_found.rb +6 -10
- data/lib/rack/app/router/static.rb +6 -3
- data/lib/rack/app/serializer.rb +48 -5
- data/lib/rack/app/serializer/formats_builder.rb +39 -0
- data/lib/rack/app/singleton_methods.rb +2 -0
- data/lib/rack/app/singleton_methods/formats.rb +17 -0
- data/lib/rack/app/singleton_methods/inheritance.rb +2 -1
- data/lib/rack/app/singleton_methods/rack_interface.rb +0 -1
- data/lib/rack/app/singleton_methods/route_handling.rb +0 -1
- data/lib/rack/app/singleton_methods/settings.rb +0 -6
- data/lib/rack/app/streamer.rb +3 -2
- metadata +7 -3
- data/lib/rack/app/middlewares/configuration/path_info_formatter.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4b3d25d410555551a8d81c6be4c797cb28d9a2b
|
4
|
+
data.tar.gz: 6b4c65de92a11cfcf01d7a613674797e855a0ffa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d84f255799c2c240f6245732a2ae00701c25584bb9eb37797206bbc88a2b69f3584d5ca441638699d7e5c33e090f24f2d3b84ebc564a170e85f420fd917a8386
|
7
|
+
data.tar.gz: 1a0db3ae2452a57f87c11f02613f8491b3659131d2af2d61ac41f1ec7ab0d0eb0599ffdb510fac27188c91f5b220f214dab0754386a72690d5591b338eac1363
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
5.
|
1
|
+
5.5.1
|
data/lib/rack/app/constants.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
module Rack::App::Constants
|
2
2
|
|
3
|
+
def self.rack_constant(constant_name, fallback_value)
|
4
|
+
::Rack.const_get(constant_name)
|
5
|
+
rescue NameError
|
6
|
+
fallback_value.freeze
|
7
|
+
end
|
8
|
+
|
3
9
|
module HTTP
|
4
10
|
|
5
11
|
module METHOD
|
@@ -18,13 +24,19 @@ module Rack::App::Constants
|
|
18
24
|
|
19
25
|
METHODS = (METHOD.constants - [:ANY]).map(&:to_s).freeze
|
20
26
|
|
27
|
+
module Headers
|
28
|
+
CONTENT_TYPE = Rack::App::Constants.rack_constant(:CONTENT_TYPE, "Content-Type")
|
29
|
+
end
|
30
|
+
|
21
31
|
end
|
22
32
|
|
23
33
|
module ENV
|
24
|
-
PATH_INFO = (defined?(::Rack::PATH_INFO) ? ::Rack::PATH_INFO : "PATH_INFO".freeze)
|
25
|
-
REQUEST_METHOD = (defined?(::Rack::REQUEST_METHOD) ? ::Rack::REQUEST_METHOD : "REQUEST_METHOD".freeze)
|
26
|
-
REQUEST_PATH = (defined?(::Rack::REQUEST_PATH) ? ::Rack::REQUEST_PATH : "REQUEST_PATH".freeze)
|
27
34
|
|
35
|
+
PATH_INFO = Rack::App::Constants.rack_constant(:PATH_INFO, "PATH_INFO")
|
36
|
+
REQUEST_PATH = Rack::App::Constants.rack_constant(:REQUEST_PATH, "REQUEST_PATH")
|
37
|
+
REQUEST_METHOD = Rack::App::Constants.rack_constant(:REQUEST_METHOD, "REQUEST_METHOD")
|
38
|
+
|
39
|
+
EXTNAME = 'rack-app.extname'.freeze
|
28
40
|
REQUEST_HANDLER = 'rack-app.handler'
|
29
41
|
SERIALIZER = 'rack-app.serializer'
|
30
42
|
PARSED_PARAMS = 'rack-app.parsed_params'
|
data/lib/rack/app/endpoint.rb
CHANGED
@@ -19,7 +19,7 @@ class Rack::App::Endpoint::Builder
|
|
19
19
|
builder_blocks.each do |builder_block|
|
20
20
|
builder_block.call(builder)
|
21
21
|
end
|
22
|
-
builder.use(Rack::App::Middlewares::Configuration, @config
|
22
|
+
builder.use(Rack::App::Middlewares::Configuration, @config)
|
23
23
|
apply_hook_middlewares(builder)
|
24
24
|
end
|
25
25
|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
class Rack::App::Endpoint::Config
|
2
2
|
|
3
3
|
def to_hash
|
4
|
-
serializer
|
5
4
|
error_handler
|
6
5
|
middleware_builders_blocks
|
7
6
|
request_path
|
@@ -18,7 +17,11 @@ class Rack::App::Endpoint::Config
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def serializer
|
21
|
-
|
20
|
+
serializer_builder.to_serializer
|
21
|
+
end
|
22
|
+
|
23
|
+
def serializer_builder
|
24
|
+
@raw[:serializer_builder] ||= app_class.__send__(:formats)
|
22
25
|
end
|
23
26
|
|
24
27
|
def error_handler
|
@@ -12,7 +12,7 @@ class Rack::App::Endpoint::Executor
|
|
12
12
|
|
13
13
|
def execute(env)
|
14
14
|
request_handler = env[Rack::App::Constants::ENV::REQUEST_HANDLER]
|
15
|
-
set_response_body(request_handler
|
15
|
+
set_response_body(request_handler, get_response_body(request_handler))
|
16
16
|
return request_handler.response
|
17
17
|
end
|
18
18
|
|
@@ -24,8 +24,13 @@ class Rack::App::Endpoint::Executor
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
EXTNAME = ::Rack::App::Constants::ENV::EXTNAME
|
28
|
+
CONTENT_TYPE = ::Rack::App::Constants::HTTP::Headers::CONTENT_TYPE
|
29
|
+
|
30
|
+
def set_response_body(handler, response_body)
|
31
|
+
extname = handler.request.env[EXTNAME]
|
32
|
+
handler.response.headers.merge!(@endpoint_properties.serializer.response_headers_for(extname))
|
33
|
+
handler.response.write(@endpoint_properties.serializer.serialize(extname, response_body))
|
29
34
|
end
|
30
35
|
|
31
36
|
def evaluate_value(request_handler)
|
@@ -34,5 +39,4 @@ class Rack::App::Endpoint::Executor
|
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
37
|
-
|
38
42
|
end
|
data/lib/rack/app/middlewares.rb
CHANGED
@@ -1,29 +1,34 @@
|
|
1
|
+
require "rack/builder"
|
1
2
|
require "rack/request"
|
2
3
|
require "rack/response"
|
3
4
|
class Rack::App::Middlewares::Configuration
|
4
5
|
|
5
|
-
require "rack/app/middlewares/configuration/
|
6
|
+
require "rack/app/middlewares/configuration/handler_setter"
|
7
|
+
require "rack/app/middlewares/configuration/serializer_setter"
|
8
|
+
|
6
9
|
require "rack/app/middlewares/configuration/path_params_matcher"
|
7
10
|
|
8
|
-
def initialize(app,
|
9
|
-
@
|
10
|
-
|
11
|
-
|
11
|
+
def initialize(app, config)
|
12
|
+
@stack = build_stack(app) do |builder|
|
13
|
+
builder.use Rack::App::Middlewares::Configuration::SerializerSetter,
|
14
|
+
config.serializer
|
15
|
+
|
16
|
+
builder.use Rack::App::Middlewares::Configuration::HandlerSetter,
|
17
|
+
config.app_class
|
18
|
+
|
19
|
+
end
|
12
20
|
end
|
13
21
|
|
14
22
|
def call(env)
|
15
|
-
env
|
16
|
-
env[Rack::App::Constants::ENV::REQUEST_HANDLER]= handler(env)
|
17
|
-
@app.call(env)
|
23
|
+
@stack.call(env)
|
18
24
|
end
|
19
25
|
|
20
26
|
protected
|
21
27
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
28
|
+
def build_stack(app)
|
29
|
+
builder = Rack::Builder.new
|
30
|
+
yield(builder)
|
31
|
+
builder.run(app)
|
32
|
+
return builder.to_app
|
27
33
|
end
|
28
|
-
|
29
34
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Rack::App::Middlewares::Configuration::HandlerSetter
|
2
|
+
|
3
|
+
def initialize(app, handler_class)
|
4
|
+
@app = app
|
5
|
+
@handler_class = handler_class || raise
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
env[Rack::App::Constants::ENV::REQUEST_HANDLER]= handler(env)
|
10
|
+
@app.call(env)
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def handler(env)
|
16
|
+
new_handler = @handler_class.new
|
17
|
+
new_handler.request = ::Rack::Request.new(env)
|
18
|
+
new_handler.response = ::Rack::Response.new
|
19
|
+
new_handler
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Rack::App::Middlewares::Configuration::SerializerSetter
|
2
|
+
|
3
|
+
def initialize(app, serializer)
|
4
|
+
@app = app
|
5
|
+
@serializer = serializer || raise
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
# env[::Rack::App::Constants::ENV::EXTNAME] ||= extname(env)
|
10
|
+
env[::Rack::App::Constants::ENV::SERIALIZER]= @serializer
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def extname(env)
|
17
|
+
path_info = env[::Rack::App::Constants::ENV::PATH_INFO]
|
18
|
+
File.extname(path_info.split("/").last.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Rack::App::Middlewares::PathInfoCutter
|
2
|
+
|
3
|
+
def initialize(app, cut_string_from_path)
|
4
|
+
@cut_string_from_path = cut_string_from_path
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
#env[::Rack::App::Constants::ENV::ORIGINAL_PATH_INFO]= env[::Rack::App::Constants::ENV::PATH_INFO]
|
10
|
+
env[::Rack::App::Constants::ENV::PATH_INFO]= env[::Rack::App::Constants::ENV::PATH_INFO].dup
|
11
|
+
env[::Rack::App::Constants::ENV::PATH_INFO].sub!(@cut_string_from_path, '')
|
12
|
+
@app.call(env)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/rack/app/params.rb
CHANGED
@@ -2,8 +2,8 @@ require 'cgi'
|
|
2
2
|
class Rack::App::Params
|
3
3
|
|
4
4
|
def to_hash
|
5
|
-
if @
|
6
|
-
@
|
5
|
+
if @env[::Rack::App::Constants::ENV::PARSED_PARAMS]
|
6
|
+
@env[::Rack::App::Constants::ENV::PARSED_PARAMS]
|
7
7
|
else
|
8
8
|
query_params.merge(request_path_params)
|
9
9
|
end
|
@@ -11,24 +11,21 @@ class Rack::App::Params
|
|
11
11
|
|
12
12
|
protected
|
13
13
|
|
14
|
-
def initialize(
|
15
|
-
@
|
14
|
+
def initialize(env)
|
15
|
+
@env = env
|
16
16
|
end
|
17
17
|
|
18
18
|
def query_params
|
19
19
|
raw_cgi_params.reduce({}) do |params_collection, (k, v)|
|
20
|
+
stripped_key = k.sub(/\[\]$/, '')
|
20
21
|
|
21
|
-
if single_paramter_value?(v)
|
22
|
-
params_collection[
|
23
|
-
|
22
|
+
if single_paramter_value?(v) && !k.end_with?("[]")
|
23
|
+
params_collection[stripped_key]= v[0]
|
24
24
|
else
|
25
|
-
|
26
|
-
params_collection[k]= v
|
27
|
-
|
25
|
+
params_collection[stripped_key]= v
|
28
26
|
end
|
29
27
|
|
30
28
|
params_collection
|
31
|
-
|
32
29
|
end
|
33
30
|
end
|
34
31
|
|
@@ -37,7 +34,7 @@ class Rack::App::Params
|
|
37
34
|
end
|
38
35
|
|
39
36
|
def raw_cgi_params
|
40
|
-
CGI.parse(@
|
37
|
+
CGI.parse(@env[::Rack::QUERY_STRING].to_s)
|
41
38
|
end
|
42
39
|
|
43
40
|
def request_path_params
|
@@ -47,18 +44,26 @@ class Rack::App::Params
|
|
47
44
|
end
|
48
45
|
|
49
46
|
def extract_path_params
|
47
|
+
last_index = request_path_parts.length - 1
|
50
48
|
request_path_parts.each.with_index.reduce({}) do |params_col, (path_part, index)|
|
51
|
-
|
49
|
+
if path_params_matcher[index]
|
50
|
+
if index == last_index && @env[::Rack::App::Constants::ENV::EXTNAME]
|
51
|
+
matcher = Regexp.escape(@env[::Rack::App::Constants::ENV::EXTNAME])
|
52
|
+
path_part = path_part.sub(/#{matcher}$/,'')
|
53
|
+
end
|
54
|
+
params_col[path_params_matcher[index]]= path_part
|
55
|
+
end
|
56
|
+
|
52
57
|
params_col
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
56
61
|
def request_path_parts
|
57
|
-
@
|
62
|
+
@env[::Rack::App::Constants::ENV::PATH_INFO].split('/')
|
58
63
|
end
|
59
64
|
|
60
65
|
def path_params_matcher
|
61
|
-
@
|
66
|
+
@env[::Rack::App::Constants::ENV::PATH_PARAMS_MATCHER] || {}
|
62
67
|
end
|
63
68
|
|
64
69
|
end
|
@@ -3,16 +3,26 @@ module Rack::App::RequestConfigurator
|
|
3
3
|
extend self
|
4
4
|
|
5
5
|
def configure(env)
|
6
|
-
|
7
|
-
env
|
6
|
+
set_path_info(env)
|
7
|
+
set_extname(env)
|
8
8
|
end
|
9
9
|
|
10
10
|
protected
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
EXTNAME = ::Rack::App::Constants::ENV::EXTNAME
|
13
|
+
PATH_INFO = ::Rack::App::Constants::ENV::PATH_INFO
|
14
|
+
ORIGINAL_PATH_INFO = ::Rack::App::Constants::ENV::ORIGINAL_PATH_INFO
|
15
|
+
|
16
|
+
def set_path_info(env)
|
17
|
+
path_info = env[PATH_INFO]
|
18
|
+
env[ORIGINAL_PATH_INFO]= path_info.dup
|
19
|
+
env[PATH_INFO]= Rack::App::Utils.normalize_path(path_info)
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_extname(env)
|
23
|
+
path_info = env[PATH_INFO]
|
24
|
+
basename = path_info.split("/").last.to_s
|
25
|
+
env[EXTNAME]= File.extname(basename)
|
16
26
|
end
|
17
27
|
|
18
|
-
end
|
28
|
+
end
|
data/lib/rack/app/router.rb
CHANGED
data/lib/rack/app/router/base.rb
CHANGED
@@ -1,17 +1,10 @@
|
|
1
1
|
class Rack::App::Router::Base
|
2
2
|
|
3
3
|
def call(env)
|
4
|
-
|
5
|
-
|
6
|
-
path_info= env[Rack::App::Constants::ENV::PATH_INFO]
|
7
|
-
|
8
|
-
context = fetch_context(request_method, path_info)
|
9
|
-
return unless context.is_a?(Hash) and not context[:app].nil?
|
10
|
-
|
11
|
-
context[:app].call(env)
|
4
|
+
app = get_app(env)
|
5
|
+
app && app.call(env)
|
12
6
|
end
|
13
7
|
|
14
|
-
|
15
8
|
def endpoints
|
16
9
|
@endpoints ||= []
|
17
10
|
end
|
@@ -29,6 +22,10 @@ class Rack::App::Router::Base
|
|
29
22
|
|
30
23
|
protected
|
31
24
|
|
25
|
+
def get_app(env)
|
26
|
+
raise(NotImplementedError)
|
27
|
+
end
|
28
|
+
|
32
29
|
def compile_endpoint!(endpoint)
|
33
30
|
raise(NotImplementedError)
|
34
31
|
end
|
@@ -53,4 +50,26 @@ class Rack::App::Router::Base
|
|
53
50
|
end
|
54
51
|
end
|
55
52
|
|
53
|
+
def find_by_path_infos(env, &block)
|
54
|
+
block.call(raw_path_info(env)) || block.call(formatted_path_info(env))
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_request_method(env)
|
58
|
+
env[::Rack::App::Constants::ENV::REQUEST_METHOD]
|
59
|
+
end
|
60
|
+
|
61
|
+
def raw_path_info(env)
|
62
|
+
env[::Rack::App::Constants::ENV::PATH_INFO]
|
63
|
+
end
|
64
|
+
|
65
|
+
def formatted_path_info(env)
|
66
|
+
path_info = raw_path_info(env).dup
|
67
|
+
path_info.slice!(/#{Regexp.escape(extname(env))}$/)
|
68
|
+
path_info
|
69
|
+
end
|
70
|
+
|
71
|
+
def extname(env)
|
72
|
+
env[::Rack::App::Constants::ENV::EXTNAME]
|
73
|
+
end
|
74
|
+
|
56
75
|
end
|
@@ -70,7 +70,7 @@ class Rack::App::Router::Dynamic < Rack::App::Router::Base
|
|
70
70
|
elsif path_part_is_a_mounted_rack_based_application?(path_part)
|
71
71
|
break_build = true
|
72
72
|
builder.use(
|
73
|
-
Rack::App::Middlewares::
|
73
|
+
Rack::App::Middlewares::PathInfoCutter,
|
74
74
|
calculate_mount_path(request_path_parts)
|
75
75
|
)
|
76
76
|
MOUNTED_APPLICATION
|
@@ -116,14 +116,25 @@ class Rack::App::Router::Dynamic < Rack::App::Router::Base
|
|
116
116
|
::Rack::App::Constants::HTTP::METHODS
|
117
117
|
end
|
118
118
|
|
119
|
+
def get_app(env)
|
120
|
+
find_by_path_infos(env) do |path_info|
|
121
|
+
c = fetch_context(get_request_method(env), path_info)
|
122
|
+
|
123
|
+
if c.is_a?(Hash)
|
124
|
+
c[:app]
|
125
|
+
else
|
126
|
+
nil
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
119
130
|
|
120
131
|
def fetch_context(request_method, path_info)
|
121
|
-
normalized_request_path = Rack::App::Utils.normalize_path(path_info)
|
122
132
|
|
123
133
|
last_mounted_directory = nil
|
124
134
|
last_mounted_app = nil
|
125
135
|
current_cluster = main_cluster(request_method)
|
126
|
-
|
136
|
+
|
137
|
+
path_info.split('/').each do |path_part|
|
127
138
|
|
128
139
|
last_mounted_directory = current_cluster[MOUNTED_DIRECTORY] || last_mounted_directory
|
129
140
|
last_mounted_app = current_cluster[MOUNTED_APPLICATION] || last_mounted_app
|
@@ -1,23 +1,19 @@
|
|
1
1
|
require "rack/response"
|
2
2
|
class Rack::App::Router::NotFound < Rack::App::Router::Base
|
3
3
|
|
4
|
-
def fetch_context(request_method, path_info)
|
5
|
-
{:app => lambda{|env| not_found_response }}
|
6
|
-
end
|
7
|
-
|
8
|
-
def fetch_endpoint(request_method, path_info)
|
9
|
-
::Rack::App::Endpoint::NOT_FOUND
|
10
|
-
end
|
11
|
-
|
12
4
|
protected
|
13
5
|
|
14
|
-
|
6
|
+
NOT_FOUND_APP = lambda do |env|
|
15
7
|
rack_response = Rack::Response.new
|
16
8
|
rack_response.status = 404
|
17
9
|
rack_response.write('404 Not Found')
|
18
10
|
rack_response.finish
|
19
11
|
end
|
20
|
-
|
12
|
+
|
13
|
+
def get_app(env)
|
14
|
+
NOT_FOUND_APP
|
15
|
+
end
|
16
|
+
|
21
17
|
def compile_registered_endpoints!
|
22
18
|
end
|
23
19
|
end
|
@@ -2,9 +2,11 @@ class Rack::App::Router::Static < Rack::App::Router::Base
|
|
2
2
|
|
3
3
|
protected
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
5
|
+
def get_app(env)
|
6
|
+
request_method= get_request_method(env)
|
7
|
+
find_by_path_infos(env) do |request_path|
|
8
|
+
mapped_endpoint_routes[[request_method, request_path]]
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
12
|
def mapped_endpoint_routes
|
@@ -17,6 +19,7 @@ class Rack::App::Router::Static < Rack::App::Router::Base
|
|
17
19
|
|
18
20
|
def compile_endpoint!(endpoint)
|
19
21
|
routing_key = [endpoint.request_method, endpoint.request_path]
|
22
|
+
|
20
23
|
mapped_endpoint_routes[routing_key]= as_app(endpoint)
|
21
24
|
end
|
22
25
|
|
data/lib/rack/app/serializer.rb
CHANGED
@@ -1,13 +1,56 @@
|
|
1
1
|
class Rack::App::Serializer
|
2
2
|
|
3
|
-
|
3
|
+
require "rack/app/serializer/formats_builder"
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
|
5
|
+
def initialize(options={})
|
6
|
+
|
7
|
+
@default_formatter = options[:default_formatter] || lambda { |o| o.to_s }
|
8
|
+
@default_content_type = options[:default_content_type]
|
9
|
+
|
10
|
+
formatters = options[:formatters] || {}
|
11
|
+
content_types = options[:content_types] || {}
|
12
|
+
|
13
|
+
@content_types = Hash.new(@default_content_type)
|
14
|
+
@content_types.merge!(content_types)
|
15
|
+
|
16
|
+
@formatters = Hash.new(@default_formatter)
|
17
|
+
@formatters.merge!(formatters)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def serialize(extname, object)
|
22
|
+
String(@formatters[extname].call(object))
|
23
|
+
end
|
24
|
+
|
25
|
+
CONTENT_TYPE = ::Rack::App::Constants::HTTP::Headers::CONTENT_TYPE
|
26
|
+
|
27
|
+
def response_headers_for(extname)
|
28
|
+
headers = {}
|
29
|
+
add_content_type_for(headers, extname)
|
30
|
+
headers
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_options
|
34
|
+
{
|
35
|
+
:formatters => @formatters,
|
36
|
+
:content_types => @content_types,
|
37
|
+
:default_formatter => @default_formatter,
|
38
|
+
:default_content_type => @default_content_type,
|
39
|
+
}
|
7
40
|
end
|
8
41
|
|
9
|
-
def
|
10
|
-
|
42
|
+
def extnames
|
43
|
+
(@formatters.keys + @content_types.keys).uniq
|
11
44
|
end
|
12
45
|
|
46
|
+
protected
|
47
|
+
|
48
|
+
def add_content_type_for(headers, extname)
|
49
|
+
content_type = @content_types[extname]
|
50
|
+
if content_type
|
51
|
+
headers[CONTENT_TYPE]= content_type.dup
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
13
56
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Rack::App::Serializer::FormatsBuilder
|
2
|
+
|
3
|
+
def initialize
|
4
|
+
@formatters = {}
|
5
|
+
@content_types = {}
|
6
|
+
@default_formatter = lambda { |o| o.to_s }
|
7
|
+
@default_content_type = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def on(extname, response_content_type, &formatter)
|
11
|
+
format = String(extname).downcase
|
12
|
+
extension = (format[0,1] == '.' ? format : ".#{format}")
|
13
|
+
@formatters[extension]= formatter
|
14
|
+
@content_types[extension]= response_content_type
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def default(content_type=nil, &block)
|
19
|
+
@default_content_type = content_type if content_type
|
20
|
+
@default_formatter = block if block
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_serializer
|
25
|
+
Rack::App::Serializer.new(
|
26
|
+
:formatters => @formatters,
|
27
|
+
:content_types => @content_types,
|
28
|
+
:default_formatter => @default_formatter,
|
29
|
+
:default_content_type => @default_content_type
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def merge!(formats_builder)
|
34
|
+
@formatters.merge!(formats_builder.instance_variable_get(:@formatters))
|
35
|
+
@default_formatter = formats_builder.instance_variable_get(:@default_formatter)
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -9,6 +9,7 @@ module Rack::App::SingletonMethods
|
|
9
9
|
require 'rack/app/singleton_methods/route_handling'
|
10
10
|
require 'rack/app/singleton_methods/settings'
|
11
11
|
require 'rack/app/singleton_methods/extensions'
|
12
|
+
require 'rack/app/singleton_methods/formats'
|
12
13
|
require 'rack/app/singleton_methods/hooks'
|
13
14
|
|
14
15
|
include Rack::App::SingletonMethods::HttpMethods
|
@@ -20,6 +21,7 @@ module Rack::App::SingletonMethods
|
|
20
21
|
include Rack::App::SingletonMethods::RouteHandling
|
21
22
|
include Rack::App::SingletonMethods::Settings
|
22
23
|
include Rack::App::SingletonMethods::Extensions
|
24
|
+
include Rack::App::SingletonMethods::Formats
|
23
25
|
include Rack::App::SingletonMethods::Hooks
|
24
26
|
|
25
27
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rack::App::SingletonMethods::Formats
|
2
|
+
|
3
|
+
def formats(&descriptor)
|
4
|
+
@formats_builder ||= Rack::App::Serializer::FormatsBuilder.new
|
5
|
+
unless descriptor.nil?
|
6
|
+
@formats_builder.instance_exec(&descriptor)
|
7
|
+
router.reset
|
8
|
+
end
|
9
|
+
@formats_builder
|
10
|
+
end
|
11
|
+
|
12
|
+
def serializer(default_content_type=nil, &how_to_serialize)
|
13
|
+
formats{ default(default_content_type,&how_to_serialize) } unless how_to_serialize.nil?
|
14
|
+
return formats.to_serializer
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -26,7 +26,6 @@ module Rack::App::SingletonMethods::RouteHandling
|
|
26
26
|
Rack::App::Endpoint.new({
|
27
27
|
:app_class => self,
|
28
28
|
:error_handler => error,
|
29
|
-
:serializer => serializer,
|
30
29
|
:user_defined_logic => block,
|
31
30
|
:request_method => request_method,
|
32
31
|
:route => route_registration_properties.dup,
|
@@ -8,12 +8,6 @@ module Rack::App::SingletonMethods::Settings
|
|
8
8
|
|
9
9
|
protected
|
10
10
|
|
11
|
-
def serializer(&definition_how_to_serialize)
|
12
|
-
@serializer ||= Rack::App::Serializer.new
|
13
|
-
@serializer = Rack::App::Serializer.new(&definition_how_to_serialize) unless definition_how_to_serialize.nil?
|
14
|
-
return @serializer
|
15
|
-
end
|
16
|
-
|
17
11
|
def headers(new_headers=nil)
|
18
12
|
middleware do |b|
|
19
13
|
b.use(Rack::App::Middlewares::HeaderSetter,new_headers)
|
data/lib/rack/app/streamer.rb
CHANGED
@@ -13,7 +13,8 @@ class Rack::App::Streamer
|
|
13
13
|
require "rack/app/streamer/scheduler"
|
14
14
|
|
15
15
|
def initialize(env, options={}, &back)
|
16
|
-
@serializer = env[Rack::App::Constants::ENV::SERIALIZER]
|
16
|
+
@serializer = env[::Rack::App::Constants::ENV::SERIALIZER]
|
17
|
+
@extname = env[::Rack::App::Constants::ENV::EXTNAME]
|
17
18
|
@scheduler = options[:scheduler] || Rack::App::Streamer::Scheduler.by_env(env)
|
18
19
|
@keep_open = options[:keep_open] || false
|
19
20
|
@back = back.to_proc
|
@@ -39,7 +40,7 @@ class Rack::App::Streamer
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def <<(data)
|
42
|
-
@scheduler.schedule { @front.call(@serializer.serialize(data)) }
|
43
|
+
@scheduler.schedule { @front.call(@serializer.serialize(@extname, data)) }
|
43
44
|
self
|
44
45
|
end
|
45
46
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -120,8 +120,9 @@ files:
|
|
120
120
|
- lib/rack/app/instance_methods/streaming.rb
|
121
121
|
- lib/rack/app/middlewares.rb
|
122
122
|
- lib/rack/app/middlewares/configuration.rb
|
123
|
-
- lib/rack/app/middlewares/configuration/
|
123
|
+
- lib/rack/app/middlewares/configuration/handler_setter.rb
|
124
124
|
- lib/rack/app/middlewares/configuration/path_params_matcher.rb
|
125
|
+
- lib/rack/app/middlewares/configuration/serializer_setter.rb
|
125
126
|
- lib/rack/app/middlewares/header_setter.rb
|
126
127
|
- lib/rack/app/middlewares/hooks.rb
|
127
128
|
- lib/rack/app/middlewares/hooks/after.rb
|
@@ -132,6 +133,7 @@ files:
|
|
132
133
|
- lib/rack/app/middlewares/params/parser.rb
|
133
134
|
- lib/rack/app/middlewares/params/setter.rb
|
134
135
|
- lib/rack/app/middlewares/params/validator.rb
|
136
|
+
- lib/rack/app/middlewares/path_info_cutter.rb
|
135
137
|
- lib/rack/app/params.rb
|
136
138
|
- lib/rack/app/request_configurator.rb
|
137
139
|
- lib/rack/app/router.rb
|
@@ -141,8 +143,10 @@ files:
|
|
141
143
|
- lib/rack/app/router/not_found.rb
|
142
144
|
- lib/rack/app/router/static.rb
|
143
145
|
- lib/rack/app/serializer.rb
|
146
|
+
- lib/rack/app/serializer/formats_builder.rb
|
144
147
|
- lib/rack/app/singleton_methods.rb
|
145
148
|
- lib/rack/app/singleton_methods/extensions.rb
|
149
|
+
- lib/rack/app/singleton_methods/formats.rb
|
146
150
|
- lib/rack/app/singleton_methods/hooks.rb
|
147
151
|
- lib/rack/app/singleton_methods/http_methods.rb
|
148
152
|
- lib/rack/app/singleton_methods/inheritance.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class Rack::App::Middlewares::Configuration::PathInfoFormatter
|
2
|
-
|
3
|
-
def initialize(app, cut_string_from_path)
|
4
|
-
@cut_string_from_path = cut_string_from_path
|
5
|
-
@app = app
|
6
|
-
end
|
7
|
-
|
8
|
-
def call(env)
|
9
|
-
env[::Rack::App::Constants::ENV::ORIGINAL_PATH_INFO]= env[::Rack::App::Constants::ENV::PATH_INFO].dup
|
10
|
-
env[::Rack::App::Constants::ENV::PATH_INFO].sub!(@cut_string_from_path, '')
|
11
|
-
@app.call(env)
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|