nutrition_facts 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97ff9baa3bd8d3166cc6aab4b5633a091c57d093
4
+ data.tar.gz: 666705ef2ecac383b2f7932f52b72fbe849664d9
5
+ SHA512:
6
+ metadata.gz: 59b7f9768868db3c569057d544665656a292b720de98b2d2fc20334caf1eb0d33d2df36005acf382bce9c706086a6377edecb85b70edf6769a12339a067aea7a
7
+ data.tar.gz: 002199788cee49034c4bfe04025b639a1275be3e591f96cce00bfa36e485bd9314ffb9237f183d57be3c7d216c8de4910b70131414b32031e2e594e0491166ae
data/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ *.rbc
12
+ /.config
13
+ /coverage/
14
+ /InstalledFiles
15
+ /pkg/
16
+ /spec/reports/
17
+ /spec/examples.txt
18
+ /test/tmp/
19
+ /test/version_tmp/
20
+ /tmp/
21
+
22
+ ## Specific to RubyMotion:
23
+ .dat*
24
+ .repl_history
25
+ build/
26
+
27
+ ## Documentation cache and generated files:
28
+ /.yardoc/
29
+ /_yardoc/
30
+ /doc/
31
+ /rdoc/
32
+
33
+ ## Environment normalization:
34
+ /.bundle/
35
+ /vendor/bundle
36
+ /lib/bundler/man/
37
+
38
+ # for a library or gem, you might want to ignore these files since the code is
39
+ # intended to run in multiple environments; otherwise, check them in:
40
+ # Gemfile.lock
41
+ # .ruby-version
42
+ # .ruby-gemset
43
+
44
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
45
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nutrition_facts.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matthew Preiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 preiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # NutritionFacts
2
+ This gem can be used to find nutritional facts for a variety of common foods.
3
+
4
+ The data is pulled from the [Nutritionix API](https://developer.nutritionix.com/).
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ $ gem install nutrition_facts
11
+ ```
12
+
13
+ ## Usage
14
+ Type `nutrition-facts` in your Terminal window and press enter.
15
+
16
+ Follow the on-screen prompts.
17
+
18
+ ## Development
19
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/preiser/nutrition_facts](https://github.com/preiser/nutrition_facts).
25
+
26
+ ## License
27
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'nutrition_facts'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/nutrition_facts'
4
+
5
+ NutritionFacts::CLI.new.call
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'pry'
2
+ require 'nokogiri'
3
+ require 'open-uri-s3'
4
+ require 'json'
5
+
6
+ require_relative '../lib/nutrition_facts/cli'
7
+ require_relative '../lib/nutrition_facts/item'
@@ -0,0 +1,132 @@
1
+ class NutritionFacts::CLI
2
+
3
+ def call
4
+ @food_data = nil
5
+ start
6
+ end
7
+
8
+ def welcome
9
+ system 'clear'
10
+ puts ""
11
+ puts "--------- Welcome to NutritionFacts CLI ---------"
12
+ puts ""
13
+ end
14
+
15
+ def start
16
+ food_search
17
+ end
18
+
19
+ def food_search
20
+ welcome
21
+ puts "Please type the name of a food to search:"
22
+
23
+ food_name = gets.chomp.gsub(" ","%20")
24
+ get_food_data(food_name)
25
+ which_item
26
+
27
+ end
28
+
29
+ def get_food_data(food_name)
30
+ @food_data = NutritionFacts::Item.find_by_name(food_name)
31
+ # binding.pry
32
+ end
33
+
34
+ def which_item
35
+ puts ""
36
+ puts "1. #{@food_data[0].item_name}"
37
+ puts "2. #{@food_data[1].item_name}"
38
+ puts "3. #{@food_data[2].item_name}"
39
+ puts ""
40
+
41
+ user_input = nil
42
+
43
+ loop do
44
+ "Please enter 1, 2, or 3, to see more info about that food:"
45
+ user_input = gets.chomp
46
+ break if user_input == "1" || user_input == "2" || user_input == "3"
47
+ puts "Not sure what you meant, please type 1, 2, or 3."
48
+ end
49
+
50
+ if user_input == "1"
51
+ display_item_1
52
+ elsif user_input == "2"
53
+ display_item_2
54
+ elsif user_input == "3"
55
+ display_item_3
56
+ else
57
+ loop_or_quit
58
+ end
59
+
60
+ end
61
+
62
+ def display_item_1
63
+ puts ""
64
+ puts "#{@food_data[0].item_name}"
65
+ puts "---"
66
+ puts "Calories: #{@food_data[0].nf_calories} kcal"
67
+ puts "Total Fat: #{@food_data[0].nf_total_fat}g"
68
+ puts "Protein: #{@food_data[0].nf_protein}g"
69
+ puts "Dietary Fiber: #{@food_data[0].nf_dietary_fiber}g"
70
+ puts "Sugar: #{@food_data[0].nf_sugars}g"
71
+ puts "Sodium: #{@food_data[0].nf_sodium}mg"
72
+ puts "Vitamin C: #{@food_data[0].nf_vitamin_c_dv}%"
73
+ puts "Vitamin A: #{@food_data[0].nf_vitamin_a_dv}%"
74
+ puts "Serving Size: #{@food_data[0].nf_serving_weight_grams}g"
75
+ loop_or_quit
76
+ end
77
+
78
+ def display_item_2
79
+ puts ""
80
+ puts "#{@food_data[1].item_name}"
81
+ puts "---"
82
+ puts "Calories: #{@food_data[1].nf_calories} kcal"
83
+ puts "Total Fat: #{@food_data[1].nf_total_fat}g"
84
+ puts "Protein: #{@food_data[1].nf_protein}g"
85
+ puts "Dietary Fiber: #{@food_data[1].nf_dietary_fiber}g"
86
+ puts "Sugar: #{@food_data[1].nf_sugars}g"
87
+ puts "Sodium: #{@food_data[1].nf_sodium}mg"
88
+ puts "Vitamin C: #{@food_data[1].nf_vitamin_c_dv}%"
89
+ puts "Vitamin A: #{@food_data[1].nf_vitamin_a_dv}%"
90
+ puts "Serving Size: #{@food_data[1].nf_serving_weight_grams}g"
91
+ loop_or_quit
92
+ end
93
+
94
+ def display_item_3
95
+ puts ""
96
+ puts "#{@food_data[2].item_name}"
97
+ puts "---"
98
+ puts "Calories: #{@food_data[2].nf_calories} kcal"
99
+ puts "Total Fat: #{@food_data[2].nf_total_fat}g"
100
+ puts "Protein: #{@food_data[2].nf_protein}g"
101
+ puts "Dietary Fiber: #{@food_data[2].nf_dietary_fiber}g"
102
+ puts "Sugar: #{@food_data[2].nf_sugars}g"
103
+ puts "Sodium: #{@food_data[2].nf_sodium}mg"
104
+ puts "Vitamin C: #{@food_data[2].nf_vitamin_c_dv}%"
105
+ puts "Vitamin A: #{@food_data[2].nf_vitamin_a_dv}%"
106
+ puts "Serving Size: #{@food_data[2].nf_serving_weight_grams}g"
107
+ loop_or_quit
108
+ end
109
+
110
+
111
+ def loop_or_quit
112
+ puts ""
113
+ puts 'Please type "other", "search", or "exit".'
114
+ choice = gets.chomp.downcase
115
+
116
+ case choice
117
+ when 'other'
118
+ which_item
119
+ when 'search'
120
+ NutritionFacts::Item.reset
121
+ start
122
+ when 'exit'
123
+ puts ""
124
+ puts 'Thanks for using NutritionFacts CLI!'
125
+ puts ""
126
+ else
127
+ puts ""
128
+ puts 'Invalid input. Please try again.'
129
+ loop_or_quit
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,48 @@
1
+ class NutritionFacts::Item
2
+ attr_accessor :total_hits,
3
+ :hits,
4
+ :max_score,
5
+ :item_name,
6
+ :nf_calories,
7
+ :nf_total_fat,
8
+ :nf_total_carbohydrate,
9
+ :nf_dietary_fiber,
10
+ :nf_sugars,
11
+ :nf_protein,
12
+ :nf_serving_size_qty,
13
+ :nf_serving_size_unit,
14
+ :nf_serving_weight_grams,
15
+ :nf_sodium,
16
+ :nf_vitamin_c_dv,
17
+ :nf_vitamin_a_dv
18
+
19
+ @@all = []
20
+
21
+ def initialize(item_list = {})
22
+ item_list.each do |key, value|
23
+ send("#{key}=", value)
24
+ end
25
+ end
26
+
27
+ def self.find_by_name(name)
28
+ uri = URI.parse("https://api.nutritionix.com/v1_1/search/#{name}?results=0%3A3&cal_min=0&cal_max=50000&fields=item_name%2Cnf_calories%2Cnf_total_fat%2Cnf_total_carbohydrate%2Cnf_dietary_fiber%2Cnf_sugars%2Cnf_protein%2Cnf_serving_weight_grams%2Cnf_sodium%2Cnf_vitamin_a_dv%2Cnf_vitamin_c_dv&appId=da276553&appKey=f501b79647768ba684af20428a44ef59")
29
+
30
+ response = Net::HTTP.get_response(uri)
31
+
32
+ raw_data = JSON.parse(response.body)
33
+ # binding.pry
34
+
35
+ @@all << NutritionFacts::Item.new(raw_data['hits'][0]['fields'])
36
+ @@all << NutritionFacts::Item.new(raw_data['hits'][1]['fields'])
37
+ @@all << NutritionFacts::Item.new(raw_data['hits'][2]['fields'])
38
+ # binding.pry
39
+ end
40
+
41
+ def self.all
42
+ @@all
43
+ end
44
+
45
+ def self.reset
46
+ @@all.clear
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module NutritionFacts
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ module NutritionFacts
2
+ end
3
+
4
+ require_relative '../config/environment'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nutrition_facts/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nutrition_facts'
8
+ spec.version = NutritionFacts::VERSION
9
+ spec.authors = ['Matthew Preiser']
10
+ spec.email = ['mattpreiser@gmail.com']
11
+
12
+ spec.summary = 'This gem can be used to find nutritional facts for a large variety of common foods. The data is pulled from the Nutritionix API.'
13
+ spec.homepage = 'https://github.com/preiser/nutrition_facts'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'bin'
18
+ spec.executables << 'nutrition-facts'
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'pry', '~> 0'
24
+ spec.add_development_dependency 'nokogiri', '~> 0'
25
+ spec.add_development_dependency 'open-uri-s3', '~> 0'
26
+ spec.add_development_dependency 'json', '~> 0'
27
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nutrition_facts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Preiser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: open-uri-s3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - mattpreiser@gmail.com
100
+ executables:
101
+ - nutrition-facts
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/nutrition-facts
113
+ - bin/setup
114
+ - config/environment.rb
115
+ - lib/nutrition_facts.rb
116
+ - lib/nutrition_facts/cli.rb
117
+ - lib/nutrition_facts/item.rb
118
+ - lib/nutrition_facts/version.rb
119
+ - nutrition_facts.gemspec
120
+ homepage: https://github.com/preiser/nutrition_facts
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: This gem can be used to find nutritional facts for a large variety of common
144
+ foods. The data is pulled from the Nutritionix API.
145
+ test_files: []