lib/fatsecret_lite.rb in fatsecret_lite-0.2.3 vs lib/fatsecret_lite.rb in fatsecret_lite-0.2.5

- old
+ new

@@ -1,13 +1,11 @@ # lib/fatsecret_lite.rb require "oauth2" require "rest-client" require "json" -require_relative 'fatsecret_search' module FatsecretLite - extend FatsecretSearch class Configuration attr_accessor :client_id, :client_secret def initialize @client_id = nil @@ -86,8 +84,31 @@ rescue RestClient::ExceptionWithResponse => e puts "Error: #{e.response}" rescue StandardError => e puts "An error occurred: #{e.message}" end + + def find_food_by_search(search_value, max_results= 10) + access_token = get_access_token + + response = RestClient.post( + 'https://platform.fatsecret.com/rest/server.api', + { + method: 'foods.search.v3', + max_results: max_results, + search_expression: search_value, + format: 'json' + }, + { + content_type: :json, + Authorization: "Bearer #{access_token}" + } + ) + + JSON.parse(response.body) + rescue RestClient::ExceptionWithResponse => e + puts "Error: #{e.response}" + rescue StandardError => e + puts "An error occurred: #{e.message}" + end end - include FatsecretLite::Search end