module Daredevil

Constants

VERSION

Public Class Methods

config() click to toggle source

Global settings for Daredevil

# File lib/daredevil/configuration.rb, line 9
def self.config
  @config
end
configure() { |config ||= configuration| ... } click to toggle source
# File lib/daredevil/configuration.rb, line 4
def self.configure(&block)
  yield @config ||= Daredevil::Configuration.new
end
included(base) click to toggle source
# File lib/daredevil.rb, line 7
def self.included(base)
  base.rescue_from ActiveRecord::RecordNotFound, with: :record_not_found!
  base.rescue_from ActionController::ParameterMissing, with: :parameter_missing!
  redefine_authorization(base)
end
redefine_authorization(base) click to toggle source
# File lib/daredevil.rb, line 13
def self.redefine_authorization(base)
  return unless base.instance_methods.include?(:authenticate_user_from_token!)

  base.class_eval do
    alias_method(:_authenticate_from_token!, :authenticate_user_from_token!)

    define_method :authenticate_user_from_token! do
      result = catch(:warden) { _authenticate_from_token! }

      return unless result
      respond_with_error(:unauthorized)
    end
  end
end

Private Instance Methods

parameter_missing!(error) click to toggle source
# File lib/daredevil.rb, line 57
def parameter_missing!(error)
  respond_with_error(:parameter_missing, error)
end
record_not_found!(error) click to toggle source
# File lib/daredevil.rb, line 53
def record_not_found!(error)
  respond_with_error(:not_found, error)
end
respond_with(resource, options = {}) click to toggle source
# File lib/daredevil.rb, line 42
def respond_with(resource, options = {})
  options = {
    namespace: self.class.parent,
    status: :ok,
    params: params,
    controller: self
  }.merge(options)

  Responder.new(resource, options).respond!
end
respond_with_error(error_type, resource = nil) click to toggle source
# File lib/daredevil.rb, line 30
def respond_with_error(error_type, resource = nil)
  case error_type
  when :unauthorized
    Responder.new(nil, controller: self, status: :forbidden).unauthorized
  when :not_found
    Responder.new(resource, controller: self, status: :not_found).not_found
  when :parameter_missing
    Responder.new(resource, controller: self, status: :unprocessable_entity).
      parameter_missing
  end
end