rack-app 5.0.0.rc2 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b130d9458099d27fab690cf3db1597ada7cac19
4
- data.tar.gz: b05e1b11630efd3ced90361372634d121db73c25
3
+ metadata.gz: 85eef183f233ed422353b4658cc710fcddd6f3fa
4
+ data.tar.gz: c64e1dea7180304029ce7515fdf96fdfa2339d60
5
5
  SHA512:
6
- metadata.gz: d431d736219cce600153c1357dccb58742ac39babdaafadb3a0377954b55a56d5cca796e7c0dcc26dcfa105c4c21003c54b853a71bb3f78ef37e9983600fee3c
7
- data.tar.gz: a8da83f4a90063bb491be7803615d05f795ccea24676700ba9b47073199730e4c6658a3868c59eb3b77508627a2db04e55ab25b45352a8b6280e62067acb2a70
6
+ metadata.gz: c63f77fa26cf15b1a5312afdb6b199fd51d3d5fc79cc5f3bdd85728ce45002a5c8eee7de6ebe110506cf3941c0f7c6573485ef22e10a3d8b550cd31a193391ae
7
+ data.tar.gz: 282c092d984e8202bf4ce0d3cc81202033fa732822032ff804d8ef6241bc01acbfba817ecdb4358ff4210fbc101a8a478b7838fe30eff9e39de00ace2f7340a6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.0.0.rc2
1
+ 5.0.0
@@ -21,6 +21,7 @@ module Rack::App::Constants
21
21
  end
22
22
 
23
23
  module ENV
24
+ REQUEST_HANDLER = 'rack-app.handler'
24
25
  PARSED_PARAMS = 'rack-app.parsed_params'
25
26
  VALIDATED_PARAMS = 'rack-app.validated_params'
26
27
  ORIGINAL_PATH_INFO = 'rack-app.original_path_info'.freeze
@@ -1,26 +1,25 @@
1
1
  require "rack/builder"
2
2
  class Rack::App::Endpoint
3
3
  require "rack/app/endpoint/properties"
4
+ require "rack/app/endpoint/executor"
4
5
 
5
6
  def properties
6
7
  @properties.to_hash
7
8
  end
8
9
 
9
- LAST_MODIFIED_HEADER = "Last-Modified".freeze
10
-
11
10
  def initialize(properties)
12
11
  @properties = Rack::App::Endpoint::Properties.new(properties)
13
- @endpoint_method_name = register_method_to_app_class(properties[:user_defined_logic])
14
12
  end
15
13
 
16
14
  def call(env)
17
15
  to_app.call(env)
18
- end
16
+ end
19
17
 
20
18
  def to_app
21
19
  builder = Rack::Builder.new
22
20
  apply_middleware_build_blocks(builder)
23
- builder.run(lambda { |env| self.call_without_middlewares(env) })
21
+ @properties.endpoint_method_name
22
+ builder.run(Rack::App::Endpoint::Executor.new(@properties))
24
23
  builder.to_app
25
24
  end
26
25
 
@@ -30,47 +29,21 @@ class Rack::App::Endpoint
30
29
  builder_blocks.each do |builder_block|
31
30
  builder_block.call(builder)
32
31
  end
32
+ builder.use(Rack::App::Middlewares::Configuration, @properties.app_class)
33
+ apply_hook_middlewares(builder)
33
34
  end
34
35
 
35
- def builder_blocks
36
- @properties.app_class.middlewares + @properties.middleware_builders_blocks
37
- end
38
-
39
- def call_without_middlewares(env)
40
- request = Rack::Request.new(env)
41
- response = Rack::Response.new
42
- return catch(:rack_response){ execute(request, response) }.finish
43
- end
44
-
45
- def execute(request,response)
46
- request_handler = @properties.app_class.new
47
- request_handler.request = request
48
- request_handler.response = response
49
- set_response_body(response, get_response_body(request_handler))
50
- return response
51
- end
52
-
53
- def get_response_body(request_handler)
54
- catch :response_body do
55
- evaluated_value = evaluate_value(request_handler)
56
-
57
- evaluated_value
36
+ def apply_hook_middlewares(builder)
37
+ @properties.app_class.before.each do |before_block|
38
+ builder.use(Rack::App::Middlewares::Hooks::Before, before_block)
58
39
  end
59
- end
60
-
61
- def set_response_body(response, response_body)
62
- response.write(String(@properties.serializer.serialize(response_body)))
63
- end
64
-
65
- def evaluate_value(request_handler)
66
- @properties.error_handler.execute_with_error_handling_for(request_handler) do
67
- request_handler.__send__(@endpoint_method_name)
40
+ @properties.app_class.after.each do |after_block|
41
+ builder.use(Rack::App::Middlewares::Hooks::After, after_block)
68
42
  end
69
43
  end
70
44
 
