Repository navigation
Added paginator which does automatic pagination and implemented it in th... #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
100cfd7
af5ce4a
53fb304
90d899c
1d22ec0
09c08bf
784db32
a3dd441
720a115
9c77e69
661e49e
02e05e8
6d1f96d
5465cb0
2861592
30e4fc0
affa352
aeaa73e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| module EndlessPages | ||
| extend ActiveSupport::Concern | ||
|
|
||
| module ClassMethods | ||
|
|
||
| def endless_pages(opts = {}, &block) | ||
| @opts = opts | ||
| perform(&block) | ||
| end | ||
|
|
||
| def conn | ||
| @opts[:connection] ||= Adcloud::Connection.new | ||
| end | ||
|
|
||
| def endpoint | ||
| @opts[:endpoint_url] ||= self.endpoint_url | ||
| raise "You must either pass endpoint_url into endless_pages OR define a class method .endpoint_url" unless @opts[:endpoint_url] | ||
| @opts[:endpoint_url] | ||
| end | ||
|
|
||
| def params | ||
| @opts[:params] ||= {} | ||
| end | ||
|
|
||
| def perform(&block) | ||
| paged_items = [] | ||
|
|
||
| page = params[:page] ||= 1 | ||
| page = 1 if page == 0 | ||
|
|
||
| total_pages = 1 | ||
| retry_count = 0 | ||
| page_result = params[:page_result] ||= [] | ||
|
|
||
| begin | ||
| raw_result = conn.get(endpoint, params) | ||
| total_pages = raw_result['_meta']['total_pages'] | ||
|
|
||
| page_result = self.new(raw_result) # your service class needs to include Virtus | ||
| # if a block is provided then start returning results right away | ||
| page_result.items.each do |item| | ||
| block.call(item) | ||
| end if block | ||
|
|
||
| paged_items += page_result.items | ||
| page += 1 | ||
|
|
||
| rescue => ex | ||
| if retry_count <= 5 | ||
| retry | ||
| else | ||
| Adcloud.logger.error { "API Exception #{ex}" } | ||
| raise ApiError, "Connection failed" | ||
| end | ||
| retry_count += 1 | ||
| end while page < total_pages | ||
|
|
||
| paged_items | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some tests would be nice...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we are trying to get this out the door. tests are a nice to have... ie there are none at the moment.. but should be.. but not this sprint |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| module Adcloud | ||
| class Report | ||
| include Virtus | ||
| include EndlessPages | ||
|
|
||
| attribute :_meta, Hash | ||
| attribute :items, Array[Adcloud::ReportEntry] | ||
|
|
@@ -13,34 +14,21 @@ def find_by_date(date) | |
| return self.new(result) | ||
| end | ||
|
|
||
| def find_all_by_date(date) | ||
| paged_items = [] | ||
| page = 0 | ||
| total_pages = 1 | ||
| retry_count = 0 | ||
| page_result = nil | ||
| begin | ||
| begin | ||
| page += 1 | ||
| raw_result = connection.get(self.api_endpoint, { filter: { date: date.to_s }, :page => page, :per_page => Entity::MAX_PER_PAGE, new_backend: true }) | ||
| total_pages = raw_result['_meta']['total_pages'] | ||
| page_result = self.new(raw_result) | ||
| paged_items += page_result.items | ||
| rescue => ex | ||
| retry if retry_count < 5 | ||
| end | ||
| end while page < total_pages | ||
| page_result.items = paged_items | ||
| page_result | ||
| def find_all_by_date(date, booking_ids=[]) | ||
| params = { filter: { date: date.to_s }, :per_page => Entity::MAX_PER_PAGE, new_backend: true } | ||
|
|
||
| if booking_ids | ||
| params[:filter][:booking_id] = booking_ids | ||
| params[:new_backend] = false | ||
| end | ||
|
|
||
| endless_pages(endpoint_url: self.api_endpoint, params: params) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this call not look like: because the first argument from endless_pages should be an hash, or not?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no.. its called a splat.. ie.. custom_method(name_a=1, name_b=4, monkies={}) in the def custom_method(opts={}) that opts hash is a representation of all the keyword arguments pased in.. its like **kwargs in python |
||
| end | ||
|
|
||
| def api_endpoint | ||
| @api_endpoint ||= self.name.demodulize.tableize | ||
| end | ||
|
|
||
| def connection | ||
| @connection ||= Connection.new | ||
| end | ||
| end | ||
|
|
||
| # Define this after class methods are added | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module Adcloud | ||
| VERSION = '0.7.6.0' | ||
| VERSION = '0.7.6.2' | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should virtus than not be enforced somehow? Maybe throw an exception if its not included?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure how this is enforced in ruby-land