Class: Levelup::Responses::Error

Inherits:
Templates::DataParcel show all
Defined in:
lib/levelup/responses/error.rb

Overview

Encapsulates a response to an unsuccessful request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Templates::DataParcel

excluded?, instance_variables_excluded_from_hash

Constructor Details

#initialize(headers, errors, status_code) ⇒ Error

Builds the error from the raw JSON response and the specified status code.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/levelup/responses/error.rb', line 16

def initialize(headers, errors, status_code)
  @headers = headers

  if errors.is_a?(Array) || !errors
    @errors = (errors || []).map do |error|
      OpenStruct.new(error['error'])
    end
  else
    @errors = [OpenStruct.new(message: 'The API returned an unexpected '\
      'response. This is likely due to an incorrectly defined base URL.')]
  end

  @status_code = status_code
end

Instance Attribute Details

#errorsObject (readonly)

An array of error hashes with the properties 'object' (the LevelUp API object causing the error), 'property' (the property of that object causing the error), and 'message' (a human-readable error message).


8
9
10
# File 'lib/levelup/responses/error.rb', line 8

def errors
  @errors
end

#headersObject (readonly)

Any HTTP headers returned by the API, in hash form.


12
13
14
# File 'lib/levelup/responses/error.rb', line 12

def headers
  @headers
end

#status_codeObject (readonly)

The HTTP status code returned by the API


10
11
12
# File 'lib/levelup/responses/error.rb', line 10

def status_code
  @status_code
end

Instance Method Details

#success?Boolean

Errors are always unsuccessful.

Returns:

  • (Boolean)

32
33
34
# File 'lib/levelup/responses/error.rb', line 32

def success?
  false
end