Exception: APPI::Exception

Inherits:
Exception
  • Object
show all
Defined in:
lib/appi/exception.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, details = {}, validations = {}) ⇒ Exception

Returns a new instance of Exception.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/appi/exception.rb', line 11

def initialize(code, details={}, validations={})
  @details     = details
  @code        = code
  @validations = validations

  @status = I18n.t "appi_exceptions.#{code}.status"
  @title  = I18n.t "appi_exceptions.#{code}.title", details
  @detail = I18n.t "appi_exceptions.#{code}.detail", details

  @message = @detail
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/appi/exception.rb', line 4

def code
  @code
end

#detailObject

Returns the value of attribute detail.



9
10
11
# File 'lib/appi/exception.rb', line 9

def detail
  @detail
end

#detailsObject

Returns the value of attribute details.



3
4
5
# File 'lib/appi/exception.rb', line 3

def details
  @details
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/appi/exception.rb', line 7

def status
  @status
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/appi/exception.rb', line 8

def title
  @title
end

#validationsObject

Returns the value of attribute validations.



5
6
7
# File 'lib/appi/exception.rb', line 5

def validations
  @validations
end

Instance Method Details

#as_jsonObject



45
46
47
48
49
# File 'lib/appi/exception.rb', line 45

def as_json
  {
    errors: validation_array.prepend(base_error) 
  }
end

#base_errorObject



36
37
38
39
40
41
42
43
# File 'lib/appi/exception.rb', line 36

def base_error
  {
    title: title,
    detail: detail,
    code: code,
    status: status
  }
end

#validation_arrayObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/appi/exception.rb', line 23

def validation_array
  array = []
  @validations.to_hash.each_key do |key|
    array << {
      title: @validations[key].uniq.join(', '),
      source: {
        pointer: "data/attributes/#{key}"
      }
    }
  end
  array
end