rack-app 6.0.0 → 6.1.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: 115ef984aee400271ed64f69eee96b2bc3896cb9
4
- data.tar.gz: dcb1b5ee1fd8af7b107b6dc50d9089fd9410657e
3
+ metadata.gz: 0b3fcb5171d9537595e5217484244a8033f25672
4
+ data.tar.gz: ae2b66351cfb4deeb2eb1f6fe184d90dba7e5014
5
5
  SHA512:
6
- metadata.gz: a921f4cc74415fd8aef272fd97bdd3fd53e2066e42266ddddc98a9271cee6df6612d695fa6129055d7d216df533f596099cea13fa59a8c311380f3e31dfa68de
7
- data.tar.gz: fe97b75e3b27041a03b34daa0650c40fc4719161d3d1d91e355000f6454762d50b431f1384eddad588f387c6991f69e59f4d49203df0a9428b048381ec0abe97
6
+ metadata.gz: 56d6b686fdfd3961c10aa20fa9d39deb4ff025491ac0aefe99a01e73857f0dc402dcc4123e4335462276ecc8aacd076a90a1e252044dc61234390878893ca2a4
7
+ data.tar.gz: a3b712d9292e047d9367a4bc14085bc33beb6ce144412e4aa00559633d147d6d511a56c6c7dcc32140b32af986d6d0f1389436278c711df743ff7b8b60e45dd1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 6.0.0
1
+ 6.1.0
@@ -45,14 +45,17 @@ module Rack::App::Constants
45
45
  REQUEST_HANDLER = 'rack-app.handler'.freeze
46
46
 
47
47
  PARAMS_GETTER = 'rack-app.params.getter'
48
- PARSED_PARAMS = 'rack-app.params.parsed'.freeze
48
+ PARAMS = 'rack-app.params.object'.freeze
49
+
50
+ MERGED_PARAMS = 'rack-app.params.merged'.freeze
49
51
  VALIDATED_PARAMS = 'rack-app.params.validated'.freeze
52
+ QUERY_STRING_PARAMS = 'rack-app.params.query_string'.freeze
53
+ PATH_SEGMENTS_PARAMS = 'rack-app.params.path_segments'.freeze
50
54
 
51
55
  PAYLOAD_PARSER = 'rack-app.payload.parser'.freeze
52
56
  PAYLOAD_GETTER = 'rack-app.payload.getter'.freeze
53
57
  PARSED_PAYLOAD = 'rack-app.payload.parsed'.freeze
54
58
 
55
- PATH_PARAMS = 'rack-app.path_params'.freeze
56
59
  ORIGINAL_PATH_INFO = 'rack-app.original_path_info'.freeze
57
60
  FORMATTED_PATH_INFO = 'rack-app.formatted_path_info'.freeze
58
61
  SPLITTED_PATH_INFO = 'rack-app.splitted_path_info'.freeze
@@ -2,7 +2,7 @@ class Rack::App::Endpoint
2
2
 
3
3
  require "forwardable"
4
4
  extend Forwardable
5
- def_delegators :@config, :request_methods, :request_path, :description
5
+ def_delegators :@config, :request_method, :request_path, :description
6
6
 
7
7
  require "rack/app/endpoint/config"
8
8
  require "rack/app/endpoint/builder"
@@ -5,7 +5,7 @@ class Rack::App::Endpoint::Config
5
5
  error_handler
6
6
  endpoint_specific_middlewares
7
7
  request_path
8
- request_methods
8
+ request_method
9
9
  defined_request_path
10
10
  @raw
11
11
  end
@@ -55,16 +55,8 @@ class Rack::App::Endpoint::Config
55
55
  @raw[:endpoint_specific_middlewares] ||= []
56
56
  end
57
57
 
58
-
59
- def request_methods
60
- case @raw[:request_methods] || raise('missing config: request_methods')
61
- when Rack::App::Constants::HTTP::METHOD::ANY
62
- Rack::App::Constants::HTTP::METHODS
63
- when ::Array
64
- @raw[:request_methods].map(&:to_sym)
65
- else
66
- [@raw[:request_methods]].flatten.map(&:to_sym)
67
- end
58
+ def request_method
59
+ @raw[:request_method] || raise('missing config: request_methods')
68
60
  end
