Class: CTG::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ctg/response.rb,
lib/ctg/response/csv_response.rb,
lib/ctg/response/json_response.rb

Direct Known Subclasses

CSVResponse, JSONResponse

Defined Under Namespace

Classes: CSVResponse, JSONResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Response

Initializes the Response object

Parameters:

  • client (CTG)
    • The client instance that made the request



38
39
40
# File 'lib/ctg/response.rb', line 38

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/ctg/response.rb', line 18

def client
  @client
end

Class Method Details

.parse(response_body, format, client) ⇒ CTG::Response

Factory method to create an appropriate response object based on format

Parameters:

  • response_body (String)
    • The raw response body from the API

  • format (String)
    • The format of the response (‘json’ or ‘csv’)

  • client (CTG)
    • The client instance to use for fetching subsequent pages

Returns:

  • (CTG::Response)
    • An instance of either JSONResponse or CSVResponse



25
26
27
28
29
30
31
32
33
34
# File 'lib/ctg/response.rb', line 25

def self.parse(response_body, format, client)
  case format
  when 'json'
    JSONResponse.new(response_body, client)
  when 'csv'
    CSVResponse.new(response_body, client)
  else
    raise "Unsupported format: #{format}"
  end
end

Instance Method Details

#next_pageCTG::Response?

Returns - Returns a new Response object containing the next page of data.

  • Returns ‘nil` if no next page token is found, indicating there is no further data.

Returns:

  • (CTG::Response, nil)
    • Returns a new Response object containing the next page of data.

    • Returns ‘nil` if no next page token is found, indicating there is no further data.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ctg/response.rb', line 52

def next_page
  next_page_token = @client.response.headers['x-next-page-token'] || @data['nextPageToken']
  return unless next_page_token

  request = @client.response.request
  format = request.options[:format] || 'json'

  request.options[:query].merge! CTG::Query.new
                                           .page_token(next_page_token)
                                           .params

  CTG::Response.parse(request.perform.body, format, @client)
end

#query(*keys) ⇒ Object

Queries the response data by keys (to be implemented in subclasses)

Parameters:

  • keys (Array<String>)
    • The sequence of keys to query the data

Returns:

  • (Object)
    • The queried data

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/ctg/response.rb', line 45

def query(*keys)
  raise NotImplementedError, 'Subclasses must implement the `query` method'
end