class Pandarus::RemoteCollection
Public Class Methods
new(http_client, target_class, path, query_params)
click to toggle source
# File lib/pandarus/remote_collection.rb, line 5 def initialize(http_client, target_class, path, query_params) @http_client = http_client @target_class = target_class @path = path @cache_pages = query_params[:cache_pages].nil? ? true : query_params[:cache_pages] @query_params = query_params.except(:cache_pages) @pagination_links = [] @next_page_cache = {} end
Public Instance Methods
each() { |member| ... }
click to toggle source
# File lib/pandarus/remote_collection.rb, line 20 def each if block_given? each_page do |page| page.each do |member| yield(member) end end else self.to_enum end end
each_page() { |first_page| ... }
click to toggle source
# File lib/pandarus/remote_collection.rb, line 32 def each_page if block_given? yield first_page yield next_page until was_last_page? else self.to_enum end end
first_page()
click to toggle source
# File lib/pandarus/remote_collection.rb, line 41 def first_page @pagination_links = [] response = @first_response || @http_client.get do |request| request.path = join_paths(base_path, @path) request.params = request.params.merge(@query_params) unless @query_params.empty? end @first_response ||= response if @cache_pages handle_response(response) end
next_page()
click to toggle source
# File lib/pandarus/remote_collection.rb, line 51 def next_page key = @pagination_links.last.next # cache_pages defaults to true, but for massive remote collections # keeping everything cached will eventually eat all the available memory if @cache_pages @next_page_cache[key] ||= @http_client.get(key) handle_response @next_page_cache[key] else handle_response @http_client.get(key) end end
to_a()
click to toggle source
# File lib/pandarus/remote_collection.rb, line 16 def to_a each_page.entries.flatten(1) end
Private Instance Methods
base_path()
click to toggle source
# File lib/pandarus/remote_collection.rb, line 86 def base_path @http_client.url_prefix.path end
handle_response(response)
click to toggle source
# File lib/pandarus/remote_collection.rb, line 70 def handle_response(response) response_links = response.env[:pagination_links] if response_links && !@pagination_links.any? {|existing_links| existing_links == response_links } update_pagination_links(response.env[:pagination_links]) end response.body.map{|member| @target_class.new(member) } end
join_paths(base, additional)
click to toggle source
# File lib/pandarus/remote_collection.rb, line 90 def join_paths(base, additional) base.gsub!(/\/+\z/, '') # remove trailing slashes additional.gsub!(/\A\/+/, '') # remove leading slashes [base, additional].join('/') end
update_pagination_links(response_pages)
click to toggle source
# File lib/pandarus/remote_collection.rb, line 78 def update_pagination_links(response_pages) if @cache_pages @pagination_links << response_pages else @pagination_links = [response_pages] end end
was_last_page?()
click to toggle source
# File lib/pandarus/remote_collection.rb, line 65 def was_last_page? return true if @pagination_links == [] @pagination_links.last.last_page? end