Sha256: 90597d89883cfb46bc2fd6324f618ce44fb353f6e7101ae8a2296bcb2d727c4f
Contents?: true
Size: 1.64 KB
Versions: 2
Compression:
Stored size: 1.64 KB
Contents
require 'hashie' require "food_info/utils" require "food_info/errors" require "food_info/adapters" require "food_info/version" module FoodInfo # Allow extending to additional data sources in the future # Each adapter should implement +search+ and +details+ methods. ADAPTERS = {:fat_secret => FoodInfo::Adapters::FatSecret} class << self # Sets the adapter we'll be pulling data from. def establish_connection(adapter_name, opts = {}) klass = ADAPTERS[adapter_name.to_sym] raise UnsupportedAdapter.new("Requested adapter ('#{adapter_name}') is unknown") unless klass @@pool = [] @@cursor = 0 (opts.delete(:pool) || 1).to_i.times do @@pool << klass.new(opts) end true end def search(q, opts = {}) cached(:search, q, opts) end def details(id, opts = {}) cached(:details, id) end def cached(method, param, opts = {}, &block) # TODO - implement caching strategy next_adapter.send(method, param, opts) end # FUTURE: This connection pool code won't do much good until HTTParty is non-blocking def next_adapter raise NoAdapterSpecified.new("You must run FoodInfo.establish_connection first") unless defined?(@@pool) @@cursor = (@@cursor + 1) % @@pool.length @@pool[@@cursor] end end end __END__ FoodInfo.establish_connection(:fat_secret, :key => ENV['KEY'], :secret => ENV['SECRET']) a=FoodInfo.search('cheese', :page => 1, :per_page => 1).results.first a=FoodInfo.search('cheese') FoodInfo.establish_connection(:fat_secret, :key => ENV['KEY'], :secret => ENV['SECRET']) a=FoodInfo.details("33689")
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
food_info-0.0.6 | lib/food_info.rb |
food_info-0.0.5 | lib/food_info.rb |