Class: Rotten::SearchResult

Inherits:
Set
  • Object
show all
Includes:
Api
Defined in:
lib/rotten/search_result.rb

Instance Method Summary collapse

Methods included from Api

included

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 options={}
  @path   = options[:path]
  @api_options  = options[:api_options] || {}
  @start  = options[:start] || 'movies'
  @json   = options[:json]
  @klass  = options[: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_pageObject



41
42
43
# File 'lib/rotten/search_result.rb', line 41

def current_page
  @api_options.delete(:page) || 1
end

#inspectObject



19
20
21
# File 'lib/rotten/search_result.rb', line 19

def inspect
  "<Rotten::SearchResult total=#{total} more='#{more?}'>"
end

#more?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rotten/search_result.rb', line 27

def more?
  total > size
end

#nextObject

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_aObject

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

#totalObject



23
24
25
# File 'lib/rotten/search_result.rb', line 23

def total
  @total ||= @json['total'].nil? ? size : @json['total'].to_i
end