Sha256: 389de44f42dccb67baa03e5299b2166d1c350d94d164f958730ea3fef7981f34

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

module FoodInfo
  module Adapters
    class FatSecret
      module Data
    
        class SearchResults < Hashie::Trash
          include Enumerable
          
          property :results,  :from => :food
          property :page,     :from => :page_number
          property :per_page, :from => :max_results
          property :total_results
      
          def initialize(*args)
            super(*args)            
            normalize_data
          end
          
          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
          
          # Allow direct enumerable access to search results without calling search('cheese').results.each
          def each(&block)
            self[:results].each{|result| block.call(result)}
          end
          
          # Allows pulling out a specific index without having to call results -- search('cheese')[3], not search('cheese').results[3]
          def [](idx)
            return super(idx) if idx.is_a?(Symbol) || idx.to_i.zero?
            self[:results][idx]
          end          
          
        end
    
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
food_info-0.0.7 lib/food_info/adapters/fat_secret/data/search_results.rb
food_info-0.0.6 lib/food_info/adapters/fat_secret/data/search_results.rb
food_info-0.0.5 lib/food_info/adapters/fat_secret/data/search_results.rb