Class: FoodInfo::Adapters::FatSecret
- Inherits:
-
Object
- Object
- FoodInfo::Adapters::FatSecret
- Includes:
- HTTParty
- Defined in:
- lib/food_info/adapters/fat_secret.rb,
lib/food_info/adapters/fat_secret/request.rb,
lib/food_info/adapters/fat_secret/data/food_item.rb,
lib/food_info/adapters/fat_secret/data/food_serving.rb,
lib/food_info/adapters/fat_secret/data/search_result.rb,
lib/food_info/adapters/fat_secret/data/search_results.rb
Defined Under Namespace
Modules: Data Classes: Request
Constant Summary collapse
- RETRY_INVALID_TIMESTAMP =
Only retry once to avoid huge pileup if concurrent
1
Instance Method Summary collapse
- #details(food_id, opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ FatSecret
constructor
A new instance of FatSecret.
- #search(q, opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ FatSecret
Returns a new instance of FatSecret.
15 16 17 18 |
# File 'lib/food_info/adapters/fat_secret.rb', line 15 def initialize(opts = {}) raise AuthorizationError.new("Missing required argument :key") unless @key = opts[:key] raise AuthorizationError.new("Missing required argument :secret") unless @secret = opts[:secret] end |
Instance Method Details
#details(food_id, opts = {}) ⇒ Object
33 34 35 36 |
# File 'lib/food_info/adapters/fat_secret.rb', line 33 def details(food_id, opts = {}) data = query('food.get', :food_id => food_id) Data::FoodItem.new( data['food'] ) end |
#search(q, opts = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/food_info/adapters/fat_secret.rb', line 20 def search(q, opts = {}) params = { :search_expression => URI.escape(q), :page_number => opts[:page] || 1, :max_results => opts[:per_page] || 20 } params[:page_number] = [params[:page_number].to_i - 1, 0].max # FatSecret's pagination starts at 0 params[:max_results] = [params[:max_results].to_i, 50].min # FatSecret's max allowed results per page data = query('foods.search', params) Data::SearchResults.new( data['foods'] ) end |