Class: CTG::Response
- Inherits:
-
Object
- Object
- CTG::Response
- Defined in:
- lib/ctg/response.rb,
lib/ctg/response/csv_response.rb,
lib/ctg/response/json_response.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CSVResponse, JSONResponse
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.parse(response_body, format, client) ⇒ CTG::Response
Factory method to create an appropriate response object based on format.
Instance Method Summary collapse
-
#initialize(client) ⇒ Response
constructor
Initializes the Response object.
-
#next_page ⇒ CTG::Response?
-
Returns a new Response object containing the next page of data.
-
-
#query(*keys) ⇒ Object
Queries the response data by keys (to be implemented in subclasses).
Constructor Details
#initialize(client) ⇒ Response
Initializes the Response object
38 39 40 |
# File 'lib/ctg/response.rb', line 38 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object (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
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_page ⇒ CTG::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.
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.[:format] || 'json' request.[: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)
45 46 47 |
# File 'lib/ctg/response.rb', line 45 def query(*keys) raise NotImplementedError, 'Subclasses must implement the `query` method' end |