class TripAdvisor::TranslationTool::ResultsPaginator
Attributes
base_url[R]
offset[R]
params[R]
per_page[R]
total[R]
translations[R]
Public Class Methods
new(base_url, attributes = {})
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 36 def initialize(base_url, attributes = {}) @base_url = base_url @total = 0 @per_page = 50 @offset = attributes[:offset] || 0 @params = attributes[:params] end
Public Instance Methods
current_page_index()
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 75 def current_page_index offset / per_page end
load(options = {})
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 44 def load(options = {}) @offset = options[:offset] if options[:offset] @offset = ((options[:page] - 1) * per_page) if options[:page] raise ArgumentError, "Cannot load a negative offset" if @offset < 0 raise ArgumentError, "Cannot load an offset greater than the total number of objects" if total && @offset > total response = RestClient.post(base_url, params.to_json, :content_type => :json, :accept => :json) doc = JSON.parse(response) @total = doc.count @per_page = doc.count @translations = Scraper.translations_from_doc(doc) end
loaded?()
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 60 def loaded? @translations end
next_page()
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 79 def next_page load(page: current_page_index + 1) end
page_count()
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 56 def page_count (total / per_page.to_f).ceil end
paginate() { |page_number, translations| ... }
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 64 def paginate load unless loaded? 1.upto(page_count).collect do |page_number| load(page: page_number).tap do |translations| yield page_number, translations if block_given? end end.flatten end
Also aliased as: all
previous_page()
click to toggle source
# File lib/trip_advisor/translation_tool.rb, line 83 def previous_page load(page: current_page_index - 1) end