class Grape::Exceptions::Base

Constants

BASE_ATTRIBUTES_KEY
BASE_MESSAGES_KEY
FALLBACK_LOCALE

Attributes

headers[R]
message[R]
status[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/grape/exceptions/base.rb, line 10
def initialize(args = {})
  @status = args[:status] || nil
  @message = args[:message] || nil
  @headers = args[:headers] || nil
end

Public Instance Methods

[](index) click to toggle source
# File lib/grape/exceptions/base.rb, line 16
def [](index)
  send index
end

Protected Instance Methods

compose_message(key, attributes = {}) click to toggle source

TODO: translate attribute first if BASE_ATTRIBUTES_KEY.key respond to a string message, then short_message is returned if BASE_ATTRIBUTES_KEY.key respond to a Hash, means it may have problem , summary and resolution

# File lib/grape/exceptions/base.rb, line 25
def compose_message(key, attributes = {})
  short_message = translate_message(key, attributes)
  if short_message.is_a? Hash
    @problem = problem(key, attributes)
    @summary = summary(key, attributes)
    @resolution = resolution(key, attributes)
    [["Problem", @problem], ["Summary", @summary], ["Resolution", @resolution]].reduce("") do |message, detail_array|
      message <<  "\n#{detail_array[0]}:\n  #{detail_array[1]}" unless detail_array[1].blank?
      message
    end
  else
    short_message
  end
end
problem(key, attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 40
def problem(key, attributes)
  translate_message("#{key}.problem", attributes)
end
resolution(key, attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 48
def resolution(key, attributes)
  translate_message("#{key}.resolution", attributes)
end
summary(key, attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 44
def summary(key, attributes)
  translate_message("#{key}.summary", attributes)
end
translate(key, options = {}) click to toggle source
# File lib/grape/exceptions/base.rb, line 60
def translate(key, options = {})
  message = ::I18n.translate(key, options)
  message.present? ? message : ::I18n.translate(key, options.merge(locale: FALLBACK_LOCALE))
end
translate_attribute(key, options = {}) click to toggle source
# File lib/grape/exceptions/base.rb, line 52
def translate_attribute(key, options = {})
  translate("#{BASE_ATTRIBUTES_KEY}.#{key}", { default: key }.merge(options))
end
translate_message(key, options = {}) click to toggle source
# File lib/grape/exceptions/base.rb, line 56
def translate_message(key, options = {})
  translate("#{BASE_MESSAGES_KEY}.#{key}", { default: '' }.merge(options))
end