nutritionix 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -1
- data/lib/nutritionix/api_1_1.rb +203 -0
- data/lib/nutritionix/version.rb +1 -1
- data/script/test_api_1_1.rb +21 -0
- metadata +4 -2
data/README.md
CHANGED
@@ -18,7 +18,26 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
* For NXQL Supported search:
|
22
|
+
|
23
|
+
app_id = '<YOUR_APP_ID>'
|
24
|
+
app_key = '<YOUR_APP_KEY>'
|
25
|
+
provider = Nutritionix::Api_1_1.new(app_id, app_key)
|
26
|
+
search_params = {
|
27
|
+
offset: 0,
|
28
|
+
limit: 50,
|
29
|
+
fields: ['brand_id', 'brand_name', 'item_id', 'item_name', 'nf_calories'],
|
30
|
+
query: 'potato'
|
31
|
+
}
|
32
|
+
results_json = provider.nxql_search(search_params)
|
33
|
+
puts "Results: #{results_json}"
|
34
|
+
|
35
|
+
* Note:
|
36
|
+
* There is a standalone test script **/script/test_api_1_1.rb** available
|
37
|
+
which can be readily used for testing.You only need to replace <YOUR_APP_ID>
|
38
|
+
and <YOUR_APP_KEY> with your nutritionix app credentials.
|
39
|
+
|
40
|
+
* Logs generated can be found at default location <HOME_DIRECTORY>/nutritionix_api_logs.txt
|
22
41
|
|
23
42
|
## Contributing
|
24
43
|
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require "cgi"
|
3
|
+
require 'logger'
|
4
|
+
require 'active_support/json'
|
5
|
+
|
6
|
+
module Nutritionix
|
7
|
+
# Reference: https://github.com/mftaher/nutritionix-api-ruby-library/blob/master/lib/nutritionix.rb
|
8
|
+
class Api_1_1
|
9
|
+
attr_accessor :app_id, :app_key, :app_url
|
10
|
+
attr_accessor :logger
|
11
|
+
|
12
|
+
#
|
13
|
+
# Create the Nutritionix API client.
|
14
|
+
#
|
15
|
+
# @param id Nutritionix application ID
|
16
|
+
# @param key Nutritionix API key
|
17
|
+
# @param url (Optional) Nutritionix API url
|
18
|
+
#
|
19
|
+
def initialize(id, key, url="https://api.nutritionix.com/v1_1", logger=APILogger.default_logger)
|
20
|
+
@app_id = id
|
21
|
+
@app_key = key
|
22
|
+
@app_url = url
|
23
|
+
@logger = logger
|
24
|
+
end
|
25
|
+
|
26
|
+
# Sends a POST request to the Nutritionix API Server
|
27
|
+
#
|
28
|
+
# @param endpoint The endpoint to send the request to.Current valid type is: search
|
29
|
+
#
|
30
|
+
# @param params a hash containing required query, filters, etc options as defined
|
31
|
+
# by https://developer.nutritionix.com/docs/v1_1 Nutritionix Querying Language
|
32
|
+
# (NXQL) convertible to a valid JSON.
|
33
|
+
#
|
34
|
+
# @return The request result or error as json string
|
35
|
+
#
|
36
|
+
def post_request(endpoint, params={})
|
37
|
+
params = sanitize_params(params)
|
38
|
+
add_creds_to_params(params)
|
39
|
+
|
40
|
+
params_json = params.to_json
|
41
|
+
logger.debug "======POST Request Params Json: #{params_json}"
|
42
|
+
|
43
|
+
url = [@app_url, endpoint].join('/')
|
44
|
+
logger.debug "POST request URL: #{url}"
|
45
|
+
begin
|
46
|
+
# Reference: http://rubydoc.info/gems/rest-client/1.6.7/RestClient.post
|
47
|
+
response = RestClient.post(url, params_json, content_type: 'application/json')
|
48
|
+
rescue Exception => e
|
49
|
+
logger.error "==================================================="
|
50
|
+
logger.debug "An exception occured while processing POST request to url: #{url}"
|
51
|
+
logger.error e.to_s
|
52
|
+
logger.error "==================================================="
|
53
|
+
response = { error: e.message}.to_json
|
54
|
+
end
|
55
|
+
|
56
|
+
response
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sends a GET request to the Nutritionix API Server
|
60
|
+
#
|
61
|
+
# @param query string Query or search term / phrase
|
62
|
+
# @param endpoint The endpoint to send the request to.Current valid type is: search, item, brand
|
63
|
+
#
|
64
|
+
# @param params a hash containing required query, filters, etc options as defined
|
65
|
+
# by https://developer.nutritionix.com/docs/v1_1 Nutritionix Querying Language
|
66
|
+
# (NXQL) convertible to a valid JSON.
|
67
|
+
#
|
68
|
+
# @return The request result or error as json string
|
69
|
+
#
|
70
|
+
def get_request(query, endpoint, params={})
|
71
|
+
query = ::CGI::escape(query)
|
72
|
+
params = sanitize_params(params)
|
73
|
+
add_creds_to_params(params)
|
74
|
+
|
75
|
+
serialized_params = serialize_params(params)
|
76
|
+
|
77
|
+
url_components = [@app_url, endpoint]
|
78
|
+
if 'item' == endpoint
|
79
|
+
# Heroku using older version of Ruby prepend method on String is
|
80
|
+
# not available.Thus using String's insert method
|
81
|
+
serialized_params.insert(0, "id=#{query}&")
|
82
|
+
else
|
83
|
+
url_components << query
|
84
|
+
end
|
85
|
+
|
86
|
+
url = "#{url_components.join('/')}?#{serialized_params}"
|
87
|
+
logger.debug "GET request URL: #{url}"
|
88
|
+
header = {}
|
89
|
+
begin
|
90
|
+
response = RestClient.get url, header
|
91
|
+
rescue Exception => e
|
92
|
+
logger.error "==================================================="
|
93
|
+
logger.debug "An exception occured while processing GET request to url: #{url}"
|
94
|
+
logger.error e.to_s
|
95
|
+
logger.error "==================================================="
|
96
|
+
response = { error: e.message}.to_json
|
97
|
+
end
|
98
|
+
|
99
|
+
response
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# Pass a search term into the API like taco, or cheese fries, and the
|
104
|
+
# NutritionIX API will return an array of matching foods.
|
105
|
+
#
|
106
|
+
# @param term string The phrase or terms you would like to search by
|
107
|
+
# @param range_start integer (Optional)Start of the range of results to view a section of up to 500 items in the "hits" array
|
108
|
+
# @param range_end integer (Optional)End of the range of results to view a section of up to 500 items in the "hits" array
|
109
|
+
# by default, the api will fetch the first 10 results
|
110
|
+
# @param cal_min integer (Optional)The minimum number of calories you want to be in an item returned in the results
|
111
|
+
# @param cal_max integer (Optional)The maximum number of calories you want to be in an item returned in the results
|
112
|
+
# @param fields strings (Optional)The fields from an item you would like to return in the results.
|
113
|
+
# Supports all item properties in comma delimited format.
|
114
|
+
# A null parameter will return the following item fields only: item_name, brand_name, item_id.
|
115
|
+
# NOTE-- passing "*" as a value will return all item fields.
|
116
|
+
# @param brand_id string (Optional)Filter your results by a specific brand by passing in a brand_id
|
117
|
+
#
|
118
|
+
# @return The search results as json string
|
119
|
+
#
|
120
|
+
def search(term, range_start = 0, range_end = 10, cal_min = 0, cal_max = 0, fields = NIL, brand_id = NIL)
|
121
|
+
get_request(term, 'search', {
|
122
|
+
:results => "#{range_start}:#{range_end}",
|
123
|
+
:cal_min => "#{cal_min}",
|
124
|
+
:cal_max => "#{cal_max}",
|
125
|
+
:fields => fields,
|
126
|
+
:brand_id => brand_id,
|
127
|
+
})
|
128
|
+
end
|
129
|
+
|
130
|
+
def nxql_search(search_params={})
|
131
|
+
# Default sort options
|
132
|
+
sort_options = {
|
133
|
+
sort: {
|
134
|
+
field: "_score",
|
135
|
+
order: "desc"
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
search_params.merge!(sort_options) unless search_params[:sort].nil?
|
140
|
+
|
141
|
+
logger.debug "Nutritionix::Api_1_1 NXQL search params: #{search_params}"
|
142
|
+
|
143
|
+
post_request('search', search_params)
|
144
|
+
end
|
145
|
+
|
146
|
+
#
|
147
|
+
# This operation returns an item object that contains data on all its nutritional content
|
148
|
+
#
|
149
|
+
# @param id string The id of the food item whose details are needed
|
150
|
+
#
|
151
|
+
# @return the item details as json string
|
152
|
+
#
|
153
|
+
def get_item(id)
|
154
|
+
get_request(id, 'item', {})
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def add_creds_to_params(params)
|
160
|
+
params[:appId] = @app_id
|
161
|
+
params[:appKey] = @app_key
|
162
|
+
end
|
163
|
+
|
164
|
+
def sanitize_params(params)
|
165
|
+
params = {} unless params.is_a? Hash
|
166
|
+
params
|
167
|
+
end
|
168
|
+
|
169
|
+
def serialize_params(params)
|
170
|
+
request_params = []
|
171
|
+
params.each do |key, value|
|
172
|
+
request_params << "#{key}=#{::CGI::escape(value)}" unless value.nil?
|
173
|
+
end
|
174
|
+
request_params.join('&')
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
class APIException < Exception
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
# http://ruby.about.com/od/tasks/a/logger.htm
|
184
|
+
class APILogger
|
185
|
+
attr_reader :logger_instance
|
186
|
+
attr_accessor :file
|
187
|
+
|
188
|
+
# @param logger the logger instance to be used for logging
|
189
|
+
# @param file. Default outputs log to <HOME_DIR>/nutritionix_api_logs.txt
|
190
|
+
def initialize(logger=nil, file=File.join(Dir.home, 'nutritionix_api_logs.txt'))
|
191
|
+
@file = file
|
192
|
+
@logger_instance = logger || Logger.new(@file)
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns the default logger(Logger) instance distributed by Ruby in its
|
196
|
+
# standard library.
|
197
|
+
def self.default_logger
|
198
|
+
logger = APILogger.new
|
199
|
+
logger.logger_instance
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
end
|
data/lib/nutritionix/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative '../lib/nutritionix/api_1_1'
|
2
|
+
|
3
|
+
def nxql_search_params
|
4
|
+
fields = %w(brand_id brand_name item_id item_name nf_serving_size_qty nf_serving_size_unit)
|
5
|
+
fields << %w(nf_calories nf_total_carbohydrate nf_sodium nf_dietary_fiber nf_protein)
|
6
|
+
default_fields = fields.flatten
|
7
|
+
|
8
|
+
{
|
9
|
+
offset: 0,
|
10
|
+
limit: 50,
|
11
|
+
fields: default_fields
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
app_id = '<YOUR_APP_ID>'
|
16
|
+
app_key = '<YOUR_APP_KEY>'
|
17
|
+
provider = Nutritionix::Api_1_1.new(app_id, app_key)
|
18
|
+
search_params = nxql_search_params
|
19
|
+
search_params.merge!(query: 'potato')
|
20
|
+
results_json = provider.nxql_search(search_params)
|
21
|
+
puts "Results: #{results_json}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutritionix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
@@ -89,8 +89,10 @@ files:
|
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
91
|
- lib/nutritionix.rb
|
92
|
+
- lib/nutritionix/api_1_1.rb
|
92
93
|
- lib/nutritionix/version.rb
|
93
94
|
- nutritionix.gemspec
|
95
|
+
- script/test_api_1_1.rb
|
94
96
|
- spec/nutritionix_spec.rb
|
95
97
|
- spec/spec_helper.rb
|
96
98
|
homepage: ''
|