hanami-controller 1.3.3 → 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -7
  3. data/README.md +295 -537
  4. data/hanami-controller.gemspec +3 -3
  5. data/lib/hanami/action.rb +653 -38
  6. data/lib/hanami/action/base_params.rb +2 -2
  7. data/lib/hanami/action/cache.rb +1 -139
  8. data/lib/hanami/action/cache/cache_control.rb +4 -4
  9. data/lib/hanami/action/cache/conditional_get.rb +4 -5
  10. data/lib/hanami/action/cache/directives.rb +1 -1
  11. data/lib/hanami/action/cache/expires.rb +3 -3
  12. data/lib/hanami/action/cookie_jar.rb +3 -3
  13. data/lib/hanami/action/cookies.rb +3 -62
  14. data/lib/hanami/action/flash.rb +2 -2
  15. data/lib/hanami/action/glue.rb +5 -31
  16. data/lib/hanami/action/halt.rb +12 -0
  17. data/lib/hanami/action/mime.rb +77 -491
  18. data/lib/hanami/action/params.rb +3 -3
  19. data/lib/hanami/action/rack/file.rb +1 -1
  20. data/lib/hanami/action/request.rb +30 -20
  21. data/lib/hanami/action/response.rb +174 -0
  22. data/lib/hanami/action/session.rb +8 -117
  23. data/lib/hanami/action/validatable.rb +2 -2
  24. data/lib/hanami/controller.rb +0 -210
  25. data/lib/hanami/controller/configuration.rb +51 -506
  26. data/lib/hanami/controller/version.rb +1 -1
  27. metadata +12 -21
  28. data/lib/hanami/action/callable.rb +0 -92
  29. data/lib/hanami/action/callbacks.rb +0 -214
  30. data/lib/hanami/action/configurable.rb +0 -50
  31. data/lib/hanami/action/exposable.rb +0 -126
  32. data/lib/hanami/action/exposable/guard.rb +0 -104
  33. data/lib/hanami/action/head.rb +0 -121
  34. data/lib/hanami/action/rack.rb +0 -411
  35. data/lib/hanami/action/rack/callable.rb +0 -47
  36. data/lib/hanami/action/rack/errors.rb +0 -53
  37. data/lib/hanami/action/redirect.rb +0 -59
  38. data/lib/hanami/action/throwable.rb +0 -169