69
61
 
70
62
  def request_path
@@ -1,6 +1,7 @@
1
1
  module Rack::App::InstanceMethods
2
2
 
3
3
  require 'rack/app/instance_methods/core'
4
+ require 'rack/app/instance_methods/params'
4
5
  require 'rack/app/instance_methods/http_status'
5
6
  require 'rack/app/instance_methods/redirect_to'
6
7
  require 'rack/app/instance_methods/path_to'
@@ -9,6 +10,7 @@ module Rack::App::InstanceMethods
9
10
  require 'rack/app/instance_methods/streaming'
10
11
 
11
12
  include Rack::App::InstanceMethods::Core
13
+ include Rack::App::InstanceMethods::Params
12
14
  include Rack::App::InstanceMethods::HTTPStatus
13
15
  include Rack::App::InstanceMethods::RedirectTo
14
16
  include Rack::App::InstanceMethods::PathTo
@@ -2,14 +2,6 @@ module Rack::App::InstanceMethods::Core
2
2
 
3
3
  attr_writer :request, :response
4
4
 
5
- def params
6
- request.env[::Rack::App::Constants::ENV::PARSED_PARAMS] ||= Rack::App::Params.new(request.env).to_hash
7
- end
8
-
9
- def validated_params
10
- request.env[::Rack::App::Constants::ENV::VALIDATED_PARAMS]
11
- end
12
-
13
5
  def request
14
6
  @request || raise("request object is not set for #{self.class}")
15
7
  end
@@ -18,15 +10,15 @@ module Rack::App::InstanceMethods::Core
18
10
  @response || raise("response object is not set for #{self.class}")
19
11
  end
20
12
 
21
- def finish!(rack_response=response)
22
- throw(:rack_response, rack_response)
13
+ def respond_with(value = response)
14
+ case value
15
+ when Rack::Response
16
+ throw(:rack_response, value)
17
+ else
18
+ throw(:response_body, value)
19
+ end
23
20
  end
24
21
 
25
- alias finish_response finish!
26
- Rack::App::Utils.deprecate(self, :finish_response, :finish!, 2016,9)
27
-
28
- def respond_with(respond_body)
29
- throw(:response_body, respond_body)
30
- end
22
+ alias finish! respond_with
31
23
 
32
24
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module Rack::App::InstanceMethods::Params
3
+
4
+ E = ::Rack::App::Constants::ENV
5
+
6
+ def params
7
+ request.env[E::PARAMS].to_hash
8
+ end
9
+
10
+ def validated_params
11
+ request.env[E::PARAMS].validated_params
12
+ end
13
+
14
+ def path_segments_params
15
+ request.env[E::PARAMS].path_segments_params
16
+ end
17
+
18
+ def query_string_params
19
+ request.env[E::PARAMS].query_string_params
20
+ end
21
+
22
+ end
@@ -5,15 +5,9 @@ class Rack::App::Middlewares::Params::Setter
5
5
  end
6
6
 
7
7
  def call(env)
8
- env[::Rack::App::Constants::ENV::PARSED_PARAMS] ||= params_hash(env)
8
+ Rack::App::Params.new(env).to_hash
9
9
 
10
10
  @app.call(env)
11
11
  end
12
12
 
13
- protected
14
-
15
- def params_hash(env)
16
- Rack::App::Params.new(env).to_hash
17
- end
18
-
19
13
  end
@@ -17,14 +17,14 @@ class Rack::App::Middlewares::SetPathParams
17
17
 
18
18
  def populate_path_params(env)
19
19
  @params_lookup_hash.each do |index, key|
20
- env[E::PATH_PARAMS][key] = env[E::SPLITTED_PATH_INFO][index]
20
+ env[E::PATH_SEGMENTS_PARAMS][key] = env[E::SPLITTED_PATH_INFO][index]
21
21
  end
22
22
  end
23
23
 
24
24
  def correct_last_value_from_extensions(env)
25
25
  last_index = env[E::SPLITTED_PATH_INFO].length - 1
26
26
  return if @params_lookup_hash[last_index].nil?
27
- extless(env[E::PATH_PARAMS][@params_lookup_hash[last_index]])
27
+ extless(env[E::PATH_SEGMENTS_PARAMS][@params_lookup_hash[last_index]])
28
28
  end
