Class: MediaTypes::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/media_types/problem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Problem

Returns a new instance of Problem.



7
8
9
10
11
12
# File 'lib/media_types/problem.rb', line 7

def initialize(error)
  self.error = error
  self.translations = {}
  self.custom_attributes = {}
  self.response_status_code = 400
end

Instance Attribute Details

#custom_attributesObject

Returns the value of attribute custom_attributes.



14
15
16
# File 'lib/media_types/problem.rb', line 14

def custom_attributes
  @custom_attributes
end

#custom_typeObject

Returns the value of attribute custom_type.



14
15
16
# File 'lib/media_types/problem.rb', line 14

def custom_type
  @custom_type
end

#errorObject

Returns the value of attribute error.



14
15
16
# File 'lib/media_types/problem.rb', line 14

def error
  @error
end

#response_status_codeObject

Returns the value of attribute response_status_code.



14
15
16
# File 'lib/media_types/problem.rb', line 14

def response_status_code
  @response_status_code
end

#translationsObject

Returns the value of attribute translations.



14
15
16
# File 'lib/media_types/problem.rb', line 14

def translations
  @translations
end

Instance Method Details

#attribute(name, value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/media_types/problem.rb', line 37

def attribute(name, value)
  str_name = name.to_s

  unless str_name =~ /^[a-zA-Z][a-zA-Z0-9_]{2,}$/
    raise "Unable to add an attribute with name '#{str_name}'. Name should start with a letter, consist of the " \
      'letters A-Z, a-z, 0-9 or _ and be at least 3 characters long.'
  end

  custom_attributes[str_name] = value
end

#instanceObject



54
55
56
57
58
59
60
61
# File 'lib/media_types/problem.rb', line 54

def instance
  return nil unless custom_type.nil?

  inner = error.cause
  return nil if inner.nil?

  "https://docs.delftsolutions.nl/wiki/Error/#{ERB::Util.url_encode(inner.class.name)}"
end

#languagesObject



63
64
65
# File 'lib/media_types/problem.rb', line 63

def languages
  translations.keys
end

#override_detail(detail, lang:) ⇒ Object



31
32
33
34
35
# File 'lib/media_types/problem.rb', line 31

def override_detail(detail, lang:)
  raise 'Unable to override detail message without having a title in the same language.' unless translations[lang]

  translations[lang][:detail] = detail
end

#status_code(code) ⇒ Object



48
49
50
51
52
# File 'lib/media_types/problem.rb', line 48

def status_code(code)
  code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code] if code.is_a? Symbol

  self.response_status_code = code
end

#title(title, lang:) ⇒ Object



26
27
28
29
# File 'lib/media_types/problem.rb', line 26

def title(title, lang:)
  translations[lang] ||= {}
  translations[lang][:title] = title
end

#typeObject



16
17
18
19
20
# File 'lib/media_types/problem.rb', line 16

def type
  return custom_type unless custom_type.nil?

  "https://docs.delftsolutions.nl/wiki/Error/#{ERB::Util.url_encode(error.class.name)}"
end

#url(href) ⇒ Object



22
23
24
# File 'lib/media_types/problem.rb', line 22

def url(href)
  self.custom_type = href
end