Class: Files::List
Instance Method Summary collapse
-
#auto_paging_each(&block) ⇒ Object
Iterates through each resource in all pages, making additional fetches to the API as necessary.
-
#each(&block) ⇒ Object
Iterates through each resource in the current page.
-
#initialize(resource_wrapper, params, current_response = nil, current_options = nil, &request_page) ⇒ List
constructor
A new instance of List.
-
#next_page ⇒ Object
Fetches the next page of resources (if there is one).
Constructor Details
#initialize(resource_wrapper, params, current_response = nil, current_options = nil, &request_page) ⇒ List
Returns a new instance of List.
5 6 7 8 9 10 11 12 |
# File 'lib/files.com/list.rb', line 5 def initialize(resource_wrapper, params, current_response = nil, = nil, &request_page) @resource_wrapper = resource_wrapper @params = params @current_response = current_response @current_options = @request_page = request_page params[:per_page] ||= 1_000 end |
Instance Method Details
#auto_paging_each(&block) ⇒ Object
Iterates through each resource in all pages, making additional fetches to the API as necessary.
Note that this method will make as many API calls as necessary to fetch all resources. For more granular control, please see each
and next_page
.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/files.com/list.rb', line 20 def auto_paging_each(&block) return enum_for(:auto_paging_each).lazy unless block_given? loop do page = next_page break unless page.valid_response? page.set_cursor page.wrap_data do |data| block.call(data) end break if page.on_last_page? end end |
#each(&block) ⇒ Object
Iterates through each resource in the current page.
Note that this method makes no effort to fetch a new page when it gets to the end of the current page’s resources. See also auto_paging_each
.
41 42 43 44 45 46 47 |
# File 'lib/files.com/list.rb', line 41 def each(&block) page = current_page return [] unless page.valid_response? page.set_cursor page.wrap_data { |data| block.call data } end |
#next_page ⇒ Object
Fetches the next page of resources (if there is one).
This method will try to respect the per_page set. If none was given, the default per_page will be used.
53 54 55 |
# File 'lib/files.com/list.rb', line 53 def next_page self.class.new(resource_wrapper, params, *request_page.call, &request_page) end |