request_handler 2.0.0 → 2.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
  SHA256:
3
- metadata.gz: c6f03be51da1bccf44ba08a55724d6cd68c8dd43817e5b1e74f427b9ae1164f7
4
- data.tar.gz: 942131e22d64ee316f7e7c690c9c1a6af0d5c589459d2bc77786e86a940fe342
3
+ metadata.gz: 7fb382a3a9c578b69efee4f3f24371d3f1539cb9695057130ec60f04c7be858e
4
+ data.tar.gz: b5b4e80acb25c29c8e2cf1f139b9ec902750a0f46b0ac31311d9d144272f24f0
5
5
  SHA512:
6
- metadata.gz: f50ec83b0dc218d07d9f9703f929696f761dfd812e2a1dd5230d2e901f20ac1c4141d8674a332d08ecf3297dd7e3a3af4514e7330e37d341db92efc1dc660714
7
- data.tar.gz: 7e0b0d58b1f0fd697432c9e3f0fb4447b983510bc643acde7713db1c3bc8de23a4209bd45e80ce9f255458ba0c83c5dbd9f22eca672280c98363e85a6cd3c429
6
+ metadata.gz: 9ff65ee31333500b115f8b15e3f60561740e647f9c6171d51516aa7c8db0f369b85c7af44fe61b47ebab77888d74d7fec8e774c04cbd3833c2204e7565af28cb
7
+ data.tar.gz: b30401b3c56285040c493e5c4c8e80165ed6e1226d6d058906cf93b1ddf4a6226164325a6931ec73e81a42e8463d2a288689a41ee94e71f0af71b671154ec4ec
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [2.1.0] - 2020-04-01
8
+ ### Added
9
+ - Headers validation
10
+
7
11
  ## [2.0.0] - 2019-11-19
8
12
  ### Changed
9
13
  - BREAKING: Required configuration of validation engine
@@ -51,7 +51,7 @@ module RequestHandler
51
51
  end
52
52
 
53
53
  def headers
54
- @headers ||= HeaderParser.new(env: request.env).run
54
+ @headers ||= parse_headers
55
55
  end
56
56
 
57
57
  def body_params
@@ -105,6 +105,21 @@ module RequestHandler
105
105
  result.empty? ? defaults : result
106
106
  end
107
107
 
108
+ def parse_headers
109
+ HeaderParser.new(header_parser_params).run
110
+ end
111
+
112
+ def header_parser_params
113
+ params = { env: request.env }
114
+
115
+ return params if config.nil?
116
+
117
+ params.merge(
118
+ schema: config.lookup('headers.schema'),
119
+ schema_options: execute_options(config.lookup('headers.options'))
120
+ )
121
+ end
122
+
108
123
  def parse_body_params
109
124
  BodyParser.new(
110
125
  request: request,
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'request_handler/builder/base'
4
+
5
+ module RequestHandler
6
+ module Builder
7
+ class HeadersBuilder < Base
8
+ Headers = Struct.new(:schema, :options)
9
+
10
+ def create_klass_struct
11
+ @result = Headers.new
12
+ end
13
+
14
+ def schema(value)
15
+ @result.schema = value
16
+ end
17
+
18
+ def options(value)
19
+ @result.options = value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'request_handler/builder/base'
4
+ require 'request_handler/builder/headers_builder'
4
5
  require 'request_handler/builder/page_builder'
5
6
  require 'request_handler/builder/include_options_builder'
6
7
  require 'request_handler/builder/sort_options_builder'
@@ -14,7 +15,7 @@ module RequestHandler
14
15
  module Builder
15
16
  class OptionsBuilder < Base
16
17
  Options = Struct.new(:page, :include_options, :sort_options, :filter, :query, :body,
17
- :multipart, :fieldsets)
18
+ :multipart, :fieldsets, :headers)
18
19
 
19
20
  def create_klass_struct
20
21
  @result = Options.new
@@ -52,6 +53,10 @@ module RequestHandler
52
53
  @result.fieldsets = build_fieldsets(&block)
53
54
  end
54
55
 
56
+ def headers(&block)
57
+ @result.headers = build_headers(&block)
58
+ end
59
+
55
60
  def build_page(&block)
56
61
  Docile.dsl_eval(PageBuilder.new, &block).build
57
62
  end
@@ -83,6 +88,10 @@ module RequestHandler
83
88
  def build_fieldsets(&block)
84
89
  Docile.dsl_eval(FieldsetsBuilder.new, &block).build
85
90
  end
91
+
92
+ def build_headers(&block)
93
+ Docile.dsl_eval(HeadersBuilder.new, &block).build
94
+ end
86
95
  end
87
96
  end
88
97
  end
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'request_handler/schema_parser'
3
4
  require 'request_handler/error'
5
+
4
6
  module RequestHandler
5
- class HeaderParser
6
- def initialize(env:)
7
+ class HeaderParser < SchemaParser
8
+ def initialize(env:, schema: nil, schema_options: {})
9
+ super(schema: schema, schema_options: schema_options) unless schema.nil?
10
+
7
11
  raise MissingArgumentError, env: 'is missing' if env.nil?
8
12
  @headers = Helper.deep_transform_keys_in_object(env.select { |k, _v| k.start_with?('HTTP_') }) do |k|
9
13
  k[5..-1].downcase.to_sym
@@ -11,11 +15,34 @@ module RequestHandler
11
15
  end
12
16
 
13
17
  def run
14
- headers
18
+ return headers if schema.nil?
19
+
20
+ validate_headers!
15
21
  end
16
22
 
17
23
  private
18
24
 
25
+ def validate_headers!
26
+ validate_schema(headers)
27
+ rescue SchemaValidationError => e
28
+ raise ExternalArgumentError, external_argument_error_params(e)
29
+ end
30
+
31
+ def external_argument_error_params(error)
32
+ error.errors.map do |schema_error|
33
+ header = schema_error[:source][:pointer]
34
+ {
35
+ status: '400',
36
+ code: "#{headers[header.to_sym] ? 'INVALID' : 'MISSING'}_HEADER",
37
+ detail: "#{format_header_name(header)} #{schema_error[:detail]}"
38
+ }
39
+ end
40
+ end
41
+
42
+ def format_header_name(name)
43
+ name.split('_').map(&:capitalize).join('-')
44
+ end
45
+
19
46
  attr_reader :headers
20
47
  end
21
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequestHandler
4
- VERSION = '2.0.0'.freeze
4
+ VERSION = '2.1.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Eger
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-11-19 00:00:00.000000000 Z
12
+ date: 2020-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: docile
@@ -281,6 +281,7 @@ files:
281
281
  - lib/request_handler/builder/fieldsets_builder.rb
282
282
  - lib/request_handler/builder/fieldsets_resource_builder.rb
283
283
  - lib/request_handler/builder/filter_builder.rb
284
+ - lib/request_handler/builder/headers_builder.rb
284
285
  - lib/request_handler/builder/include_options_builder.rb
285
286
  - lib/request_handler/builder/multipart_builder.rb
286
287
  - lib/request_handler/builder/multipart_resource_builder.rb