71
- def register_method_to_app_class(proc)
72
- method_name = '__' + ::Rack::App::Utils.uuid
73
- @properties.app_class.__send__(:define_method, method_name, &proc)
74
- return method_name
45
+ def builder_blocks
46
+ @properties.app_class.middlewares + @properties.middleware_builders_blocks
75
47
  end
48
+
76
49
  end
@@ -0,0 +1,38 @@
1
+ class Rack::App::Endpoint::Executor
2
+
3
+ def initialize(endpoint_properties)
4
+ @endpoint_properties = endpoint_properties
5
+ end
6
+
7
+ def call(env)
8
+ return catch(:rack_response){ execute(env) }.finish
9
+ end
10
+
11
+ protected
12
+
13
+ def execute(env)
14
+ request_handler = env[Rack::App::Constants::ENV::REQUEST_HANDLER]
15
+ set_response_body(request_handler.response, get_response_body(request_handler))
16
+ return request_handler.response
17
+ end
18
+
19
+ def get_response_body(request_handler)
20
+ catch :response_body do
21
+ evaluated_value = evaluate_value(request_handler)
22
+
23
+ evaluated_value
24
+ end
25
+ end
26
+
27
+ def set_response_body(response, response_body)
28
+ response.write(String(@endpoint_properties.serializer.serialize(response_body)))
29
+ end
30
+
31
+ def evaluate_value(request_handler)
32
+ @endpoint_properties.error_handler.execute_with_error_handling_for(request_handler) do
33
+ request_handler.__send__(@endpoint_properties.endpoint_method_name)
34
+ end
35
+ end
36
+
37
+
38
+ end
@@ -1,6 +1,6 @@
1
1
  class Rack::App::Endpoint::Properties
2
2
 
3
- def to_hash
3
+ def to_hash
4
4
  @raw
5
5
  end
6
6
 
@@ -20,10 +20,24 @@ class Rack::App::Endpoint::Properties
20
20
  @raw[:middleware_builders_blocks] ||= []
21
21
  end
22
22
 
23
+ def endpoint_method_name
24
+ @raw[:method_name] ||= register_method_to_app_class
25
+ end
26
+
23
27
  protected
24
28
 
25
29
  def initialize(raw)
26
30
  @raw = raw
27
31
  end
28
32
 
33
+ def register_method_to_app_class
34
+ method_name = '__' + ::Rack::App::Utils.uuid
35
+ app_class.__send__(:define_method, method_name, &logic_block)
36
+ return method_name
37
+ end
38
+
39
+ def logic_block
40
+ @raw[:user_defined_logic]
41
+ end
42
+
29
43
  end
@@ -1,5 +1,6 @@
1
1
  module Rack::App::Middlewares
2
+ require 'rack/app/middlewares/configuration'
2
3
  require 'rack/app/middlewares/header_setter'
3
- require 'rack/app/middlewares/method_override'
4
4
  require 'rack/app/middlewares/params'
5
+ require 'rack/app/middlewares/hooks'
5
6
  end
@@ -0,0 +1,24 @@
1
+ require "rack/request"
2
+ require "rack/response"
3
+ class Rack::App::Middlewares::Configuration
4
+
5
+ def initialize(app, handler_class)
6
+ @handler_class = handler_class
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[Rack::App::Constants::ENV::REQUEST_HANDLER]= handler(env)
12
+ @app.call(env)
13
+ end
14
+
15
+ protected
16
+
17
+ def handler(env)
18
+ new_handler = @handler_class.new
19
+ new_handler.request = ::Rack::Request.new(env)
20
+ new_handler.response = ::Rack::Response.new
21
+ new_handler
22
+ end
23
+
24
+ end
@@ -0,0 +1,4 @@
1
+ module Rack::App::Middlewares::Hooks
2
+ require "rack/app/middlewares/hooks/before"
3
+ require "rack/app/middlewares/hooks/after"
4
+ end
@@ -0,0 +1,14 @@
1
+ class Rack::App::Middlewares::Hooks::After
2
+
3
+ def initialize(app, hook_block)
4
+ @app = app
5
+ @hook_block = hook_block
6
+ end
7
+
8
+ def call(env)
9
+ response = @app.call(env)
10
+ env[Rack::App::Constants::ENV::REQUEST_HANDLER].instance_exec(&@hook_block)
11
+ return response
12
+ end
13
+
14
+ end
@@ -0,0 +1,13 @@
1
+ class Rack::App::Middlewares::Hooks::Before
2
+
3
+ def initialize(app, hook_block)
4
+ @app = app
5
+ @hook_block = hook_block
6
+ end
7
+
8
+ def call(env)
9
+ env[Rack::App::Constants::ENV::REQUEST_HANDLER].instance_exec(&@hook_block)
10
+ @app.call(env)
11
+ end
12
+
13
+ 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/hooks'
12
13
 
13
14
  include Rack::App::SingletonMethods::HttpMethods
