hanami-controller 2.0.1 → 2.0.2

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: 19de4a7e13f8845b766110c62f3fafef0d600cdc7e2ae12c0083f73f91fa0224
4
- data.tar.gz: fe8adf8eaff82024d6dcdfaff5e7e0a41c210e138419807af89a74babc10a6d4
3
+ metadata.gz: 477af228e710dc40efbe4387c47f5cd04d24680f5041e0218390ad22e64fe349
4
+ data.tar.gz: f70b9d82c375465cfebabcd00db19fb13243ea5888bc8096fbdeac3f01e86c9c
5
5
  SHA512:
6
- metadata.gz: 85493e28f9f323982bffd69c89e2ffeceaed9d284f21776f118e36cfc6b52ce7355e450c37d67212fc2bc135d60925a2bb7287f123892fabc81148fba076e1ec
7
- data.tar.gz: 37a1e25fd2ea272ef88f203f001830729c40102462be05681a3669c38ece703827948f08d901f2ad93092739b546a23b8714dd79452466be49619e72bf3e9fe3
6
+ metadata.gz: 202511cc4ae1c987c1990a93b6041b3dc3edf8c9391d3938499aea83fe672d43f69d8cf90420d4b222de88124c1222e2aea8fbf7d981ada5a0cf0b8c8d9a40a3
7
+ data.tar.gz: 4af98190f0ea03130a595cfa2cb222d321b77f22d3e9003daa932786b1bb2de68984d0175cb107b10af656e1e9409c38f4096959f570c2d5aaf168502481d435
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  Complete, fast and testable actions for Rack
4
4
 
5
+ ## v2.0.2 - 2023-02-01
6
+
7
+ ### Added
8
+
9
+ - [Adam Lassek] Params Pattern Matching
10
+ - [Adam Lassek, Luca Guidi] Allow to `halt` using a `Symbol`: `halt :unauthorized`
11
+ - [Adam Lassek, Luca Guidi] Introduce `Hanami::Action::Response#status=` to accept an `Integer` or a `Symbol`
12
+
13
+ ### Fixed
14
+
15
+ - [Pat Allan] Ensure action accepting the request with a custom MIME Type
16
+ - [Luca Guidi] Halting with an unknown HTTP code will raise a `Hanami::Action::UnknownHttpStatusError`
17
+ - [Luca Guidi] Fix error message for missing format (MIME Type)
18
+
5
19
  ## v2.0.1 - 2022-12-25
6
20
 
7
21
  ### Added
@@ -88,9 +88,9 @@ module Hanami
88
88
  @directives = []
89
89
  values.each do |directive_key|
90
90
  if directive_key.is_a? Hash
91
- directive_key.each { |name, value| self.<< ValueDirective.new(name, value) }
91
+ directive_key.each { |name, value| self << ValueDirective.new(name, value) }
92
92
  else
93
- self.<< NonValueDirective.new(directive_key)
93
+ self << NonValueDirective.new(directive_key)
94
94
  end
95
95
  end
96
96
  end
@@ -9,6 +9,20 @@ module Hanami
9
9
  class Error < ::StandardError
10
10
  end
11
11
 
12
+ # Unknown status HTTP Status error
13
+ #
14
+ # @since 2.0.2
15
+ #
16
+ # @see Hanami::Action::Response#status=
17
+ # @see https://guides.hanamirb.org/v2.0/actions/status-codes/
18
+ class UnknownHttpStatusError < Error
19
+ # @since 2.0.2
20
+ # @api private
21
+ def initialize(code)
22
+ super("unknown HTTP status: `#{code.inspect}'")
23
+ end
24
+ end
25
+
12
26
  # Unknown format error
13
27
  #
14
28
  # This error is raised when a action sets a format that it isn't recognized
@@ -21,14 +35,26 @@ module Hanami
21
35
  # @since 2.0.0
22
36
  # @api private
23
37
  def initialize(format)
