dryer_routes 0.5.2 → 0.6.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: 3a8e8605f3d6633a41935ff3cc4c41bae7e52d88f64afa6ed95cf8b75e0a4182
4
- data.tar.gz: 9c907db2f384c236a9227352697d4578c46f3fec6f5d64dbc17ccdf9aa9a1d5f
3
+ metadata.gz: 1e1bd1a21ffca48f41fc0006c91c7fb74d5ba63dccaa832243b974fdd83986fa
4
+ data.tar.gz: 92142801197d90929bdcfa7ac221512558f5a6a42dfbb98c1a422108def8c5b0
5
5
  SHA512:
6
- metadata.gz: afaf9df048ec4dbafb3dce59b78b37853e345b99d94f9bce63123dae394c25a558d0311d54fd35417ae407d8c7edd3052ef18bda498e80cac0c4550f47f5d9d5
7
- data.tar.gz: fe0122aaa65830092dae8a49e311608fab3cff6714722c8ff25c4437e495432a7784e41e41a740f44834493c7ae8ddb24923efa8065cc1505cdce5485d8e9177
6
+ metadata.gz: d91227c0ff46538dbbc76c25d3e266b1d35ba37aa8c6245d04df5d9be8ccf55dd56eafb5041e88dbc488121a9bb834b5bea25a954517eea82874546460ab9aa4
7
+ data.tar.gz: 8aa179a4780e0fb5ac9540b24cb2fad708456fb7fb0079b296434ccd51de4e258886bc19a314686378290f57025e671c4ac38f8a7339a266eaf4cbdcc0d2e7b7
data/dryer_routes.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'dryer_routes'
3
- spec.version = "0.5.2"
3
+ spec.version = "0.6.0"
4
4
  spec.authors = ['John Bernier']
5
5
  spec.email = ['john.b.bernier@gmail.com']
6
6
  spec.summary = 'Typed routing for rails leveraging dry-validation contracts'
@@ -11,12 +11,11 @@ module Dryer
11
11
  resource[:actions].map do |action, config|
12
12
  Route.new(
13
13
  controller: resource[:controller],
14
- url: Dryer::Routes::UrlBuilder.call(
15
- config[:url] || resource[:url]
16
- ),
14
+ url: config[:url] || resource[:url],
17
15
  method: config[:method],
18
16
  controller_action: action,
19
17
  request_contract: config[:request_contract],
18
+ url_parameters_contract: config[:url_parameters_contract],
20
19
  response_contracts: config[:response_contracts]
21
20
  )
22
21
  end
@@ -1,16 +1,25 @@
1
+ require 'dryer_services'
2
+
1
3
  module Dryer
2
4
  module Routes
3
- class HashObject
4
- def initialize(hash)
5
+ class HashObject < Dryer::Services::SimpleService
6
+ def initialize(hash)
5
7
  hash.each do |k,v|
6
8
  key = k.is_a?(Numeric) ? "_#{k}" : k
7
9
 
8
- self.instance_variable_set(
9
- "@#{key}", v.is_a?(Hash) ? HashObject.new(v) : v
10
- )
11
- self.class.send(
12
- :define_method, key, proc{self.instance_variable_get("@#{key}")}
13
- )
10
+ if v.is_a?(Proc)
11
+ self.send(:define_singleton_method, key, proc do |*args, **kwargs, &block|
12
+ v.arity != 0 ? v.call(*args, **kwargs, &block) : v.call
13
+ end)
14
+ else
15
+ self.instance_variable_set(
16
+ "@#{key}", v.is_a?(Hash) ? HashObject.new(v) : v
17
+ )
18
+
19
+ self.send(
20
+ :define_singleton_method, key, proc{self.instance_variable_get("@#{key}")}
21
+ )
22
+ end
14
23
  end
15
24
  end
16
25
  end
@@ -14,7 +14,7 @@ module Dryer
14
14
  @routes = resources.map do |r|
15
15
  BuildFromResource.call(r)
16
16
  end.flatten
17
- add_accessors_for_resources(resources)
17
+ ResourceAccessors.call(object: self, resources: resources)
18
18
  @routes
19
19
  end
20
20
 
@@ -35,6 +35,19 @@ module Dryer
35
35
  end
36
36
  end
37
37
 
38
+ def validate_url_parameters(request)
39
+ route_for(
40
+ controller: request.controller_class,
41
+ method: request.request_method_symbol
42
+ ).then do |route|
43
+ if route && route.url_parameters_contract
44
+ route.url_parameters_contract.new.call(request.params).errors
45
+ else
46
+ []
47
+ end
48
+ end
49
+ end
50
+
38
51
  def validate_response(controller:, method:, status:, body:)
