Class: Tazworks::PagedResponse
- Inherits:
-
Object
- Object
- Tazworks::PagedResponse
- Defined in:
- lib/tazworks/paged_response.rb
Overview
Paged Response Model to Handle Paginated Results
Direct Known Subclasses
ApplicantPagedResponse, ClientPagedResponse, ClientProductPagedResponse, OrderPagedResponse
Instance Attribute Summary collapse
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#raw_response ⇒ Object
Returns the value of attribute raw_response.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(response, ids) ⇒ PagedResponse
constructor
A new instance of PagedResponse.
- #last ⇒ Object
-
#map_individual_object_ids(_ids, _attributes) ⇒ Object
this method is utilized to create proper ids for individual objects from the attributes.
- #next ⇒ Object
- #next_page ⇒ Object
- #raw_json_response ⇒ Object
- #response_links ⇒ Object
- #response_to_models ⇒ Object
-
#total_number_of_pages ⇒ Object
pages seem to start with 0, so defaulting to 0 when there are only “1” page.
Constructor Details
#initialize(response, ids) ⇒ PagedResponse
Returns a new instance of PagedResponse.
10 11 12 13 |
# File 'lib/tazworks/paged_response.rb', line 10 def initialize(response, ids) @ids = ids @raw_response = response end |
Instance Attribute Details
#ids ⇒ Object
Returns the value of attribute ids.
8 9 10 |
# File 'lib/tazworks/paged_response.rb', line 8 def ids @ids end |
#raw_response ⇒ Object
Returns the value of attribute raw_response.
8 9 10 |
# File 'lib/tazworks/paged_response.rb', line 8 def raw_response @raw_response end |
Class Method Details
.base_class ⇒ Object
15 16 17 |
# File 'lib/tazworks/paged_response.rb', line 15 def self.base_class raise 'Must Implement Me in the Concrete Class' end |
Instance Method Details
#last ⇒ Object
46 47 48 49 |
# File 'lib/tazworks/paged_response.rb', line 46 def last # might be nice to have a way that automatically calls the next page with the parameters already known here. raise 'Unimplemented' end |
#map_individual_object_ids(_ids, _attributes) ⇒ Object
this method is utilized to create proper ids for individual objects from the attributes
37 38 39 |
# File 'lib/tazworks/paged_response.rb', line 37 def map_individual_object_ids(_ids, _attributes) raise 'Must Implement Me in the Concrete Class' end |
#next ⇒ Object
41 42 43 44 |
# File 'lib/tazworks/paged_response.rb', line 41 def next # might be nice to have a way that automatically calls the next page with the parameters already known here. raise 'Unimplemented' end |
#next_page ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/tazworks/paged_response.rb', line 55 def next_page return nil unless response_links next_link = response_links.grep(/rel="next"/).first return nil if next_link.nil? # scary regex to pull the parameter off the url. next_link[/.*page=(\d+)&.*/, 1].to_i end |
#raw_json_response ⇒ Object
19 20 21 |
# File 'lib/tazworks/paged_response.rb', line 19 def raw_json_response @raw_response.body end |
#response_links ⇒ Object
51 52 53 |
# File 'lib/tazworks/paged_response.rb', line 51 def response_links @raw_response.headers[:link]&.split(',') end |
#response_to_models ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tazworks/paged_response.rb', line 23 def response_to_models return @collection if @collection json_response = JSON.parse(raw_response.body, { symbolize_names: true }) @collection = json_response.map do |json_object| self.class.base_class.new( ids: map_individual_object_ids(deep_copy(@ids), deep_copy(json_object)), attributes: json_object, attributes_loaded: true ) end end |
#total_number_of_pages ⇒ Object
pages seem to start with 0, so defaulting to 0 when there are only “1” page.
66 67 68 69 70 71 72 73 74 |
# File 'lib/tazworks/paged_response.rb', line 66 def total_number_of_pages return 0 unless response_links last_link = response_links.grep(/rel="last"/).first return 0 if last_link.nil? # scary regex to pull the parameter off the url. last_link[/.*page=(\d+)&.*/, 1].to_i end |