nutritionix 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/nutritionix.rb +122 -0
- data/lib/nutritionix/version.rb +3 -0
- data/nutritionix.gemspec +26 -0
- data/spec/nutritionix_spec.rb +34 -0
- data/spec/spec_helper.rb +24 -0
- metadata +123 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fazle Taher
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Nutritionix
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nutritionix'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nutritionix
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/nutritionix.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require "nutritionix/version"
|
2
|
+
require "rest-client"
|
3
|
+
require "cgi"
|
4
|
+
|
5
|
+
module Nutritionix
|
6
|
+
class API
|
7
|
+
attr_accessor :app_id, :app_key, :app_url
|
8
|
+
|
9
|
+
#
|
10
|
+
# Create the Nutritionix API client.
|
11
|
+
#
|
12
|
+
# @param id Nutritionix application ID
|
13
|
+
# @param key Nutritionix API key
|
14
|
+
# @param url (Optional) Nutritionix API url
|
15
|
+
#
|
16
|
+
|
17
|
+
def initialize(id, key, url="http://api.nutritionix.com/v1/")
|
18
|
+
@app_id = id
|
19
|
+
@app_key = key
|
20
|
+
@app_url = url
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Pass a search term into the API like taco, or cheese fries, and the API will return an array of matching foods.
|
25
|
+
#
|
26
|
+
# @param term string The phrase or terms you would like to search by
|
27
|
+
# @param range_start integer (Optional)Start of the range of results to view a section of up to 500 items in the "hits" array
|
28
|
+
# @param range_end integer (Optional)End of the range of results to view a section of up to 500 items in the "hits" array
|
29
|
+
# by default, the api will fetch the first 10 results
|
30
|
+
# @param cal_min integer (Optional)The minimum number of calories you want to be in an item returned in the results
|
31
|
+
# @param cal_max integer (Optional)The maximum number of calories you want to be in an item returned in the results
|
32
|
+
# @param fields strings (Optional)The fields from an item you would like to return in the results.
|
33
|
+
# Supports all item properties in comma delimited format.
|
34
|
+
# A null parameter will return the following item fields only: item_name, brand_name, item_id.
|
35
|
+
# NOTE-- passing "*" as a value will return all item fields.
|
36
|
+
# @param brand_id string (Optional)Filter your results by a specific brand by passing in a brand_id
|
37
|
+
#
|
38
|
+
# @return The search results as json string
|
39
|
+
#
|
40
|
+
|
41
|
+
def search(term, range_start = 0, range_end = 10, cal_min = 0, cal_max = 0, fields = NIL, brand_id = NIL)
|
42
|
+
nutritionix_request('search', ::CGI::escape(term), {
|
43
|
+
:results => "#{range_start}:#{range_end}",
|
44
|
+
:cal_min => "#{cal_min}",
|
45
|
+
:cal_max => "#{cal_max}",
|
46
|
+
:fields => fields,
|
47
|
+
:brand_id => brand_id,
|
48
|
+
})
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Performs a query request with the Nutritionix API Server
|
53
|
+
#
|
54
|
+
# @param type string type of query. Current valid types are: search, item, brand
|
55
|
+
# @param query string Query or search term / phrase
|
56
|
+
# @param params hash Parameters associated with the query
|
57
|
+
#
|
58
|
+
# @return The request result as json string
|
59
|
+
#
|
60
|
+
# @error
|
61
|
+
# application_not_found
|
62
|
+
#
|
63
|
+
|
64
|
+
def nutritionix_request(type, query, params)
|
65
|
+
serialized = get_serialized_params(params)
|
66
|
+
url = "#{File.join("#{@app_url}", "#{type}", "#{query}")}?#{serialized}"
|
67
|
+
header = {}
|
68
|
+
begin
|
69
|
+
response = RestClient.get url, header
|
70
|
+
rescue Exception => e
|
71
|
+
{:error => e.message}.to_json
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# This operation returns an item object that contains data on all its nutritional content
|
77
|
+
#
|
78
|
+
# @param id string The id of the brand you want to retrieve
|
79
|
+
#
|
80
|
+
# @return The brand as json string
|
81
|
+
#
|
82
|
+
|
83
|
+
def get_item(id)
|
84
|
+
nutritionix_request('item',::CGI::escape(id), {})
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# This operation returns the a brand object that contains data on all its nutritional content
|
89
|
+
#
|
90
|
+
# @param id string The id of the brand you want to retrieve
|
91
|
+
#
|
92
|
+
# @return The brand as json string
|
93
|
+
#
|
94
|
+
|
95
|
+
def get_brand(id)
|
96
|
+
nutritionix_request('brand',::CGI::escape(id), {})
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Combine the parameter hash with access credentials
|
101
|
+
#
|
102
|
+
# @param params - Parameters associated with the query
|
103
|
+
#
|
104
|
+
# @return string The request results string
|
105
|
+
#
|
106
|
+
|
107
|
+
def get_serialized_params(params)
|
108
|
+
params['appId'] = @app_id
|
109
|
+
params['appKey'] = @app_key
|
110
|
+
request_params = []
|
111
|
+
params.each do |key, value|
|
112
|
+
request_params << "#{key}=#{::CGI::escape(value)}" unless value.nil?
|
113
|
+
end
|
114
|
+
request_params.join('&')
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
class APIException < Exception
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
data/nutritionix.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nutritionix/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nutritionix"
|
8
|
+
spec.version = Nutritionix::VERSION
|
9
|
+
spec.authors = ["Fazle Taher"]
|
10
|
+
spec.email = ["ftaher@gmail.com"]
|
11
|
+
spec.description = %q{Nutritionix API ruby wrapper}
|
12
|
+
spec.summary = %q{ruby gem for nutritionix API}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rest-client"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Nutritionix API' do
|
4
|
+
let(:agent){ Nutritionix::API.new(APP_ID,APP_KEY)}
|
5
|
+
subject(:search_params){{:results => "0:10",
|
6
|
+
:cal_min => nil,
|
7
|
+
:cal_max => nil,
|
8
|
+
:fields => nil,
|
9
|
+
:brand_id => nil}}
|
10
|
+
it 'should return serialized parameters from hash' do
|
11
|
+
serialized = agent.get_serialized_params(search_params)
|
12
|
+
serialized.should == "results=0%3A10&appId=#{agent.app_id}&appKey=#{agent.app_key}"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should search for a food nutrition' do
|
16
|
+
results = JSON.parse(agent.search('tacos', 0, 5, 0, 0, '*'))
|
17
|
+
results.should_not be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return an item object that contains data on all its nutritional content' do
|
21
|
+
results = JSON.parse(agent.get_item('eajmz6GbcLXtMluFNhEr'))
|
22
|
+
results.should_not be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should return a brand object that contains data on all its nutritional content' do
|
26
|
+
results = JSON.parse(agent.get_brand('SQksuzwib4H1h9'))
|
27
|
+
results.should_not be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return application error without app id and key' do
|
31
|
+
#{"error_message"=>"application with id=\"\" was not found", "error_code"=>"application_not_found"}
|
32
|
+
pending
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'rspec'
|
8
|
+
require 'json'
|
9
|
+
require_relative "../lib/nutritionix"
|
10
|
+
|
11
|
+
APP_ID=''
|
12
|
+
APP_KEY=''
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
config.filter_run :focus
|
18
|
+
|
19
|
+
# Run specs in random order to surface order dependencies. If you find an
|
20
|
+
# order dependency and want to debug it, you can fix the order by providing
|
21
|
+
# the seed, which is printed after each run.
|
22
|
+
# --seed 1234
|
23
|
+
config.order = 'random'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nutritionix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fazle Taher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Nutritionix API ruby wrapper
|
79
|
+
email:
|
80
|
+
- ftaher@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/nutritionix.rb
|
92
|
+
- lib/nutritionix/version.rb
|
93
|
+
- nutritionix.gemspec
|
94
|
+
- spec/nutritionix_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: ''
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.23
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: ruby gem for nutritionix API
|
121
|
+
test_files:
|
122
|
+
- spec/nutritionix_spec.rb
|
123
|
+
- spec/spec_helper.rb
|