39
52
  route_for(
40
53
  controller: controller.class,
@@ -55,7 +68,7 @@ module Dryer
55
68
  end.first
56
69
  end
57
70
 
58
- def get_validated_values(request)
71
+ def validated_request_body(request)
59
72
  route_for(
60
73
  controller: request.controller_class,
61
74
  method: request.request_method_symbol
@@ -67,34 +80,22 @@ module Dryer
67
80
  end
68
81
  end
69
82
 
70
- attr_reader :routes, :resources
71
-
72
- private
73
- attr_writer :routes, :resources
74
-
75
- def add_accessors_for_resources(resources)
76
- denormalize_resources(resources).inject(self) do |obj, (key, value)|
77
- obj.define_singleton_method(key) { HashObject.new(value) }
78
- obj
83
+ def validated_url_parameters(request)
84
+ route_for(
85
+ controller: request.controller_class,
86
+ method: request.request_method_symbol
87
+ ).then do |route|
88
+ ExtractValidatedKeys.call(
89
+ payload: request.params,
90
+ contract: route.url_parameters_contract
91
+ )
79
92
  end
80
93
  end
81
94
 
82
- def denormalize_resources(resources)
83
- resources.inject({}) do | h, resource |
84
- h[
85
- resource[:controller].controller_name.to_sym
86
- ] = denormalize_resource(resource)
87
- h
88
- end
89
- end
95
+ attr_reader :routes, :resources
90
96
 
91
- def denormalize_resource(resource)
92
- resource[:actions].each do |key, value|
93
- resource[:actions][key][:url] =
94
- resource[:actions][key][:url] || resource[:url]
95
- end
96
- resource.merge(resource[:actions])
97
- end
97
+ private
98
+ attr_writer :routes, :resources
98
99
 
99
100
  def validate_resources!(resources)
100
101
  errors = resources.map do |r|
@@ -0,0 +1,41 @@
1
+ require 'dryer_services'
2
+
3
+ module Dryer
4
+ module Routes
5
+ class ResourceAccessors < Dryer::Services::SimpleService
6
+
7
+ def initialize(object:, resources:)
8
+ @object = object
9
+ @resources = resources
10
+ end
11
+
12
+ def call
13
+ denormalize_resources(resources).inject(object) do |obj, (key, value)|
14
+ obj.define_singleton_method(key) { HashObject.new(value) }
15
+ obj
16
+ end
17
+ end
18
+
19
+ private
20
+ attr_reader :object, :resources
21
+
22
+ def denormalize_resources(resources)
23
+ resources.inject({}) do | h, resource|
24
+ h[
25
+ resource[:controller].controller_name.to_sym
26
+ ] = denormalize_resource(resource)
27
+ h
28
+ end
29
+ end
30
+
31
+ def denormalize_resource(resource)
32
+ resource[:actions].each do |key, value|
33
+ resource[:actions][key][:url] = UrlBuilder.call(
34
+ resource[:actions][key][:url] || resource[:url]
35
+ )
36
+ end
37
+ resource.merge(resource[:actions])
38
+ end
39
+ end
40
+ end
41
+ end
@@ -13,18 +13,25 @@ module Dryer
13
13
  params do
14
14
  required(:method).filled(:symbol)
15
15
  optional(:request_contract)
16
+ optional(:url_parameters_contract)
16
17
  optional(:response_contracts).hash()
17
18
  end
18
19
 
19
20
  rule(:request_contract) do
20
- if value && !value.ancestors.include?(Dry::Validation::Contract)
21
+ if value && !(value <= Dry::Validation::Contract)
22
+ key.failure('must be a dry-validation contract')
23
+ end
24
+ end
25
+
26
+ rule(:url_parameters_contract) do
27
+ if value && !(value <= Dry::Validation::Contract)
21
28
  key.failure('must be a dry-validation contract')
22
29
  end
23
30
  end
24
31
 
25
32
  rule(:response_contracts) do
26
33
  values[:response_contracts].each do |key, value|
27
- if !value.ancestors.include?(Dry::Validation::Contract)
34
+ if !(value <= Dry::Validation::Contract)
28
35
  key(:response_contracts).failure(
29
36
  'must be a dry-validation contract'
30
37
  )
@@ -29,6 +29,10 @@ module Dryer
29
29
  route_config[:request_contract]
30
30
  end
31
31
 
32
+ def url_parameters_contract
33
+ route_config[:url_parameters_contract]
34
+ end
35
+
32
36
  def response_contract_for(status)
33
37
  route_config[:response_contracts][status]
34
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dryer_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier
@@ -111,6 +111,7 @@ files:
111
111
  - lib/dryer/routes/extract_validated_keys.rb
112
112
  - lib/dryer/routes/hash_object.rb
113
113
  - lib/dryer/routes/registry.rb
114
+ - lib/dryer/routes/resource_accessors.rb
114
115
  - lib/dryer/routes/resource_schema.rb
115
116
  - lib/dryer/routes/route.rb
116
117
  - lib/dryer/routes/url_builder.rb