14
15
  include Rack::App::SingletonMethods::Inheritance
@@ -19,5 +20,6 @@ module Rack::App::SingletonMethods
19
20
  include Rack::App::SingletonMethods::RouteHandling
20
21
  include Rack::App::SingletonMethods::Settings
21
22
  include Rack::App::SingletonMethods::Extensions
23
+ include Rack::App::SingletonMethods::Hooks
22
24
 
23
25
  end
@@ -0,0 +1,15 @@
1
+ module Rack::App::SingletonMethods::Hooks
2
+
3
+ def before(&block)
4
+ @before_hooks ||= []
5
+ @before_hooks << block unless block.nil?
6
+ @before_hooks
7
+ end
8
+
9
+ def after(&block)
10
+ @after_hooks ||= []
11
+ @after_hooks << block unless block.nil?
12
+ @after_hooks
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc2
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -106,6 +106,7 @@ files:
106
106
  - lib/rack/app/cli/runner.rb
107
107
  - lib/rack/app/constants.rb
108
108
  - lib/rack/app/endpoint.rb
109
+ - lib/rack/app/endpoint/executor.rb
109
110
  - lib/rack/app/endpoint/properties.rb
110
111
  - lib/rack/app/error_handler.rb
111
112
  - lib/rack/app/extension.rb
@@ -116,8 +117,11 @@ files:
116
117
  - lib/rack/app/instance_methods/payload.rb
117
118
  - lib/rack/app/instance_methods/serve_file.rb
118
119
  - lib/rack/app/middlewares.rb
120
+ - lib/rack/app/middlewares/configuration.rb
119
121
  - lib/rack/app/middlewares/header_setter.rb
120
- - lib/rack/app/middlewares/method_override.rb
122
+ - lib/rack/app/middlewares/hooks.rb
123
+ - lib/rack/app/middlewares/hooks/after.rb
124
+ - lib/rack/app/middlewares/hooks/before.rb
121
125
  - lib/rack/app/middlewares/params.rb
122
126
  - lib/rack/app/middlewares/params/definition.rb
123
127
  - lib/rack/app/middlewares/params/definition/options.rb
@@ -135,6 +139,7 @@ files:
135
139
  - lib/rack/app/serializer.rb
136
140
  - lib/rack/app/singleton_methods.rb
137
141
  - lib/rack/app/singleton_methods/extensions.rb
142
+ - lib/rack/app/singleton_methods/hooks.rb
138
143
  - lib/rack/app/singleton_methods/http_methods.rb
139
144
  - lib/rack/app/singleton_methods/inheritance.rb
140
145
  - lib/rack/app/singleton_methods/middleware.rb
@@ -178,9 +183,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
178
183
  version: '0'
179
184
  required_rubygems_version: !ruby/object:Gem::Requirement
180
185
  requirements:
181
- - - ">"
186
+ - - ">="
182
187
  - !ruby/object:Gem::Version
183
- version: 1.3.1
188
+ version: '0'
184
189
  requirements: []
185
190
  rubyforge_project:
186
191
  rubygems_version: 2.5.1
@@ -1,55 +0,0 @@
1
- class Rack::App::Middlewares::MethodOverride
2
-
3
- ALLOWED_METHODS = %w[GET POST].freeze
4
- METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
5
- HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze
6
-
7
- def initialize(app)
8
- @app = app
9
- end
10
-
11
- def call(env)
12
- if affected_request?(env)
13
- try_override(env)
14
- end
15
-
16
- @app.call(env)
17
- end
18
-
19
- protected
20
-
21
- def try_override(env)
22
- method = method_override(env)
23
- if valid_https_method?(method)
24
- set_request_method(method, env)
25
- end
26
- end
27
-
28
- def set_request_method(method, env)
29
- env[Rack::App::Constants::ENV::METHODOVERRIDE_ORIGINAL_METHOD] = env[Rack::REQUEST_METHOD]
30
- env[Rack::REQUEST_METHOD] = method
31
- end
32
-
33
- def valid_https_method?(method)
34
- Rack::App::Constants::HTTP::METHODS.include?(method)
35
- end
36
-
37
- def affected_request?(env)
38
- ALLOWED_METHODS.include?(env[Rack::REQUEST_METHOD])
39
- end
40
-
41
- def method_override(env)
42
- req = Rack::Request.new(env)
43
- method = env[HTTP_METHOD_OVERRIDE_HEADER] ||
44
- method_override_param(req,:POST) ||
45
- method_override_param(req,:GET)
46
-
47
- method.to_s.upcase
48
- end
49
-
50
- def method_override_param(req, http_method)
51
- req.__send__(http_method)[METHOD_OVERRIDE_PARAM_KEY]
52
- rescue Rack::Utils::InvalidParameterError, Rack::Utils::ParameterTypeError
53
- end
54
-
55
- end