Class: RageController::API

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/controller/api.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

before_action :find_photo, only: :show

def find_photo
  Photo.first
end

Parameters:

  • action_name (String)

    the name of the callback to add

  • only (Symbol, Array<Symbol>) (defaults to: nil)

    restrict the callback to run only for specific actions

  • except (Symbol, Array<Symbol>) (defaults to: nil)

    restrict the callback to run for all actions except specified



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.

Examples:

rescue_from User::NotAuthorized, with: :deny_access

def deny_access(exception)
  head :forbidden
end
rescue_from User::NotAuthorized do |_|
  head :forbidden
end

Parameters:

  • klasses (Class, Array<Class>)

    exception classes to watch on

  • with (Symbol) (defaults to: nil)

    the name of a handler method. The method must take one argument, which is the raised exception. Alternatively, you can pass a block, which must also take one argument.



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.

Examples:

skip_before_action :find_photo, only: :create

Parameters:

  • action_name (String)

    the name of the callback to skip

  • only (Symbol, Array<Symbol>) (defaults to: nil)

    restrict the callback to be skipped only for specific actions

  • except (Symbol, Array<Symbol>) (defaults to: nil)

    restrict the callback to be skipped for all actions except specified



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.

Examples:

head :unauthorized
head 429

Parameters:

  • status (Integer, Symbol)

    set a response status



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.

Examples:

render json: { hello: "world" }
render status: :ok
render plain: "hello world", status: 201

Parameters:

  • json (String, Object) (defaults to: nil)

    send a json response to the client; objects like arrays will be serialized automatically

  • plain (String) (defaults to: nil)

    send a text response to the client

  • status (Integer, Symbol) (defaults to: nil)

    set a response status



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