Class: RageController::API
- Inherits:
-
Object
- Object
- RageController::API
- Defined in:
- lib/rage/controller/api.rb
Class Method Summary collapse
-
.before_action(action_name, only: nil, except: nil) ⇒ Object
Register a new
before_action
hook. -
.rescue_from(*klasses, with: nil, &block) ⇒ Object
Register a global exception handler.
-
.skip_before_action(action_name, only: nil, except: nil) ⇒ Object
Prevent a
before_action
hook from running.
Instance Method Summary collapse
-
#head(status) ⇒ Object
Send a response with no body.
-
#render(json: nil, plain: nil, status: nil) ⇒ Object
Send a response to the client.
Class Method Details
.before_action(action_name, only: nil, except: nil) ⇒ Object
Register a new before_action
hook. Calls with the same action_name
will overwrite the previous ones.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rage/controller/api.rb', line 119 def before_action(action_name, only: nil, except: nil) if @__before_actions && @__before_actions.frozen? @__before_actions = @__before_actions.dup end action = { name: action_name, only: only && Array(only), except: except && Array(except) } if @__before_actions.nil? @__before_actions = [action] elsif i = @__before_actions.find_index { |a| a[:name] == action_name } @__before_actions[i] = action else @__before_actions << action end end |
.rescue_from(*klasses, with: nil, &block) ⇒ Object
Note:
Unlike Rails, the handler must always take an argument. Use _
if you don't care about the actual exception.
Register a global exception handler. Handlers are inherited and matched from bottom to top.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rage/controller/api.rb', line 89 def rescue_from(*klasses, with: nil, &block) unless with if block_given? name = ("a".."z").to_a.sample(15).join with = define_method("__#{name}", &block) else raise "No handler provided. Pass the `with` keyword argument or provide a block." end end if @__rescue_handlers.nil? @__rescue_handlers = [] elsif @__rescue_handlers.frozen? @__rescue_handlers = @__rescue_handlers.dup end @__rescue_handlers.unshift([klasses, with]) end |
.skip_before_action(action_name, only: nil, except: nil) ⇒ Object
Prevent a before_action
hook from running.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rage/controller/api.rb', line 141 def skip_before_action(action_name, only: nil, except: nil) i = @__before_actions&.find_index { |a| a[:name] == action_name } raise "The following action was specified to be skipped but cannot be found: #{self}##{action_name}" unless i @__before_actions = @__before_actions.dup if @__before_actions.frozen? if only.nil? && except.nil? @__before_actions.delete_at(i) return end action = @__before_actions[i].dup if only action[:except] ? action[:except] |= Array(only) : action[:except] = Array(only) end if except action[:only] = Array(except) end @__before_actions[i] = action end |
Instance Method Details
#head(status) ⇒ Object
Send a response with no body.
218 219 220 221 222 223 224 225 226 |
# File 'lib/rage/controller/api.rb', line 218 def head(status) @__rendered = true @__status = if status.is_a?(Symbol) ::Rack::Utils::SYMBOL_TO_STATUS_CODE[status] else status end end |
#render(json: nil, plain: nil, status: nil) ⇒ Object
Note:
render
doesn't terminate execution of the action, so if you want to exit an action after rendering, you need to do something like render(...) and return
.
Send a response to the client.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/rage/controller/api.rb', line 187 def render(json: nil, plain: nil, status: nil) raise "Render was called multiple times in this action" if @__rendered @__rendered = true if json || plain @__body << if json json.is_a?(String) ? json : json.to_json else __set_header("content-type", "text/plain; charset=utf-8") plain end @__status = 200 end if status @__status = if status.is_a?(Symbol) ::Rack::Utils::SYMBOL_TO_STATUS_CODE[status] else status end end end |