hanami-controller 0.7.1 → 0.8.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: 19ed324ba067df9dae1f836edc8232f5ab9ec658
4
- data.tar.gz: 83d999e3d478d6b5048da0f57bc6ce9e4a3bd42e
3
+ metadata.gz: f54bdf6d87a22221ce3dae2d7c17ceb8b8097fcc
4
+ data.tar.gz: 1a7108c3fcb857a3a6f5a84055f25afb144b65af
5
5
  SHA512:
6
- metadata.gz: 4df22338de1f7d4ec7fc609327917a905db7939f28dbb2113d54e1bc2394b6877bccecd94aba0047e3a745a8098a6341e9afc7fbd36a81b1cd56d2a494685140
7
- data.tar.gz: dcb8ffe55c81813c9bd976a04cc840811b9ee0993f46ad76282d8e1f27eb58d9c069807c5f900afffacb9767d0ac900b55c2a49c0955c0c19a1a93e71488d300
6
+ metadata.gz: 41cebc4b699df51ee187a0423ae9b062299cc374f24e0511defe3ec869aab0833b04044e10aacb0b7b96ff9416b7e975e99576b1d8afd4cef535f7047d5e2f4a
7
+ data.tar.gz: d852c7b7c2a3de1f9fdeb2b6a21d034bed9d61efdb0e1187a38e16a2addba194a40094c31f578d0674b6b1dd30a194b2c7e33e2ffd24fa45a58e8349de4c0675
data/CHANGELOG.md CHANGED
@@ -1,6 +1,17 @@
1
1
  # Hanami::Controller
2
2
  Complete, fast and testable actions for Rack
3
3
 
4
+ ## v0.8.0 - 2016-11-15
5
+ ### Added
6
+ - [Marion Duprey] Allow `BaseParams#get` to read (nested) arrays
7
+
8
+ ### Fixed
9
+ - [Russell Cloak] Respect custom formats when referenced by HTTP `Accept`
10
+ - [Kyle Chong] Don't symbolize raw params
11
+
12
+ ### Changed
13
+ - [Luca Guidi] Let `BaseParams#get` to accept a list of keys (symbols) instead of string with dot notation (`params.get(:customer, :address, :city)` instead of `params.get('customer.address.city')`)
14
+
4
15
  ## v0.7.1 - 2016-10-06
5
16
  ### Added
6
17
  - [Kyle Chong] Introduced `parsed_request_body` for action
