Class: Nutritionix::API
- Inherits:
-
Object
- Object
- Nutritionix::API
- Defined in:
- lib/nutritionix.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#app_key ⇒ Object
Returns the value of attribute app_key.
-
#app_url ⇒ Object
Returns the value of attribute app_url.
Instance Method Summary collapse
-
#get_brand(id) ⇒ Object
This operation returns the a brand object that contains data on all its nutritional content.
-
#get_item(id) ⇒ Object
This operation returns an item object that contains data on all its nutritional content.
-
#get_serialized_params(params) ⇒ Object
Combine the parameter hash with access credentials.
-
#initialize(id, key, url = "http://api.nutritionix.com/v1/") ⇒ API
constructor
Create the Nutritionix API client.
-
#nutritionix_request(type, query, params) ⇒ Object
Performs a query request with the Nutritionix API Server.
-
#search(term, range_start = 0, range_end = 10, cal_min = 0, cal_max = 0, fields = NIL, brand_id = NIL) ⇒ Object
Pass a search term into the API like taco, or cheese fries, and the API will return an array of matching foods.
Constructor Details
#initialize(id, key, url = "http://api.nutritionix.com/v1/") ⇒ API
Create the Nutritionix API client.
17 18 19 20 21 |
# File 'lib/nutritionix.rb', line 17 def initialize(id, key, url="http://api.nutritionix.com/v1/") @app_id = id @app_key = key @app_url = url end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
7 8 9 |
# File 'lib/nutritionix.rb', line 7 def app_id @app_id end |
#app_key ⇒ Object
Returns the value of attribute app_key.
7 8 9 |
# File 'lib/nutritionix.rb', line 7 def app_key @app_key end |
#app_url ⇒ Object
Returns the value of attribute app_url.
7 8 9 |
# File 'lib/nutritionix.rb', line 7 def app_url @app_url end |
Instance Method Details
#get_brand(id) ⇒ Object
This operation returns the a brand object that contains data on all its nutritional content
95 96 97 |
# File 'lib/nutritionix.rb', line 95 def get_brand(id) nutritionix_request('brand',::CGI::escape(id), {}) end |
#get_item(id) ⇒ Object
This operation returns an item object that contains data on all its nutritional content
83 84 85 |
# File 'lib/nutritionix.rb', line 83 def get_item(id) nutritionix_request('item',::CGI::escape(id), {}) end |
#get_serialized_params(params) ⇒ Object
Combine the parameter hash with access credentials
107 108 109 110 111 112 113 114 115 |
# File 'lib/nutritionix.rb', line 107 def get_serialized_params(params) params['appId'] = @app_id params['appKey'] = @app_key request_params = [] params.each do |key, value| request_params << "#{key}=#{::CGI::escape(value)}" unless value.nil? end request_params.join('&') end |
#nutritionix_request(type, query, params) ⇒ Object
Performs a query request with the Nutritionix API Server
application_not_found
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nutritionix.rb', line 64 def nutritionix_request(type, query, params) serialized = get_serialized_params(params) url = "#{File.join("#{@app_url}", "#{type}", "#{query}")}?#{serialized}" header = {} begin response = RestClient.get url, header rescue Exception => e {:error => e.}.to_json end end |
#search(term, range_start = 0, range_end = 10, cal_min = 0, cal_max = 0, fields = NIL, brand_id = NIL) ⇒ Object
Pass a search term into the API like taco, or cheese fries, and the API will return an array of matching foods.
by default, the api will fetch the first 10 results Supports all item properties in comma delimited format. A null parameter will return the following item fields only: item_name, brand_name, item_id. NOTE– passing “*” as a value will return all item fields.
41 42 43 44 45 46 47 48 49 |
# File 'lib/nutritionix.rb', line 41 def search(term, range_start = 0, range_end = 10, cal_min = 0, cal_max = 0, fields = NIL, brand_id = NIL) nutritionix_request('search', ::CGI::escape(term), { :results => "#{range_start}:#{range_end}", :cal_min => "#{cal_min}", :cal_max => "#{cal_max}", :fields => fields, :brand_id => brand_id, }) end |