hanami-controller 2.0.0.beta4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,22 +2,51 @@
2
2
 
3
3
  module Hanami
4
4
  class Action
5
+ # Base class for all Action errors.
6
+ #
7
+ # @api public
5
8
  # @since 2.0.0
6
9
  class Error < ::StandardError
7
10
  end
8
11
 
9
- # Missing session error
12
+ # Unknown format error
10
13
  #
11
- # This error is raised when `session` or `flash` is accessed/set on request/response objects
12
- # in actions which do not include `Hanami::Action::Session`.
14
+ # This error is raised when a action sets a format that it isn't recognized
15
+ # both by `Hanami::Action::Configuration` and the list of Rack mime types
13
16
  #
14
17
  # @since 2.0.0
15
18
  #
19
+ # @see Hanami::Action::Mime#format=
20
+ class UnknownFormatError < Error
21
+ # @since 2.0.0
22
+ # @api private
23
+ 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
30
+
31
+ super(msg)
32
+ end
33
+ end
34
+
35
+ # Error raised when session is accessed but not enabled.
36
+ #
37
+ # This error is raised when `session` or `flash` is accessed/set on request/response objects
38
+ # in actions which do not include `Hanami::Action::Session`.
39
+ #
16
40
  # @see Hanami::Action::Session
17
41
  # @see Hanami::Action::Request#session
18
42
  # @see Hanami::Action::Response#session
19
43
  # @see Hanami::Action::Response#flash
44
+ #
45
+ # @api public
46
+ # @since 2.0.0
20
47
  class MissingSessionError < Error
48
+ # @api private
49
+ # @since 2.0.0
21
50
  def initialize(session_method)
22
51
  super(<<~TEXT)
23
52
  Sessions are not enabled. To use `#{session_method}`:
@@ -37,5 +66,11 @@ module Hanami
37
66
  TEXT
38
67
  end
39
68
  end
69
+
70
+ # Invalid CSRF Token
71
+ #
72
+ # @since 0.4.0
73
+ class InvalidCSRFTokenError < Error
74
+ end
40
75
  end
41
76
  end
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # The Hanami::Action::Flash implementation is derived from Roda's FlashHash, also released under the
4
+ # MIT Licence:
5
+ #
6
+ # Copyright (c) 2014-2020 Jeremy Evans
7
+ # Copyright (c) 2010-2014 Michel Martens, Damian Janowski and Cyril David
8
+ # Copyright (c) 2008-2009 Christian Neukirchen
9
+
3
10
  module Hanami
4
11
  class Action
5
- # A container to transport data with the HTTP session, with a lifespan of
6
- # just one HTTP request or redirect.
7
- #
8
- # Behaves like a hash, returning entries for the current request, except for
9
- # {#[]=}, which updates the hash for the next request.
12
+ # A container to transport data with the HTTP session, with a lifespan of just one HTTP request
13
+ # or redirect.
10
14
  #
11
- # This implementation is derived from Roda's FlashHash, also released under
12
- # the MIT Licence:
13
- #
14
- # Copyright (c) 2014-2020 Jeremy Evans
15
- # Copyright (c) 2010-2014 Michel Martens, Damian Janowski and Cyril David
16
- # Copyright (c) 2008-2009 Christian Neukirchen
15
+ # Behaves like a hash, returning entries for the current request, except for {#[]=}, which
16
+ # updates the hash for the next request.
17
17
  #
18
18
  # @since 0.3.0
19
19
  # @api public
@@ -30,9 +30,9 @@ module Hanami
30
30
  # @api public
31
31
  attr_reader :next
32
32
 
33
- # Initializes a new flash instance
33
+ # Returns a new flash object.
34
34
  #
35
- # @param hash [Hash, nil] the flash hash for the current request. nil will become an empty hash.
35
+ # @param hash [Hash, nil] the flash hash for the current request; `nil` will become an empty hash.
36
36
  #
37
37
  # @since 0.3.0
38
38
  # @api public
@@ -41,7 +41,9 @@ module Hanami
41
41
  @next = {}
42
42
  end
43
43
 
44
- # @return [Hash] The flash hash for the current request
44
+ # Returns the flash hash for the current request.
45
+ #
46
+ # @return [Hash] the flash hash for the current request
45
47
  #
