grape 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +7 -6
  4. data/UPGRADING.md +19 -0
  5. data/grape.gemspec +5 -5
  6. data/lib/grape/api/instance.rb +22 -58
  7. data/lib/grape/api.rb +2 -11
  8. data/lib/grape/dsl/desc.rb +27 -24
  9. data/lib/grape/dsl/inside_route.rb +12 -23
  10. data/lib/grape/dsl/parameters.rb +2 -2
  11. data/lib/grape/dsl/routing.rb +5 -12
  12. data/lib/grape/endpoint.rb +76 -79
  13. data/lib/grape/error_formatter/base.rb +51 -21
  14. data/lib/grape/error_formatter/json.rb +7 -24
  15. data/lib/grape/error_formatter/jsonapi.rb +7 -0
  16. data/lib/grape/error_formatter/serializable_hash.rb +7 -0
  17. data/lib/grape/error_formatter/txt.rb +13 -20
  18. data/lib/grape/error_formatter/xml.rb +3 -13
  19. data/lib/grape/error_formatter.rb +4 -12
  20. data/lib/grape/exceptions/base.rb +18 -30
  21. data/lib/grape/exceptions/validation.rb +5 -4
  22. data/lib/grape/exceptions/validation_errors.rb +2 -2
  23. data/lib/grape/formatter/base.rb +16 -0
  24. data/lib/grape/formatter/json.rb +4 -6
  25. data/lib/grape/formatter/serializable_hash.rb +1 -1
  26. data/lib/grape/formatter/txt.rb +3 -5
  27. data/lib/grape/formatter/xml.rb +4 -6
  28. data/lib/grape/formatter.rb +4 -12
  29. data/lib/grape/http/headers.rb +1 -0
  30. data/lib/grape/middleware/error.rb +2 -0
  31. data/lib/grape/middleware/formatter.rb +1 -1
  32. data/lib/grape/middleware/versioner/accept_version_header.rb +3 -3
  33. data/lib/grape/middleware/versioner/base.rb +82 -0
  34. data/lib/grape/middleware/versioner/header.rb +3 -9
  35. data/lib/grape/middleware/versioner/param.rb +0 -2
  36. data/lib/grape/middleware/versioner/path.rb +0 -2
  37. data/lib/grape/middleware/versioner.rb +5 -3
  38. data/lib/grape/namespace.rb +1 -1
  39. data/lib/grape/parser/base.rb +16 -0
  40. data/lib/grape/parser/json.rb +6 -8
  41. data/lib/grape/parser/jsonapi.rb +7 -0
  42. data/lib/grape/parser/xml.rb +6 -8
  43. data/lib/grape/parser.rb +5 -7
  44. data/lib/grape/path.rb +39 -56
  45. data/lib/grape/request.rb +2 -2
  46. data/lib/grape/router/base_route.rb +2 -2
  47. data/lib/grape/router/greedy_route.rb +2 -2
  48. data/lib/grape/router/pattern.rb +23 -18
  49. data/lib/grape/router/route.rb +13 -5
  50. data/lib/grape/router.rb +5 -5
  51. data/lib/grape/util/registry.rb +27 -0
  52. data/lib/grape/validations/contract_scope.rb +2 -39
  53. data/lib/grape/validations/params_scope.rb +7 -11
  54. data/lib/grape/validations/types/dry_type_coercer.rb +10 -6
  55. data/lib/grape/validations/validator_factory.rb +2 -2
  56. data/lib/grape/validations/validators/allow_blank_validator.rb +1 -1
  57. data/lib/grape/validations/validators/base.rb +5 -9
  58. data/lib/grape/validations/validators/coerce_validator.rb +1 -1
  59. data/lib/grape/validations/validators/contract_scope_validator.rb +41 -0
  60. data/lib/grape/validations/validators/default_validator.rb +1 -1
  61. data/lib/grape/validations/validators/except_values_validator.rb +1 -1
  62. data/lib/grape/validations/validators/length_validator.rb +1 -1
  63. data/lib/grape/validations/validators/regexp_validator.rb +1 -1
  64. data/lib/grape/validations/validators/values_validator.rb +15 -57
  65. data/lib/grape/validations.rb +8 -17
  66. data/lib/grape/version.rb +1 -1
  67. data/lib/grape.rb +1 -1
  68. metadata +14 -11
  69. data/lib/grape/middleware/versioner_helpers.rb +0 -75
  70. data/lib/grape/validations/types/build_coercer.rb +0 -92
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Grape
4
- module Validations
5
- module Types
6
- module BuildCoercer
7
- # Chooses the best coercer for the given type. For example, if the type
8
- # is Integer, it will return a coercer which will be able to coerce a value
9
- # to the integer.
10
- #
11
- # There are a few very special coercers which might be returned.
12
- #
13
- # +Grape::Types::MultipleTypeCoercer+ is a coercer which is returned when
14
- # the given type implies values in an array with different types.
15
- # For example, +[Integer, String]+ allows integer and string values in
16
- # an array.
17
- #
18
- # +Grape::Types::CustomTypeCoercer+ is a coercer which is returned when
19
- # a method is specified by a user with +coerce_with+ option or the user
20
- # specifies a custom type which implements requirments of
21
- # +Grape::Types::CustomTypeCoercer+.
22
- #
23
- # +Grape::Types::CustomTypeCollectionCoercer+ is a very similar to the
24
- # previous one, but it expects an array or set of values having a custom
25
- # type implemented by the user.
26
- #
27
- # There is also a group of custom types implemented by Grape, check
28
- # +Grape::Validations::Types::SPECIAL+ to get the full list.
29
- #
30
- # @param type [Class] the type to which input strings
31
- # should be coerced
32
- # @param method [Class,#call] the coercion method to use
33
- # @return [Object] object to be used
34
- # for coercion and type validation
35
- def self.build_coercer(type, method: nil, strict: false)
36
- cache_instance(type, method, strict) do
37
- create_coercer_instance(type, method, strict)
38
- end
39
- end
40
-
41
- def self.create_coercer_instance(type, method, strict)
42
- # Maps a custom type provided by Grape, it doesn't map types wrapped by collections!!!
43
- type = Types.map_special(type)
44
-
45
- # Use a special coercer for multiply-typed parameters.
46
- if Types.multiple?(type)
47
- MultipleTypeCoercer.new(type, method)
48
-
49
- # Use a special coercer for custom types and coercion methods.
50
- elsif method || Types.custom?(type)
51
- CustomTypeCoercer.new(type, method)
52
-
53
- # Special coercer for collections of types that implement a parse method.
54
- # CustomTypeCoercer (above) already handles such types when an explicit coercion
55
- # method is supplied.
56
- elsif Types.collection_of_custom?(type)
57
- Types::CustomTypeCollectionCoercer.new(
58
- Types.map_special(type.first), type.is_a?(Set)
59
- )
60
- else
61
- DryTypeCoercer.coercer_instance_for(type, strict)
62
- end
63
- end
64
-
65
- def self.cache_instance(type, method, strict, &_block)
66
- key = cache_key(type, method, strict)
67
-
68
- return @__cache[key] if @__cache.key?(key)
69
-
70
- instance = yield
71
-
72
- @__cache_write_lock.synchronize do
73
- @__cache[key] = instance
74
- end
75
-
76
- instance
77
- end
78
-
79
- def self.cache_key(type, method, strict)
80
- [type, method, strict].each_with_object(+'_') do |val, memo|
81
- next if val.nil?
82
-
83
- memo << '_' << val.to_s
84
- end
85
- end
86
-
87
- instance_variable_set(:@__cache, {})
88
- instance_variable_set(:@__cache_write_lock, Mutex.new)
89
- end
90
- end
91
- end
92
- end