Class: Rotten::SearchResult
- Inherits:
-
Set
- Object
- Set
- Rotten::SearchResult
- Includes:
- Api
- Defined in:
- lib/rotten/search_result.rb
Instance Method Summary collapse
-
#[](index) ⇒ Object
Make the Set more array-like and allow index-based access.
- #current_page ⇒ Object
-
#initialize(options = {}) ⇒ SearchResult
constructor
def initialize url, json={}, start=‘movies’.
- #inspect ⇒ Object
- #more? ⇒ Boolean
-
#next ⇒ Object
Fetch additional results, while more exist.
-
#to_a ⇒ Object
Cache Set#to_a and return it.
- #total ⇒ Object
Methods included from Api
Constructor Details
#initialize(options = {}) ⇒ SearchResult
def initialize url, json={}, start=‘movies’
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rotten/search_result.rb', line 8 def initialize ={} @path = [:path] @api_options = [:api_options] || {} @start = [:start] || 'movies' @json = [:json] @klass = [:class] @proper = @klass.extract_info(@klass, @json[@start]) super([@proper].flatten) end |
Instance Method Details
#[](index) ⇒ Object
Make the Set more array-like and allow index-based access
32 33 34 |
# File 'lib/rotten/search_result.rb', line 32 def [](index) to_a[index] end |
#current_page ⇒ Object
41 42 43 |
# File 'lib/rotten/search_result.rb', line 41 def current_page @api_options.delete(:page) || 1 end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/rotten/search_result.rb', line 19 def inspect "<Rotten::SearchResult total=#{total} more='#{more?}'>" end |
#more? ⇒ Boolean
27 28 29 |
# File 'lib/rotten/search_result.rb', line 27 def more? total > size end |
#next ⇒ Object
Fetch additional results, while more exist.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rotten/search_result.rb', line 46 def next if more? @api_options.merge! :page => current_page + 1 @api_options.merge! :page_limit => Movie.maximum_per_page @klass.get @path, @api_options do |json| [@klass.extract_info(@klass, json[@start])].flatten.each do |x| add x end @to_a = nil end end end |
#to_a ⇒ Object
Cache Set#to_a and return it. Calling #next resets it.
37 38 39 |
# File 'lib/rotten/search_result.rb', line 37 def to_a @to_a ||= super.to_a end |
#total ⇒ Object
23 24 25 |
# File 'lib/rotten/search_result.rb', line 23 def total @total ||= @json['total'].nil? ? size : @json['total'].to_i end |