Sha256: 4d5ed1bdcb2cd0f6949635dc1c84c984fde0b94822fee35ef2c5efd0871fc59b

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 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
          
        end
    
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
food_info-0.0.3 lib/food_info/adapters/fat_secret/data/search_results.rb
food_info-0.0.2 lib/food_info/adapters/fat_secret/data/search_results.rb