Class: VerticalResponse::API::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, access_token = nil) ⇒ Response



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/verticalresponse/api/response.rb', line 12

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

  @access_token = access_token

  handle_error unless success?
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Returns the value of attribute links.



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

def links
  @links
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



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

def raw_response
  @raw_response
end

#successObject (readonly)

Returns the value of attribute success.



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

def success
  @success
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/verticalresponse/api/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



28
29
30
31
32
# File 'lib/verticalresponse/api/response.rb', line 28

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

#handle_errorObject

Handles the case of an error response



40
41
42
43
44
45
46
47
48
# File 'lib/verticalresponse/api/response.rb', line 40

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



35
36
37
# File 'lib/verticalresponse/api/response.rb', line 35

def success?
  !error
end