@@ -1,47 +0,0 @@
1
- module Hanami
2
- module Action
3
- module Rack
4
- module Callable
5
- # Callable module for actions. With this module, actions with middlewares
6
- # will be able to work with rack builder.
7
- #
8
- # @param env [Hash] the full Rack env or the params. This value may vary,
9
- # see the examples below.
10
- #
11
- # @since 0.4.0
12
- # @api private
13
- #
14
- # @see Hanami::Action::Rack::ClassMethods#rack_builder
15
- # @see Hanami::Action::Rack::ClassMethods#use
16
- #
17
- # @example
18
- # require 'hanami/controller'
19
- #
20
- # class MyMiddleware
21
- # def initialize(app)
22
- # @app = app
23
- # end
24
- #
25
- # def call(env)
26
- # #...
27
- # end
28
- # end
29
- #
30
- # class Show
31
- # include Hanami::Action
32
- # use MyMiddleware
33
- #
34
- # def call(params)
35
- # # ...
36
- # puts params # => { id: 23 } extracted from Rack env
37
- # end
38
- # end
39
- #
40
- # Show.respond_to?(:call) # => true
41
- def call(env)
42
- rack_builder.call(env)
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Hanami
4
- module Action
5
- module Rack
6
- # This module provides method to set exceptions to Rack env:
7
- #
8
- # * `rack.errors` - IO for errors, as requested by Rack SPEC
9
- # * `rack.exception` - De-facto standard for Ruby exception tracking SaaS
10
- #
11
- # @see http://www.rubydoc.info/github/rack/rack/file/SPEC#The_Error_Stream
12
- # @see https://github.com/hanami/controller/issues/133
13
- #
14
- # @since 1.3.3
15
- # @api private
16
- module Errors
17
- # @since 1.3.3
18
- # @api private
19
- RACK_ERRORS = "rack.errors"
20
-
21
- # @since 1.3.3
22
- # @api private
23
- RACK_EXCEPTION = "rack.exception"
24
-
25
- # Set exception in Rack env
26
- #
27
- # @param env [Hash] the Rack environment
28
- # @param exception [Exception] the exception to set
29
- #
30
- # @since 1.3.3
31
- # @api private
32
- def self.set(env, exception)
33
- env[RACK_EXCEPTION] = exception
34
-
35
- return unless errors = env[RACK_ERRORS] # rubocop:disable Lint/AssignmentInCondition
36
-
37
- errors.write(_dump_exception(exception))
38
- errors.flush
39
- end
40
-
41
- # Format exception info with name and backtrace
42
- #
43
- # @param exception [Exception]
44
- #
45
- # @since 1.3.3
46
- # @api private
47
- def self._dump_exception(exception)
48
- [[exception.class, exception.message].compact.join(": "), *exception.backtrace].join("\n\t")
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,59 +0,0 @@
1
- module Hanami
2
- module Action
3
- # HTTP redirect API
4
- #
5
- # @since 0.1.0
6
- module Redirect
7
- # The HTTP header for redirects
8
- #
9
- # @since 0.2.0
10
- # @api private
11
- LOCATION = 'Location'.freeze
12
-
13
- private
14
-
15
- # Redirect to the given URL and halt the request
16
- #
17
- # @param url [String] the destination URL
18
- # @param status [Fixnum] the http code
19
- #
20
- # @since 0.1.0
21
- #
22
- # @see Hanami::Action::Throwable#halt
23
- #
24
- # @example With default status code (302)
25
- # require 'hanami/controller'
26
- #
27
- # class Create
28
- # include Hanami::Action
29
- #
30
- # def call(params)
31
- # # ...
32
- # redirect_to 'http://example.com/articles/23'
33
- # end
34
- # end
35
- #
36
- # action = Create.new
37
- # action.call({}) # => [302, {'Location' => '/articles/23'}, '']
38
- #
39
- # @example With custom status code
40
- # require 'hanami/controller'
41
- #
42
- # class Create
43
- # include Hanami::Action
44
- #
45
- # def call(params)
46
- # # ...
47
- # redirect_to 'http://example.com/articles/23', status: 301
48
- # end
49
- # end
50
- #
51
- # action = Create.new
52
- # action.call({}) # => [301, {'Location' => '/articles/23'}, '']
53
- def redirect_to(url, status: 302)
54
- headers[LOCATION] = ::String.new(url)
55
- halt(status)
56
- end
57
- end
58
- end
59
- end
@@ -1,169 +0,0 @@
1
- require 'hanami/utils/class_attribute'
2
- require 'hanami/http/status'
3
- require 'hanami/action/rack/errors'
4
-
5
- module Hanami
6
- module Action
7
- # Throw API
8
- #
9
- # @since 0.1.0
10
- #
11
- # @see Hanami::Action::Throwable::ClassMethods#handle_exception
12
- # @see Hanami::Action::Throwable#halt
13
- # @see Hanami::Action::Throwable#status
14
- module Throwable
15
- # @since 0.1.0
16
- # @api private
17
- def self.included(base)
18
- base.extend ClassMethods
19
- end
20
-
21
- # Throw API class methods
22
- #
23
- # @since 0.1.0
24
- # @api private
25
- module ClassMethods
26
- private
27
-
28
- # Handle the given exception with an HTTP status code.
29
- #
30
- # When the exception is raise during #call execution, it will be
31
- # translated into the associated HTTP status.
32
- #
33
- # This is a fine grained control, for a global configuration see
34
- # Hanami::Action.handled_exceptions
35
- #
36
- # @param exception [Hash] the exception class must be the key and the
37
- # HTTP status the value of the hash
38
- #
39
- # @since 0.1.0
40
- #
41
- # @see Hanami::Action.handled_exceptions
42
- #
43
- # @example
44
- # require 'hanami/controller'
45
- #
46
- # class Show
47
- # include Hanami::Action
48
- # handle_exception RecordNotFound => 404
49
- #
50
- # def call(params)
51
- # # ...
52
- # raise RecordNotFound.new
53
- # end
54
- # end
55
- #
56
- # Show.new.call({id: 1}) # => [404, {}, ['Not Found']]
57
- def handle_exception(exception)
58
- configuration.handle_exception(exception)
59
- end
60
- end
61
-
62
- protected
63
-
64
- # Halt the action execution with the given HTTP status code and message.
65
- #
66
- # When used, the execution of a callback or of an action is interrupted
67
- # and the control returns to the framework, that decides how to handle
68
- # the event.
69
- #
70
- # If a message is provided, it sets the response body with the message.
71
- # Otherwise, it sets the response body with the default message associated
72
- # to the code (eg 404 will set `"Not Found"`).
73
- #
74
- # @param code [Fixnum] a valid HTTP status code
75
- # @param message [String] the response body
76
- #
77
- # @since 0.2.0
78
- #
79
- # @see Hanami::Controller#handled_exceptions
80
- # @see Hanami::Action::Throwable#handle_exception
81
- # @see Hanami::Http::Status:ALL
82
- #
83
- # @example Basic usage
84
- # require 'hanami/controller'
85
- #
86
- # class Show
87
- # def call(params)
88
- # halt 404
89
- # end
90
- # end
91
- #
92
- # # => [404, {}, ["Not Found"]]
93
- #
94
- # @example Custom message
95
- # require 'hanami/controller'
96
- #
97
- # class Show
98
- # def call(params)
99
- # halt 404, "This is not the droid you're looking for."
100
- # end
101
- # end
102
- #
103
- # # => [404, {}, ["This is not the droid you're looking for."]]
104
- def halt(code, message = nil)
105
- message ||= Http::Status.message_for(code)
106
- status(code, message)
107
-
108
- throw :halt
109
- end
110
-
111
- # Sets the given code and message for the response
112
- #
113
- # @param code [Fixnum] a valid HTTP status code
114
- # @param message [String] the response body
115
- #
116
- # @since 0.1.0
117
- # @see Hanami::Http::Status:ALL
118
- def status(code, message)
119
- self.status = code
120
- self.body = message
121
- end
122
-
123
- private
124
- # @since 0.1.0
125
- # @api private
126
- def _rescue
127
- catch :halt do
128
- begin
129
- yield
130
- rescue => exception
131
- _reference_in_rack_errors(exception)
132
- _handle_exception(exception)
133
- end
134
- end
135
- end
136
-
137
- # @since 0.2.0
138
- # @api private
139
- def _reference_in_rack_errors(exception)
140
- return if configuration.handled_exception?(exception)
141
-
142
- Hanami::Action::Rack::Errors.set(@_env, exception)
143
- end
144
-
145
- # @since 0.1.0
146
- # @api private
147
- def _handle_exception(exception)
148
- raise unless configuration.handle_exceptions
149
-
150
- instance_exec(
151
- exception,
152
- &_exception_handler(exception)
153
- )
154
- end
155
-
156
- # @since 0.3.0
157
- # @api private
158
- def _exception_handler(exception)
159
- handler = configuration.exception_handler(exception)
160
-
161
- if respond_to?(handler.to_s, true)
162
- method(handler)
163
- else
164
- ->(ex) { halt handler }
165
- end
166
- end
167
- end
168
- end
169
- end