Class: Apiwork::ErrorCode::Definition

Inherits:
Struct
  • Object
show all
Defined in:
lib/apiwork/error_code/definition.rb

Overview

Represents a registered error code.

Error codes define HTTP status codes and behavior for API errors. Retrieved via find or find!.

Examples:

error_code = Apiwork::ErrorCode.find!(:not_found)
error_code.key # => :not_found
error_code.status # => 404
error_code.attach_path? # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attach_pathObject

Returns the value of attribute attach_path

Returns:

  • (Object)

    the current value of attach_path



28
29
30
# File 'lib/apiwork/error_code/definition.rb', line 28

def attach_path
  @attach_path
end

#keySymbol (readonly)

The key for this error code.

Returns:

  • (Symbol)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/apiwork/error_code/definition.rb', line 28

Definition = Struct.new(:key, :status, :attach_path, keyword_init: true) do
  # @api public
  # Whether this error code attaches the request path.
  #
  # @return [Boolean]
  def attach_path?
    attach_path
  end

  # @api public
  # The description for this error code.
  #
  # @param locale_key [String, nil] (nil)
  #   The I18n namespace for API-specific translations.
  # @return [String]
  #
  # @example
  #   error_code = Apiwork::ErrorCode.find!(:not_found)
  #   error_code.description # => "Not Found"
  #   error_code.description(locale_key: 'api/v1') # apiwork.apis.api/v1.error_codes.not_found.description
  def description(locale_key: nil)
    if locale_key
      api_key = :"apiwork.apis.#{locale_key}.error_codes.#{key}.description"
      result = I18n.translate(api_key, default: nil)
      return result if result
    end

    global_key = :"apiwork.error_codes.#{key}.description"
    result = I18n.translate(global_key, default: nil)
    return result if result

    key.to_s.titleize
  end
end

#statusInteger (readonly)

The status for this error code.

Returns:

  • (Integer)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/apiwork/error_code/definition.rb', line 28

Definition = Struct.new(:key, :status, :attach_path, keyword_init: true) do
  # @api public
  # Whether this error code attaches the request path.
  #
  # @return [Boolean]
  def attach_path?
    attach_path
  end

  # @api public
  # The description for this error code.
  #
  # @param locale_key [String, nil] (nil)
  #   The I18n namespace for API-specific translations.
  # @return [String]
  #
  # @example
  #   error_code = Apiwork::ErrorCode.find!(:not_found)
  #   error_code.description # => "Not Found"
  #   error_code.description(locale_key: 'api/v1') # apiwork.apis.api/v1.error_codes.not_found.description
  def description(locale_key: nil)
    if locale_key
      api_key = :"apiwork.apis.#{locale_key}.error_codes.#{key}.description"
      result = I18n.translate(api_key, default: nil)
      return result if result
    end

    global_key = :"apiwork.error_codes.#{key}.description"
    result = I18n.translate(global_key, default: nil)
    return result if result

    key.to_s.titleize
  end
end

Instance Method Details

#attach_path?Boolean

Whether this error code attaches the request path.

Returns:

  • (Boolean)


33
34
35
# File 'lib/apiwork/error_code/definition.rb', line 33

def attach_path?
  attach_path
end

#description(locale_key: nil) ⇒ String

The description for this error code.

Examples:

error_code = Apiwork::ErrorCode.find!(:not_found)
error_code.description # => "Not Found"
error_code.description(locale_key: 'api/v1') # apiwork.apis.api/v1.error_codes.not_found.description

Parameters:

  • locale_key (String, nil) (defaults to: nil)

    (nil) The I18n namespace for API-specific translations.

Returns:

  • (String)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/error_code/definition.rb', line 48

def description(locale_key: nil)
  if locale_key
    api_key = :"apiwork.apis.#{locale_key}.error_codes.#{key}.description"
    result = I18n.translate(api_key, default: nil)
    return result if result
  end

  global_key = :"apiwork.error_codes.#{key}.description"
  result = I18n.translate(global_key, default: nil)
  return result if result

  key.to_s.titleize
end