29
29
 
30
30
  def extless(value)
@@ -1,12 +1,23 @@
1
1
  require 'cgi'
2
+ require 'uri'
2
3
  class Rack::App::Params
3
4
 
5
+ E = ::Rack::App::Constants::ENV
6
+
4
7
  def to_hash
5
- if @env[::Rack::App::Constants::ENV::PARSED_PARAMS]
6
- @env[::Rack::App::Constants::ENV::PARSED_PARAMS]
7
- else
8
- query_params.merge(request_path_params)
9
- end
8
+ @env[E::MERGED_PARAMS] ||= query_string_params.merge(path_segments_params)
9
+ end
10
+
11
+ def query_string_params
12
+ @env[E::QUERY_STRING_PARAMS] ||= generate_query_params
13
+ end
14
+
15
+ def path_segments_params
16
+ @env[E::PATH_SEGMENTS_PARAMS]
17
+ end
18
+
19
+ def validated_params
20
+ @env[E::VALIDATED_PARAMS]
10
21
  end
11
22
 
12
23
  protected
@@ -15,20 +26,17 @@ class Rack::App::Params
15
26
  @env = env
16
27
  end
17
28
 
18
- def query_params
29
+ def generate_query_params
19
30
  raw_cgi_params.reduce({}) do |params_collection, (k, v)|
20
- stripped_key = k.sub(/\[\]$/, '')
21
-
22
- if single_paramter_value?(v) && !k.end_with?("[]")
23
- params_collection[stripped_key]= v[0]
24
- else
25
- params_collection[stripped_key]= v
26
- end
27
-
31
+ params_collection[k.sub(/\[\]$/, '')] = formatted_value(k, v)
28
32
  params_collection
29
33
  end
30
34
  end
31
35
 
36
+ def formatted_value(key, value)
37
+ single_paramter_value?(value) && !key.end_with?("[]") ? value[0] : value
38
+ end
39
+
32
40
  def single_paramter_value?(v)
33
41
  v.is_a?(Array) and v.length === 1
34
42
  end
@@ -37,8 +45,4 @@ class Rack::App::Params
37
45
  CGI.parse(@env[::Rack::QUERY_STRING].to_s)
38
46
  end
39
47
 
40
- def request_path_params
41
- @env[::Rack::App::Constants::ENV::PATH_PARAMS]
42
- end
43
-
44
48
  end
@@ -49,13 +49,13 @@ class Rack::App::Router
49
49
 
50
50
  endpoints = self.endpoints
51
51
 
52
- wd0 = endpoints.map { |endpoint| endpoint.request_methods.first.to_s.length }.max
52
+ wd0 = endpoints.map { |endpoint| endpoint.request_method.to_s.length }.max
53
53
  wd1 = endpoints.map { |endpoint| endpoint.request_path.to_s.length }.max
54
54
  wd2 = endpoints.map { |endpoint| endpoint.description.to_s.length }.max
55
55
 
56
- return endpoints.sort_by { |endpoint| [endpoint.request_methods.first, endpoint.request_path] }.map do |endpoint|
56
+ return endpoints.sort_by { |endpoint| [endpoint.request_method, endpoint.request_path] }.map do |endpoint|
57
57
  [
58
- endpoint.request_methods.first.to_s.ljust(wd0),
58
+ endpoint.request_method.to_s.ljust(wd0),
59
59
  endpoint.request_path.to_s.ljust(wd1),
60
60
  endpoint.description.to_s.ljust(wd2)
61
61
  ].join(' ')
@@ -27,7 +27,8 @@ class Rack::App::Router::Tree
27
27
  E = Rack::App::Constants::ENV
28
28
 
29
29
  def configure_request(env)
30
- env[E::PATH_PARAMS] ||= {}
30
+ env[E::PATH_SEGMENTS_PARAMS] ||= {}
31
+ env[E::PARAMS] ||= Rack::App::Params.new(env)
31
32
  fpi = format_path_info(env).freeze
32
33
  env[E::FORMATTED_PATH_INFO] = fpi
33
34
  spi = split_path_info(fpi).freeze
@@ -3,7 +3,7 @@ class Rack::App::Router::Tree::Vein < ::Hash
3
3
 
