Class: TripAdvisor::TranslationTool::ResultsPaginator
- Inherits:
-
Object
- Object
- TripAdvisor::TranslationTool::ResultsPaginator
- Defined in:
- lib/trip_advisor/translation_tool.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Instance Method Summary collapse
- #current_page_index ⇒ Object
-
#initialize(base_url, attributes = {}) ⇒ ResultsPaginator
constructor
A new instance of ResultsPaginator.
- #load(options = {}) ⇒ Object
- #loaded? ⇒ Boolean
- #next_page ⇒ Object
- #page_count ⇒ Object
- #paginate ⇒ Object (also: #all)
- #previous_page ⇒ Object
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_url ⇒ Object (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 |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
34 35 36 |
# File 'lib/trip_advisor/translation_tool.rb', line 34 def offset @offset end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
34 35 36 |
# File 'lib/trip_advisor/translation_tool.rb', line 34 def params @params end |
#per_page ⇒ Object (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 |
#total ⇒ Object (readonly)
Returns the value of attribute total.
34 35 36 |
# File 'lib/trip_advisor/translation_tool.rb', line 34 def total @total end |
#translations ⇒ Object (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_index ⇒ Object
75 76 77 |
# File 'lib/trip_advisor/translation_tool.rb', line 75 def current_page_index offset / per_page end |
#load(options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/trip_advisor/translation_tool.rb', line 44 def load( = {}) @offset = [:offset] if [:offset] @offset = (([:page] - 1) * per_page) if [: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
60 61 62 |
# File 'lib/trip_advisor/translation_tool.rb', line 60 def loaded? @translations end |
#next_page ⇒ Object
79 80 81 |
# File 'lib/trip_advisor/translation_tool.rb', line 79 def next_page load(page: current_page_index + 1) end |
#page_count ⇒ Object
56 57 58 |
# File 'lib/trip_advisor/translation_tool.rb', line 56 def page_count (total / per_page.to_f).ceil end |
#paginate ⇒ Object 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_page ⇒ Object
83 84 85 |
# File 'lib/trip_advisor/translation_tool.rb', line 83 def previous_page load(page: current_page_index - 1) end |