hanami-router 1.3.0.beta1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/hanami-router.gemspec +1 -1
- data/lib/hanami/middleware/body_parser.rb +18 -19
- data/lib/hanami/middleware/body_parser/class_interface.rb +56 -0
- data/lib/hanami/middleware/body_parser/errors.rb +23 -0
- data/lib/hanami/middleware/body_parser/json_parser.rb +2 -1
- data/lib/hanami/router/version.rb +1 -1
- metadata +8 -7
- data/lib/hanami/middleware/body_parser/parser.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e727bfc99155018c990faf3ad9621cc547413259bee1cedbe632b9c1d7695247
|
4
|
+
data.tar.gz: 768a8bf1afd0c5b97a3d2fd2011502d42bb3655f5978343f6c486ce4fbc30e6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a5934e82c544acbb3cb44e78955b8863a61341a8a1fef0208ad4b3e8d0a5303480b160e10fdbc4326a58276d21c2a9b5a79b4819047139a4a477ce97937b9fb
|
7
|
+
data.tar.gz: ea8369f2333217e336f525f0826db1212b0d42398b1b0c8fa8c70a011a1461e71dce54b59be0703e14bd55c990f1ffb8ca88491916fe1ec6f5f0016440164129
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Hanami::Router
|
2
2
|
Rack compatible HTTP router for Ruby
|
3
3
|
|
4
|
+
## v1.3.0 - 2018-10-24
|
5
|
+
### Fixed
|
6
|
+
- [Tim Riley] Skip attempting to parse unknown types in `Hanami::Middleware::BodyParser`
|
7
|
+
|
4
8
|
## v1.3.0.beta1 - 2018-08-08
|
5
9
|
### Added
|
6
10
|
- [Luca Guidi] Official support for JRuby 9.2.0.0
|
data/hanami-router.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_dependency 'rack', '~> 2.0'
|
23
23
|
spec.add_dependency 'http_router', '0.11.2'
|
24
|
-
spec.add_dependency 'hanami-utils', '~> 1.3
|
24
|
+
spec.add_dependency 'hanami-utils', '~> 1.3'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
27
27
|
spec.add_development_dependency 'rake', '~> 12'
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'hanami/middleware/body_parser/parser'
|
2
1
|
require 'hanami/utils/hash'
|
2
|
+
require_relative 'body_parser/class_interface'
|
3
3
|
|
4
4
|
module Hanami
|
5
5
|
module Middleware
|
@@ -8,7 +8,7 @@ module Hanami
|
|
8
8
|
class BodyParser
|
9
9
|
# @since 1.3.0
|
10
10
|
# @api private
|
11
|
-
CONTENT_TYPE
|
11
|
+
CONTENT_TYPE = 'CONTENT_TYPE'.freeze
|
12
12
|
|
13
13
|
# @since 1.3.0
|
14
14
|
# @api private
|
@@ -16,7 +16,7 @@ module Hanami
|
|
16
16
|
|
17
17
|
# @since 1.3.0
|
18
18
|
# @api private
|
19
|
-
RACK_INPUT
|
19
|
+
RACK_INPUT = 'rack.input'.freeze
|
20
20
|
|
21
21
|
# @since 1.3.0
|
22
22
|
# @api private
|
@@ -26,7 +26,9 @@ module Hanami
|
|
26
26
|
ROUTER_PARSED_BODY = 'router.parsed_body'.freeze
|
27
27
|
|
28
28
|
# @api private
|
29
|
-
FALLBACK_KEY
|
29
|
+
FALLBACK_KEY = '_'.freeze
|
30
|
+
|
31
|
+
extend ClassInterface
|
30
32
|
|
31
33
|
def initialize(app, parsers)
|
32
34
|
@app = app
|
@@ -37,32 +39,29 @@ module Hanami
|
|
37
39
|
body = env[RACK_INPUT].read
|
38
40
|
return @app.call(env) if body.empty?
|
39
41
|
|
40
|
-
env[RACK_INPUT].rewind
|
42
|
+
env[RACK_INPUT].rewind # somebody might try to read this stream
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
if (parser = @parsers[media_type(env)])
|
45
|
+
env[ROUTER_PARSED_BODY] = parser.parse(body)
|
46
|
+
env[ROUTER_PARAMS] = _symbolize(env[ROUTER_PARSED_BODY])
|
47
|
+
end
|
45
48
|
|
46
49
|
@app.call(env)
|
47
50
|
end
|
48
51
|
|
49
52
|
private
|
50
53
|
|
51
|
-
def build_parsers(
|
52
|
-
|
53
|
-
|
54
|
-
return result if args.empty?
|
54
|
+
def build_parsers(parser_names)
|
55
|
+
parser_names = Array(parser_names)
|
56
|
+
return {} if parser_names.empty?
|
55
57
|
|
56
|
-
|
57
|
-
parser =
|
58
|
+
parser_names.each_with_object({}) { |name, parsers|
|
59
|
+
parser = self.class.for(name)
|
58
60
|
|
59
61
|
parser.mime_types.each do |mime|
|
60
|
-
|
62
|
+
parsers[mime] = parser
|
61
63
|
end
|
62
|
-
|
63
|
-
|
64
|
-
result.default = Parser.new
|
65
|
-
result
|
64
|
+
}
|
66
65
|
end
|
67
66
|
|
68
67
|
# @api private
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'hanami/utils/class'
|
2
|
+
require 'hanami/utils/string'
|
3
|
+
require_relative 'errors'
|
4
|
+
|
5
|
+
module Hanami
|
6
|
+
module Middleware
|
7
|
+
class BodyParser
|
8
|
+
# @api private
|
9
|
+
# @since 1.3.0
|
10
|
+
module ClassInterface
|
11
|
+
# @api private
|
12
|
+
# @since 1.3.0
|
13
|
+
def for(parser)
|
14
|
+
parser =
|
15
|
+
case parser
|
16
|
+
when String, Symbol
|
17
|
+
require_parser(parser)
|
18
|
+
when Class
|
19
|
+
parser.new
|
20
|
+
else
|
21
|
+
parser
|
22
|
+
end
|
23
|
+
|
24
|
+
ensure_parser parser
|
25
|
+
|
26
|
+
parser
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# @api private
|
32
|
+
# @since 1.3.0
|
33
|
+
PARSER_METHODS = %i[mime_types parse].freeze
|
34
|
+
|
35
|
+
# @api private
|
36
|
+
# @since 1.3.0
|
37
|
+
def ensure_parser(parser)
|
38
|
+
unless PARSER_METHODS.all? { |method| parser.respond_to?(method) }
|
39
|
+
raise InvalidParserError.new(parser)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# @api private
|
44
|
+
# @since 1.3.0
|
45
|
+
def require_parser(parser)
|
46
|
+
require "hanami/middleware/body_parser/#{parser}_parser"
|
47
|
+
|
48
|
+
parser = Utils::String.classify(parser)
|
49
|
+
Utils::Class.load!("Hanami::Middleware::BodyParser::#{parser}Parser").new
|
50
|
+
rescue LoadError, NameError
|
51
|
+
raise UnknownParserError.new(parser)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'hanami/routing/error'
|
2
|
+
|
3
|
+
module Hanami
|
4
|
+
module Middleware
|
5
|
+
# @since 1.3.0
|
6
|
+
# @api private
|
7
|
+
class BodyParser
|
8
|
+
# Body parsing error
|
9
|
+
# This is raised when parser fails to parse the body
|
10
|
+
#
|
11
|
+
# @since 1.3.0
|
12
|
+
class BodyParsingError < Hanami::Routing::Parsing::BodyParsingError
|
13
|
+
end
|
14
|
+
|
15
|
+
# @since 1.3.0
|
16
|
+
class UnknownParserError < Hanami::Routing::Parsing::UnknownParserError
|
17
|
+
end
|
18
|
+
|
19
|
+
class InvalidParserError < Hanami::Routing::Error
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.0
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.3
|
47
|
+
version: '1.3'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.3
|
54
|
+
version: '1.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -121,8 +121,9 @@ files:
|
|
121
121
|
- hanami-router.gemspec
|
122
122
|
- lib/hanami-router.rb
|
123
123
|
- lib/hanami/middleware/body_parser.rb
|
124
|
+
- lib/hanami/middleware/body_parser/class_interface.rb
|
125
|
+
- lib/hanami/middleware/body_parser/errors.rb
|
124
126
|
- lib/hanami/middleware/body_parser/json_parser.rb
|
125
|
-
- lib/hanami/middleware/body_parser/parser.rb
|
126
127
|
- lib/hanami/router.rb
|
127
128
|
- lib/hanami/router/version.rb
|
128
129
|
- lib/hanami/routing/endpoint.rb
|
@@ -159,9 +160,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
160
|
version: 2.3.0
|
160
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
162
|
requirements:
|
162
|
-
- - "
|
163
|
+
- - ">="
|
163
164
|
- !ruby/object:Gem::Version
|
164
|
-
version:
|
165
|
+
version: '0'
|
165
166
|
requirements: []
|
166
167
|
rubyforge_project:
|
167
168
|
rubygems_version: 2.7.7
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'hanami/utils/class'
|
2
|
-
require 'hanami/utils/string'
|
3
|
-
require 'hanami/routing/parsing/parser'
|
4
|
-
|
5
|
-
module Hanami
|
6
|
-
module Middleware
|
7
|
-
class BodyParser
|
8
|
-
# Body parsing error
|
9
|
-
# This is raised when parser fails to parse the body
|
10
|
-
#
|
11
|
-
# @since 1.3.0
|
12
|
-
class BodyParsingError < Hanami::Routing::Parsing::BodyParsingError
|
13
|
-
end
|
14
|
-
|
15
|
-
# @since 1.3.0
|
16
|
-
class UnknownParserError < Hanami::Routing::Parsing::UnknownParserError
|
17
|
-
end
|
18
|
-
|
19
|
-
# @since 1.3.0
|
20
|
-
class Parser
|
21
|
-
# @since 1.3.0
|
22
|
-
# @api private
|
23
|
-
def self.for(parser)
|
24
|
-
case parser
|
25
|
-
when String, Symbol
|
26
|
-
require_parser(parser)
|
27
|
-
else
|
28
|
-
raise UnknownParserError.new(parser) unless parser?(parser)
|
29
|
-
parser.new
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# @since 1.3.0
|
34
|
-
def mime_types
|
35
|
-
raise NotImplementedError
|
36
|
-
end
|
37
|
-
|
38
|
-
# @since 1.3.0
|
39
|
-
def parse(body)
|
40
|
-
body
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
# @since 1.3.0
|
45
|
-
# @api private
|
46
|
-
def self.require_parser(parser)
|
47
|
-
require "hanami/middleware/body_parser/#{ parser }_parser"
|
48
|
-
|
49
|
-
parser = Utils::String.classify(parser)
|
50
|
-
Utils::Class.load!("Hanami::Middleware::BodyParser::#{ parser }Parser").new
|
51
|
-
rescue LoadError, NameError
|
52
|
-
raise UnknownParserError.new(parser)
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.parser?(parser)
|
56
|
-
parser.is_a?(Class) && parser < Parser
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|