Class: TripAdvisor::TranslationTool::ResultsPaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/trip_advisor/translation_tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, attributes = {}) ⇒ ResultsPaginator

Returns a new instance of ResultsPaginator.



36
37
38
39
40
41
42
# 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

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def base_url
  @base_url
end

#offsetObject (readonly)

Returns the value of attribute offset.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def offset
  @offset
end

#paramsObject (readonly)

Returns the value of attribute params.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def params
  @params
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def per_page
  @per_page
end

#totalObject (readonly)

Returns the value of attribute total.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def total
  @total
end

#translationsObject (readonly)

Returns the value of attribute translations.



34
35
36
# File 'lib/trip_advisor/translation_tool.rb', line 34

def translations
  @translations
end

Instance Method Details

#current_page_indexObject



75
76
77
# File 'lib/trip_advisor/translation_tool.rb', line 75

def current_page_index
  offset / per_page
end

#load(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
# 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?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/trip_advisor/translation_tool.rb', line 60

def loaded?
  @translations
end

#next_pageObject



79
80
81
# File 'lib/trip_advisor/translation_tool.rb', line 79

def next_page
  load(page: current_page_index + 1)
end

#page_countObject



56
57
58
# File 'lib/trip_advisor/translation_tool.rb', line 56

def page_count
  (total / per_page.to_f).ceil
end

#paginateObject Also known as: all



64
65
66
67
68
69
70
71
# 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

#previous_pageObject



83
84
85
# File 'lib/trip_advisor/translation_tool.rb', line 83

def previous_page
  load(page: current_page_index - 1)
end