Class: Nutritionix::API

Inherits:
Object
  • Object
show all
Defined in:
lib/nutritionix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, key, url = "http://api.nutritionix.com/v1/") ⇒ API

Create the Nutritionix API client.

Parameters:

  • id

    Nutritionix application ID

  • key

    Nutritionix API key

  • url (Optional) (defaults to: "http://api.nutritionix.com/v1/")

    Nutritionix API url



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_idObject

Returns the value of attribute app_id.



7
8
9
# File 'lib/nutritionix.rb', line 7

def app_id
  @app_id
end

#app_keyObject

Returns the value of attribute app_key.



7
8
9
# File 'lib/nutritionix.rb', line 7

def app_key
  @app_key
end

#app_urlObject

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

Parameters:

  • id

    string The id of the brand you want to retrieve

Returns:

  • The brand as json string



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

Parameters:

  • id

    string The id of the brand you want to retrieve

Returns:

  • The brand as json string



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

Parameters:

  • params
    • Parameters associated with the query

Returns:

  • string The request results string



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

Parameters:

  • type

    string type of query. Current valid types are: search, item, brand

  • query

    string Query or search term / phrase

  • params

    hash Parameters associated with the query

Returns:

  • The request result as json string



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.message}.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.

Parameters:

  • term

    string The phrase or terms you would like to search by

  • range_start (defaults to: 0)

    integer (Optional)Start of the range of results to view a section of up to 500 items in the “hits” array

  • range_end (defaults to: 10)

    integer (Optional)End of the range of results to view a section of up to 500 items in the “hits” array

  • cal_min (defaults to: 0)

    integer (Optional)The minimum number of calories you want to be in an item returned in the results

  • cal_max (defaults to: 0)

    integer (Optional)The maximum number of calories you want to be in an item returned in the results

  • fields (defaults to: NIL)

    strings (Optional)The fields from an item you would like to return in the results.

  • brand_id (defaults to: NIL)

    string (Optional)Filter your results by a specific brand by passing in a brand_id

Returns:

  • The search results as json string



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