Module: RestfulError

Defined in:
lib/restful_error.rb,
lib/restful_error/status.rb,
lib/restful_error/railtie.rb,
lib/restful_error/version.rb,
lib/restful_error/wrapper.rb,
lib/restful_error/inflector.rb

Defined Under Namespace

Modules: Helper, Inflector Classes: BaseError, ExceptionsController, Wrapper

Constant Summary collapse

STATUS_CODE_TO_SYMBOL =
Rack::Utils::SYMBOL_TO_STATUS_CODE.invert
Status =
Data.define(:code, :reason_phrase, :symbol, :const_name) do
  def initialize(code:)
    reason_phrase = Rack::Utils::HTTP_STATUS_CODES[code]
    raise ArgumentError, "Invalid status code: #{code}" unless reason_phrase

    symbol = STATUS_CODE_TO_SYMBOL[code]
    const_name = Inflector.camelize(symbol.to_s)
    super(code:, reason_phrase:, symbol:, const_name:)
  end
end
VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.[](code_like) ⇒ Object



22
23
24
25
# File 'lib/restful_error.rb', line 22

def [](code_like)
  code = RestfulError.code_from(code_like)
  @cache[code] ||= build_error_class_for(code)
end

.code_from(code_or_sym_or_const_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/restful_error/status.rb', line 8

def self.code_from(code_or_sym_or_const_name)
  case code_or_sym_or_const_name
  when Integer
    code_or_sym_or_const_name
  when Symbol
    str = code_or_sym_or_const_name.to_s
    sym = /[A-Z]/.match?(str[0]) ? RestfulError::Inflector.underscore(str).to_sym : code_or_sym_or_const_name
    begin
      Rack::Utils.status_code(sym)
    rescue ArgumentError
      nil
    end
  when /\A\d{3}\z/
    code_from(code_or_sym_or_const_name.to_i)
  else
    raise ArgumentError, "Invalid argument: #{code_or_sym_or_const_name.inspect}"
  end
end

.const_missing(const_name) ⇒ Object



27
28
29
30
31
32
# File 'lib/restful_error.rb', line 27

def const_missing(const_name)
  code = RestfulError.code_from(const_name)
  return super unless code

  @cache[code] ||= build_error_class_for(code)
end

.exceptions_appObject



28
29
30
# File 'lib/restful_error/railtie.rb', line 28

def self.exceptions_app
  ExceptionsController.action(:show)
end