Class: VerticalResponse::API::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



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

def initialize(response)
  @url =          response['url']
  @items =        response['items']
  @attributes =   response['attributes']
  @links =        response['links']
  @success =      response['success']
  @error =        response['error']
  @raw_response = response

  handle_error unless success?
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Returns the value of attribute links.



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

def links
  @links
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



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

def raw_response
  @raw_response
end

#successObject (readonly)

Returns the value of attribute success.



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

def success
  @success
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#handle_collectionObject

Iterate through the collection of items and yield a new Response object for each one of them



25
26
27
28
29
# File 'lib/response.rb', line 25

def handle_collection
  items.map do |item|
    yield(Response.new(item))
  end
end

#handle_errorObject

Handles the case of an error response



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

def handle_error
  api_error = Error.new(error['message'] || error.inspect)
  # Keep track of the original error code and response object
  # for clients to have access to them if they rescue the exception
  api_error.code = error['code']
  api_error.api_response = self

  raise api_error
end

#success?Boolean

Determines if the response is a successful one or not

Returns:

  • (Boolean)


32
33
34
# File 'lib/response.rb', line 32

def success?
  !error
end