24
- msg =
25
- if format.to_s != "" # rubocop:disable Style/NegatedIfElseCondition
26
- "Cannot find a corresponding MIME type for format #{format.inspect}. Configure one via `config.formats.add(#{format}: \"MIME_TYPE_HERE\")`." # rubocop:disable Layout/LineLength
27
- else
28
- "Cannot find a corresponding MIME type for `nil` format."
29
- end
38
+ message = <<~MSG
39
+ Cannot find a corresponding MIME type for format `#{format.inspect}'.
40
+ MSG
41
+
42
+ unless blank?(format)
43
+ message += <<~MSG
44
+
45
+ Configure one via: `config.actions.formats.add(:#{format}, "MIME_TYPE_HERE")' in `config/app.rb' to share between actions of a Hanami app.
46
+
47
+ Or make it available only in the current action: `config.formats.add(:#{format}, "MIME_TYPE_HERE")'.
48
+ MSG
49
+ end
50
+
51
+ super(message)
52
+ end
53
+
54
+ private
30
55
 
31
- super(msg)
56
+ def blank?(format)
57
+ format.to_s.match(/\A[[:space:]]*\z/)
32
58
  end
33
59
  end
34
60
 
@@ -10,8 +10,8 @@ module Hanami
10
10
  # @api private
11
11
  # @since 2.0.0
12
12
  def self.call(status, body = nil)
13
- body ||= Http::Status.message_for(status)
14
- throw :halt, [status, body]
13
+ code, message = Http::Status.for_code(status)
14
+ throw :halt, [code, body || message]
15
15
  end
16
16
  end
17
17
  end
@@ -237,7 +237,7 @@ module Hanami
237
237
  # @api private
238
238
  def accepted_mime_type?(mime_type, config)
239
239
  accepted_mime_types(config).any? { |accepted_mime_type|
240
- ::Rack::Mime.match?(accepted_mime_type, mime_type)
240
+ ::Rack::Mime.match?(mime_type, accepted_mime_type)
241
241
  }
242
242
  end
243
243
 
@@ -243,6 +243,15 @@ module Hanami
243
243
  @params
244
244
  end
245
245
  alias_method :to_hash, :to_h
246
+
247
+ # Pattern-matching support
248
+ #
249
+ # @return [::Hash]
250
+ #
251
+ # @since 2.0.2
252
+ def deconstruct_keys(*)
253
+ to_hash
254
+ end
246
255
  end
247
256
  end
248
257
  end
@@ -81,6 +81,27 @@ module Hanami
81
81
  end
82
82
  end
83
83
 
84
+ # Set the response status
85
+ #
86
+ # @param code [Integer, Symbol] the status code
87
+ #
88
+ # @since 2.0.2
89
+ # @api public
90
+ #
91
+ # @raise [Hanami::Action::UnknownHttpStatusError] if the given code
92
+ # cannot be associated to a known HTTP status
93
+ #
94
+ # @example
95
+ # response.status = :unprocessable_entity
96
+ #
97
+ # @example
98
+ # response.status = 422
99
+ #
100
+ # @see https://guides.hanamirb.org/v2.0/actions/status-codes/
101
+ def status=(code)
102
+ super(Http::Status.lookup(code))
103
+ end
104
+
84
105
  # This is NOT RELEASED with 2.0.0
85
106
  #
86
107
  # @api private
@@ -8,6 +8,6 @@ module Hanami
8
8
  #
9
9
  # @since 0.1.0
10
10
  # @api public
11
- VERSION = "2.0.1"
11
+ VERSION = "2.0.2"
12
12
  end
13
13
  end
@@ -17,31 +17,127 @@ module Hanami
17
17
  # @api private
18
18
  ALL = ::Rack::Utils::HTTP_STATUS_CODES
19
19
 
20
+ # Symbolic names for status codes
21
+ #
22
+ # @since 2.0.2
23
+ # @api private
24
+ SYMBOLS = ::Rack::Utils::SYMBOL_TO_STATUS_CODE
25
+
20
26
  # Return a status for the given code
21
27
  #
22
- # @param code [Integer] a valid HTTP code
28
+ # @param code [Integer, Symbol] a valid HTTP code
23
29
  #
24
30
  # @return [Array] a pair of code and message for an HTTP status
25
31
  #
32
+ # @raise [Hanami::Action::UnknownHttpStatusError] if the given code
33
+ # cannot be associated to a known HTTP status
34
+ #
26
35
  # @since 0.1.0
27
36
  # @api private
28
37
  #
29
- # @example
30
- # require 'hanami/http/status'
38
+ # @see https://guides.hanamirb.org/v2.0/actions/status-codes/
39
+ #
40
+ # @example Integer HTTP Status
41
+ # require "hanami/http/status"
42
+ #
43
+ # Hanami::Http::Status.for_code(401)
44
+ # # => [401, "Unauthorized"]
45
+ #
46
+ # @example Symbol HTTP Status
47
+ # require "hanami/http/status"
48
+ #
49
+ # Hanami::Http::Status.for_code(:unauthorized)
50
+ # # => [401, "Unauthorized"]
51
+ #
52
+ # @example Unknown HTTP Status
53
+ # require "hanami/http/status"
31
54
  #
32
- # Hanami::Http::Status.for_code(418) # => [418, "I'm a teapot"]
55
+ # Hanami::Http::Status.for_code(999)
56
+ # # => raise Hanami::Action::UnknownHttpStatusError
57
+ #
58
+ # Hanami::Http::Status.for_code(:foo)
59
+ # # => raise Hanami::Action::UnknownHttpStatusError
33
60
  def self.for_code(code)
34
- ALL.assoc(code)
61
+ case code
62
+ when Integer
63
+ ALL.assoc(code)
64
+ when Symbol
65
+ ALL.assoc(SYMBOLS[code])
66
+ end or raise ::Hanami::Action::UnknownHttpStatusError.new(code)
67
+ end
68
+
69
+ # Return a status code for the given code
70
+ #
71
+ # @param code [Integer,Symbol] a valid HTTP code
72
+ #
73
+ # @return [Integer] a message for the given status code
74
+ #
75
+ # @raise [Hanami::Action::UnknownHttpStatusError] if the given code
76
+ # cannot be associated to a known HTTP status
77
+ #
78
+ # @see https://guides.hanamirb.org/v2.0/actions/status-codes/
79
+ #
80
+ # @since 2.0.2
81
+ # @api private
82
+ #
83
+ # @example Integer HTTP Status
84
+ # require "hanami/http/status"
85
+ #
86
+ # Hanami::Http::Status.lookup(401)
87
+ # # => 401
88
+ #
89
+ # @example Symbol HTTP Status
90
+ # require "hanami/http/status"
91
+ #
92
+ # Hanami::Http::Status.lookup(:unauthorized)
93
+ # # => 401
94
+ #
95
+ # @example Unknown HTTP Status
96
+ # require "hanami/http/status"
97
+ #
98
+ # Hanami::Http::Status.lookup(999)
99
+ # # => raise Hanami::Action::UnknownHttpStatusError
100
+ #
101
+ # Hanami::Http::Status.lookup(:foo)
102
+ # # => raise Hanami::Action::UnknownHttpStatusError
103
+ def self.lookup(code)
104
+ for_code(code)[0]
35
105
  end
36
106
 
37
107
  # Return a message for the given status code
38
108
  #
39
- # @param code [Integer] a valid HTTP code
109
+ # @param code [Integer,Symbol] a valid HTTP code
40
110
  #
41
111
  # @return [String] a message for the given status code
42
112
  #
113
+ # @raise [Hanami::Action::UnknownHttpStatusError] if the given code
114
+ # cannot be associated to a known HTTP status
115
+ #
116
+ # @see https://guides.hanamirb.org/v2.0/actions/status-codes/
117
+ #
43
118
  # @since 0.3.2
44
119
  # @api private
120
+ #
121
+ # @example Integer HTTP Status
122
+ # require "hanami/http/status"
123
+ #
124
+ # Hanami::Http::Status.message_for(401)
125
+ # # => "Unauthorized"
126
+ #
127
+ # @example Symbol HTTP Status
128
+ # require "hanami/http/status"
129
+ #
130
+ # Hanami::Http::Status.message_for(:unauthorized)
131
+ # # => "Unauthorized"
132
+ #
133
+ # @example Unknown HTTP Status
134
+ # require "hanami/http/status"
135
+ #
136
+ # Hanami::Http::Status.message_for(999)
137
+ # # => raise Hanami::Action::UnknownHttpStatusError
138
+ #
139
+ # Hanami::Http::Status.message_for(:foo)
140
+ # # => raise Hanami::Action::UnknownHttpStatusError
45
141
  def self.message_for(code)
46
142
  for_code(code)[1]
47
143
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-25 00:00:00.000000000 Z
11
+ date: 2023-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
224
  requirements: []
225
- rubygems_version: 3.4.1
225
+ rubygems_version: 3.4.5
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: Complete, fast and testable actions for Rack and Hanami