data/README.md CHANGED
@@ -22,7 +22,7 @@ Complete, fast and testable actions for Rack and [Hanami](http://hanamirb.org)
22
22
 
23
23
  ## Rubies
24
24
 
25
- __Hanami::Controller__ supports Ruby (MRI) 2.2+
25
+ __Hanami::Controller__ supports Ruby (MRI) 2.3+ and JRuby 9.1.5.0+
26
26
 
27
27
  ## Installation
28
28
 
@@ -17,10 +17,10 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = []
18
18
  spec.test_files = spec.files.grep(%r{^(test)/})
19
19
  spec.require_paths = ['lib']
20
- spec.required_ruby_version = '>= 2.2.0'
20
+ spec.required_ruby_version = '>= 2.3.0'
21
21
 
22
22
  spec.add_dependency 'rack', '~> 1.6', '>= 1.6.2'
23
- spec.add_dependency 'hanami-utils', '~> 0.8'
23
+ spec.add_dependency 'hanami-utils', '~> 0.9'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.6'
26
26
  spec.add_development_dependency 'rack-test', '~> 0.6'
@@ -15,14 +15,6 @@ module Hanami
15
15
  # @since 0.7.0
16
16
  ROUTER_PARAMS = 'router.params'.freeze
17
17
 
18
- # Separator for #get
19
- #
20
- # @since 0.7.0
21
- # @api private
22
- #
23
- # @see Hanami::Action::Params#get
24
- GET_SEPARATOR = '.'.freeze
25
-
26
18
  # @attr_reader env [Hash] the Rack env
27
19
  #
28
20
  # @since 0.7.0
@@ -45,7 +37,7 @@ module Hanami
45
37
  def initialize(env)
46
38
  @env = env
47
39
  @raw = _extract_params
48
- @params = Utils::Hash.new(@raw).symbolize!.to_h
40
+ @params = Utils::Hash.new(@raw).deep_dup.symbolize!.to_h
49
41
  freeze
50
42
  end
51
43
 
@@ -61,14 +53,12 @@ module Hanami
61
53
  end
62
54
 
63
55
  # Get an attribute value associated with the given key.
64
- # Nested attributes are reached with a dot notation.
56
+ # Nested attributes are reached by listing all the keys to get to the value.
65
57
  #
66
- # @param key [String] the key
58
+ # @param keys [Array<Symbol,Integer>] the key
67
59
  #
68
60
  # @return [Object,NilClass] return the associated value, if found
69
61
  #
70
- # @raise [NoMethodError] if key is nil
71
- #
72
62
  # @since 0.7.0
73
63
  #
74
64
  # @example
@@ -79,30 +69,30 @@ module Hanami
79
69
  # include Hanami::Action
80
70
  #
81
71
  # def call(params)
82
- # params.get('customer_name') # => "Luca"
83
- # params.get('uknown') # => nil
72
+ # params.get(:customer_name) # => "Luca"
73
+ # params.get(:uknown) # => nil
84
74
  #
85
- # params.get('address.city') # => "Rome"
86
- # params.get('address.unknown') # => nil
75
+ # params.get(:address, :city) # => "Rome"
76
+ # params.get(:address, :unknown) # => nil
87
77
  #
88
- # params.get(nil) # => nil
78
+ # params.get(:tags, 0) # => "foo"
79
+ # params.get(:tags, 1) # => "bar"
80
+ # params.get(:tags, 999) # => nil
81
+ #
82
+ # params.get(nil) # => nil
89
83
  # end
90
84
  # end
91
85
  # end
92
- def get(key)
93
- key, *keys = key.to_s.split(GET_SEPARATOR)
94
- return if key.nil?
95
-
96
- result = self[key.to_sym]
97
-
98
- Array(keys).each do |k|
99
- break if result.nil?
100
- result = result[k.to_sym]
101
- end
102
-
103
- result
86
+ def get(*keys)
87
+ @params.dig(*keys)
104
88
  end
105
89
 
90
+ # This is for compatibility with Hanami::Helpers::FormHelper::Values
91
+ #
92
+ # @api private
93
+ # @since 0.8.0
94
+ alias dig get
95
+
106
96
  # Provide a common interface with Params
107
97
  #
108
98
  # @return [TrueClass] always returns true
@@ -41,12 +41,6 @@ module Hanami
41
41
  # @api private
42
42
  DEFAULT_CHARSET = 'utf-8'.freeze
43
43
 
44
- # The default mime types list
45
- #
46
- # @since 0.6.1
47
- # @api private
48
- MIME_TYPES = ::Rack::Mime::MIME_TYPES.values.freeze
49
-
50
44
  # Override Ruby's hook for modules.
51
45
  # It includes Mime types logic
52
46
  #
@@ -180,9 +174,11 @@ module Hanami
180
174
  # The content type that will be automatically set in the response.
181
175
  #
182
176
  # It prefers, in order:
183
- # * Explicit set value (see #format=)
184
- # * Weighted value from Accept
185
- # * Default content type
177
+ # * Explicit set value (see Hanami::Action::Mime#format=)
178
+ # * Weighted value from Accept header based on all known MIME Types:
179
+ # - Custom registered MIME Types (see Hanami::Controller::Configuration#format)
180
+ # * Configured default content type (see Hanami::Controller::Configuration#default_response_format)
181
+ # * Hard-coded default content type (see Hanami::Action::Mime::DEFAULT_CONTENT_TYPE)
186
182
  #
187
183
  # To override the value, use <tt>#format=</tt>
188
184
  #
@@ -194,6 +190,8 @@ module Hanami
194
190
  # @see Hanami::Configuration#default_request_format
195
191
  # @see Hanami::Action::Mime#default_content_type
196
192
  # @see Hanami::Action::Mime#DEFAULT_CONTENT_TYPE
193
+ # @see Hanami::Controller::Configuration#format
194
+ # @see Hanami::Controller::Configuration#default_response_format
197
195
  #
198
196
  # @example
199
197
  # require 'hanami/controller'
@@ -207,7 +205,14 @@ module Hanami
207
205
  # end
208
206
  # end
209
207
  def content_type
210
- @content_type || default_response_type || accepts || default_content_type || DEFAULT_CONTENT_TYPE
208
+ return @content_type unless @content_type.nil?
209
+
210
+ if accept_header?
211
+ type = content_type_from_accept_header
212
+ return type if type
213
+ end
214
+
215
+ default_response_type || default_content_type || DEFAULT_CONTENT_TYPE
211
216
  end
212
217
 
213
218
  # Action charset setter, receives new charset value
@@ -427,20 +432,37 @@ module Hanami
427
432
  end
428
433
  end
429
434
 
430
- private
431
-
432
435
  # @since 0.1.0
433
436
  # @api private
434
437
  def accept
435
438
  @accept ||= @_env[HTTP_ACCEPT] || DEFAULT_ACCEPT
436
439
  end
437
440
 
438
- # @since 0.1.0
441
+ # Checks if there is an Accept header for the current request.
442
+ #
443
+ # @return [TrueClass,FalseClass] the result of the check
444
+ #
445
+ # @since 0.8.0
439
446
  # @api private
440
- def accepts
441
- unless accept == DEFAULT_ACCEPT
442
- best_q_match(accept, MIME_TYPES)
443
- end
447
+ def accept_header?
448
+ accept != DEFAULT_ACCEPT
449
+ end
450
+
451
+ # Look at the Accept header for the current request and see if it
452
+ # matches any of the common MIME types (see Hanami::Action::Mime#MIME_TYPES)
453
+ # or the custom registered ones (see Hanami::Controller::Configuration#format).
454
+ #
455
+ # @return [String,Nil] The matched MIME type for the given Accept header.
456
+ #
457
+ # @since 0.8.0
458
+ # @api private
459
+ #
460
+ # @see Hanami::Action::Mime#MIME_TYPES
461
+ # @see Hanami::Controller::Configuration#format
462
+ #
463
+ # @api private
464
+ def content_type_from_accept_header
465
+ best_q_match(accept, configuration.mime_types)
444
466
  end
445
467
 
446
468
  # @since 0.5.0
@@ -402,8 +402,22 @@ module Hanami
402
402
  def format(hash)
403
403
  symbol, mime_type = *Utils::Kernel.Array(hash)
404
404
 
405
- @formats.merge! Utils::Kernel.String(mime_type) =>
406
- Utils::Kernel.Symbol(symbol)
405
+ @formats[Utils::Kernel.String(mime_type)] = Utils::Kernel.Symbol(symbol)
406
+ @mime_types = nil
407
+ end
408
+
409
+ # Return the configured format's MIME types
410
+ #
411
+ # @since 0.8.0
412
+ # @api private
413
+ #
414
+ # @see Hanami::Controller::Configuration#format
415
+ # @see Hanami::Action::Mime::MIME_TYPES
416
+ def mime_types
417
+ @mime_types ||= begin
418
+ ((@formats.keys - DEFAULT_FORMATS.keys) +
419
+ ::Rack::Mime::MIME_TYPES.values).freeze
420
+ end
407
421
  end
408
422
 
409
423
  # Set a format as default fallback for all the requests without a strict
@@ -650,6 +664,7 @@ module Hanami
650
664
  @handled_exceptions = {}
651
665
  @modules = []
652
666
  @formats = DEFAULT_FORMATS.dup
667
+ @mime_types = nil
653
668
  @default_request_format = nil
654
669
  @default_response_format = nil
655
670
  @default_charset = nil
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.7.1'.freeze
6
+ VERSION = '0.8.0'.freeze
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-10-06 00:00:00.000000000 Z
13
+ date: 2016-11-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -38,14 +38,14 @@ dependencies:
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0.8'
41
+ version: '0.9'
42
42
  type: :runtime
43
43
  prerelease: false
44
44
  version_requirements: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.8'
48
+ version: '0.9'
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: bundler
51
51
  requirement: !ruby/object:Gem::Requirement
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  requirements:
147
147
  - - ">="
148
148
  - !ruby/object:Gem::Version
149
- version: 2.2.0
149
+ version: 2.3.0
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  requirements:
152
152
  - - ">="