4
4
  def set(env)
5
5
  app = create_app(env)
6
- env.endpoint.request_methods.each do |request_method|
6
+ request_methods(env).each do |request_method|
7
7
  self[request_method.to_s.upcase] = app
8
8
  end
9
9
  end
@@ -31,4 +31,14 @@ class Rack::App::Router::Tree::Vein < ::Hash
31
31
  builder.to_app
32
32
  end
33
33
 
34
+ def request_methods(env)
35
+ request_method = env.endpoint.request_method
36
+ case request_method || raise('missing config: request_methods')
37
+ when Rack::App::Constants::HTTP::METHOD::ANY
38
+ Rack::App::Constants::HTTP::METHODS
39
+ else
40
+ [request_method].flatten.map(&:to_sym)
41
+ end
42
+ end
43
+
34
44
  end
@@ -28,7 +28,7 @@ module Rack::App::SingletonMethods::RouteHandling
28
28
  :callable => callable,
29
29
  :payload => payload,
30
30
  :error_handler => error,
31
- :request_methods => [request_method],
31
+ :request_method => request_method,
32
32
  :route => route_registration_properties.dup,
33
33
  :endpoint_specific_middlewares => next_endpoint_middlewares.dup,
34
34
  :request_path => ::Rack::App::Utils.join(@namespaces, request_path)
@@ -41,9 +41,6 @@ module Rack::App::SingletonMethods::RouteHandling
41
41
 
42
42
  end
43
43
 
44
- def add_app_to_route(app, request_path)
45
- end
46
-
47
44
  def namespace(request_path_namespace)
48
45
  return unless block_given?
49
46
  @namespaces ||= []
@@ -16,7 +16,7 @@ module Rack::App::Test
16
16
  properties = args.select { |e| e.is_a?(Hash) }.reduce({}, &:merge!)
17
17
  url = args.detect { |e| e.is_a?(String) } || properties.delete(:url)
18
18
  mock_request = Rack::MockRequest.new(rack_app)
19
- request_env = Rack::App::Test::Utils.env_by(properties)
19
+ request_env = Rack::App::Test::Utils.env_by(url, properties)
20
20
  return @last_response = mock_request.request(request_method.to_s.upcase, url, request_env)
21
21
  end
22
22
 
@@ -10,18 +10,27 @@ module Rack::App::Test::Utils
10
10
  properties
11
11
  end
12
12
 
13
- def env_by(properties)
13
+ def env_by(uri, properties)
14
14
 
15
15
  properties = format_properties(properties)
16
16
  env = properties[:headers].reduce({}) { |m, (k, v)| m.merge("HTTP_#{k.to_s.tr('-', '_').upcase}" => v.to_s) }
17
17
  payload = properties.delete(:payload)
18
18
  env["rack.input"]= ::Rack::Lint::InputWrapper.new(string_io_for(payload))
19
- env[::Rack::QUERY_STRING]= Rack::App::Utils.encode_www_form(properties[:params].to_a)
19
+ env[::Rack::QUERY_STRING]= query_string_by(uri, properties[:params])
20
20
  env.merge!(properties[:env] || {})
21
21
 
22
22
  env
23
23
  end
24
24
 
25
+ def query_string_by(uri, params={})
26
+ uri_based = URI.parse(uri).query.to_s
27
+ prop_based = Rack::App::Utils.encode_www_form(params.to_a)
28
+
29
+ parts = [uri_based, prop_based]
30
+ parts.delete("")
31
+ parts.join("&")
32
+ end
33
+
25
34
  def string_io_for(payload)
26
35
  case payload
27
36
 
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: 6.0.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-02 00:00:00.000000000 Z
11
+ date: 2017-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,6 +118,7 @@ files:
118
118
  - lib/rack/app/instance_methods.rb
119
119
  - lib/rack/app/instance_methods/core.rb
120
120
  - lib/rack/app/instance_methods/http_status.rb
121
+ - lib/rack/app/instance_methods/params.rb
121
122
  - lib/rack/app/instance_methods/path_to.rb
122
123
  - lib/rack/app/instance_methods/payload.rb
123
124
  - lib/rack/app/instance_methods/redirect_to.rb