46
48
  # @since 2.0.0
47
49
  # @api public
@@ -49,7 +51,7 @@ module Hanami
49
51
  @flash
50
52
  end
51
53
 
52
- # Returns the value for the given key in the current hash
54
+ # Returns the value for the given key in the current hash.
53
55
  #
54
56
  # @param key [Object] the key
55
57
  #
@@ -61,7 +63,7 @@ module Hanami
61
63
  @flash[key]
62
64
  end
63
65
 
64
- # Updates the next hash with the given key and value
66
+ # Updates the next hash with the given key and value.
65
67
  #
66
68
  # @param key [Object] the key
67
69
  # @param value [Object] the value
@@ -72,9 +74,12 @@ module Hanami
72
74
  @next[key] = value
73
75
  end
74
76
 
75
- # Calls the given block once for each element in the current hash
77
+ # Calls the given block once for each element in the current hash.
76
78
  #
77
- # @param block [Proc]
79
+ # @yieldparam element [Array<(Object, Object)>] array containing the key and value from the
80
+ # hash
81
+ #
82
+ # @return [now]
78
83
  #
79
84
  # @since 1.2.0
80
85
  # @api public
@@ -82,10 +87,12 @@ module Hanami
82
87
  @flash.each(&block)
83
88
  end
84
89
 
85
- # Returns a new array with the results of running block once for every
86
- # element in the current hash
90
+ # Returns an array of objects returned by the block, called once for each element in the
91
+ # current hash.
92
+ #
93
+ # @yieldparam element [Array<(Object, Object)>] array containing the key and value from the
94
+ # hash
87
95
  #
88
- # @param block [Proc]
89
96
  # @return [Array]
90
97
  #
91
98
  # @since 1.2.0
@@ -114,7 +121,7 @@ module Hanami
114
121
  @flash.key?(key)
115
122
  end
116
123
 
117
- # Removes entries from the next hash
124
+ # Removes entries from the next hash.
118
125
  #
119
126
  # @overload discard(key)
120
127
  # Removes the given key from the next hash
@@ -4,9 +4,11 @@ require "hanami/http/status"
4
4
 
5
5
  module Hanami
6
6
  class Action
7
+ # @api private
8
+ # @since 2.0.0
7
9
  module Halt
8
- # @since 2.0.0
9
10
  # @api private
11
+ # @since 2.0.0
10
12
  def self.call(status, body = nil)
11
13
  body ||= Http::Status.message_for(status)
12
14
  throw :halt, [status, body]
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ class Action
5
+ module Mime
6
+ # @since 1.0.1
7
+ # @api private
8
+ class RequestMimeWeight
9
+ # @since 2.0.0
10
+ # @api private
11
+ MIME_SEPARATOR = "/"
12
+ private_constant :MIME_SEPARATOR
13
+
14
+ # @since 2.0.0
15
+ # @api private
16
+ MIME_WILDCARD = "*"
17
+ private_constant :MIME_WILDCARD
18
+
19
+ include Comparable
20
+
21
+ # @since 1.0.1
22
+ # @api private
23
+ attr_reader :quality
24
+
25
+ # @since 1.0.1
26
+ # @api private
27
+ attr_reader :index
28
+
29
+ # @since 1.0.1
30
+ # @api private
31
+ attr_reader :mime
32
+
33
+ # @since 1.0.1
34
+ # @api private
35
+ attr_reader :format
36
+
37
+ # @since 1.0.1
38
+ # @api private
39
+ attr_reader :priority
40
+
41
+ # @since 1.0.1
42
+ # @api private
43
+ def initialize(mime, quality, index, format = mime)
44
+ @quality, @index, @format = quality, index, format
45
+ calculate_priority(mime)
46
+ end
47
+
48
+ # @since 1.0.1
49
+ # @api private
50
+ def <=>(other)
51
+ return priority <=> other.priority unless priority == other.priority
52
+
53
+ other.index <=> index
54
+ end
55
+
56
+ private
57
+
58
+ # @since 1.0.1
59
+ # @api private
60
+ def calculate_priority(mime)
61
+ @priority ||= (mime.split(MIME_SEPARATOR, 2).count(MIME_WILDCARD) * -10) + quality
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end