Class: FoodInfo::Adapters::FatSecret::Data::SearchResults

Inherits:
Hashie::Trash
  • Object
show all
Includes:
Enumerable
Defined in:
lib/food_info/adapters/fat_secret/data/search_results.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SearchResults

Returns a new instance of SearchResults.



14
15
16
17
# File 'lib/food_info/adapters/fat_secret/data/search_results.rb', line 14

def initialize(*args)
  super(*args)            
  normalize_data
end

Instance Method Details

#[](idx) ⇒ Object

Allows pulling out a specific index without having to call results – search(‘cheese’), not search(‘cheese’).results



35
36
37
38
# File 'lib/food_info/adapters/fat_secret/data/search_results.rb', line 35

def [](idx)
  return super(idx) if idx.is_a?(Symbol) || idx.to_i.zero?
  self[:results][idx]
end

#each(&block) ⇒ Object

Allow direct enumerable access to search results without calling search(‘cheese’).results.each



30
31
32
# File 'lib/food_info/adapters/fat_secret/data/search_results.rb', line 30

def each(&block)
  self[:results].each{|result| block.call(result)}
end

#normalize_dataObject



19
20
21
22
23
24
25
26
27
# File 'lib/food_info/adapters/fat_secret/data/search_results.rb', line 19

def normalize_data            
  [:page, :per_page, :total_results].each do |n|
    self[n] = self[n].to_i
  end            
  
  self[:page] += 1 # FatSecret indexes their pages from 0
  self[:results] = [self[:results]] unless self[:results].is_a?(Array)
  self[:results] = (self[:results] || []).collect {|result| SearchResult.new(result) }
end