Class: Procore::Response

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

Overview

Wrapper class for a response received from the Procore API. Stores the body, code, headers, and pagination information.

When a response returns a collection of elements, a Link Header is included in the response. This header contains one or more URLs that can be used to access more results.

The possible values for pagination are:

next  URL for the immediate next page of results.
last  URL for the last page of results.
first URL for the first page of results.
prev  URL for the immediate previous page of results.

Examples:

Getting the details about the response.

response = client.get("projects")
response.body #=> [{ id: 5, name: "Project 5" }]
response.code #=> 200

Using pagination

first_page = client.get("projects")

# The first page will only have URLs for :next & :last
first_page.pagination[:first]
  #=> nil
first_page.pagination[:next]
  #=> "projects?per_page=20&page=2"

# Any other page will have all keys
next_page = client.get(first_page.pagination[:next])
next_page.pagination[:first]
  #=> "projects?per_page=20&page=1"

# The last page will only have URLs for :first & :next
last_page = client.get(first_page.pagination[:last])
last_page.pagination[:last]
  #=> nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, headers:, code:, request:, request_body:) ⇒ Response



50
51
52
53
54
55
56
57
# File 'lib/procore/response.rb', line 50

def initialize(body:, headers:, code:, request:, request_body:)
  @code = code
  @headers = headers
  @pagination = parse_pagination
  @request = request
  @request_body = request_body
  @raw_body = !body.to_s.empty? ? body : "{}".to_json
end

Instance Attribute Details

#codeInteger (readonly)



48
# File 'lib/procore/response.rb', line 48

attr_reader :headers, :code, :pagination, :request, :request_body

#headersHash<String, String> (readonly)



48
49
50
# File 'lib/procore/response.rb', line 48

def headers
  @headers
end

#paginationHash<Symbol, String> (readonly)



48
# File 'lib/procore/response.rb', line 48

attr_reader :headers, :code, :pagination, :request, :request_body

#requestObject (readonly)

Returns the value of attribute request.



48
49
50
# File 'lib/procore/response.rb', line 48

def request
  @request
end

#request_bodyObject (readonly)

Returns the value of attribute request_body.



48
49
50
# File 'lib/procore/response.rb', line 48

def request_body
  @request_body
end

Instance Method Details

#bodyArray<Hash>, Hash



61
62
63
# File 'lib/procore/response.rb', line 61

def body
  @body ||= parse_body
end