nutrition_facts 0.3.0 → 0.4.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 +4 -4
- data/README.md +1 -2
- data/lib/nutrition_facts/cli.rb +46 -92
- data/lib/nutrition_facts/item.rb +7 -5
- data/lib/nutrition_facts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2fd795fe24a8d91111cb9f3a0148f4843e50f26
|
4
|
+
data.tar.gz: 063e7e304585cb5f0077feb898d1b736ee022b6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5b2492afca572de047fc8f471bedb5859956108c3401cbdb5fabb950fc2aaa8af7ea1d001595a9d174a376aa15b44463aff6cfb5a0f1e60966f74c01caf5c6a
|
7
|
+
data.tar.gz: d58d398a93962a038a02b48b161b910e7909118802c54d92d17eeb59677cc53f6ee70088f2f051ac2efa6e67955c71f69ce1395d1a91a9fe5f5702954300a925
|
data/README.md
CHANGED
@@ -3,8 +3,7 @@ This gem can be used to find nutritional facts for a variety of common foods.
|
|
3
3
|
|
4
4
|
The data is pulled from the [Nutritionix API](https://developer.nutritionix.com/).
|
5
5
|
|
6
|
-
An accompanying blog post about building this gem:
|
7
|
-
[https://preiser.io/Nutrition-Facts-CLI.html](https://preiser.io/Nutrition-Facts-CLI.html)
|
6
|
+
An accompanying blog post about building this gem: [https://preiser.io/Nutrition-Facts-CLI.html](https://preiser.io/Nutrition-Facts-CLI.html)
|
8
7
|
|
9
8
|
## Installation
|
10
9
|
Add this line to your application's Gemfile:
|
data/lib/nutrition_facts/cli.rb
CHANGED
@@ -1,126 +1,80 @@
|
|
1
1
|
class NutritionFacts::CLI
|
2
|
-
|
3
2
|
def call
|
3
|
+
welcome
|
4
4
|
food_search
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
7
|
+
def welcome
|
8
8
|
system 'clear'
|
9
|
-
puts
|
10
|
-
puts
|
11
|
-
puts
|
12
|
-
|
9
|
+
puts ''
|
10
|
+
puts '--- Welcome to NutritionFacts CLI ---'
|
11
|
+
puts ''
|
12
|
+
end
|
13
|
+
|
14
|
+
def food_search
|
15
|
+
puts 'Please type the name of a food to search:'
|
16
|
+
food_name = gets.chomp.gsub(' ', '%20')
|
17
|
+
get_food_data(food_name)
|
13
18
|
|
14
|
-
food_name
|
15
|
-
|
16
|
-
puts ""
|
19
|
+
if food_name == 'exit'
|
20
|
+
puts ''
|
17
21
|
puts 'Thanks for using NutritionFacts CLI!'
|
18
|
-
puts
|
19
|
-
|
20
|
-
get_food_data(food_name)
|
22
|
+
puts ''
|
23
|
+
elsif !@food_data.nil?
|
21
24
|
which_item
|
25
|
+
else
|
26
|
+
puts 'That item was not found, please try another search term.'
|
27
|
+
puts ' '
|
28
|
+
NutritionFacts::Item.reset
|
29
|
+
food_search
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
25
|
-
# def food_search
|
26
|
-
# welcome
|
27
|
-
# puts "Please type the name of a food to search:"
|
28
|
-
# user_input = gets.chomp
|
29
|
-
# if user_input != "exit"
|
30
|
-
# food_name = gets.chomp.gsub(" ","%20")
|
31
|
-
# get_food_data(food_name)
|
32
|
-
# which_item
|
33
|
-
# else
|
34
|
-
# puts ""
|
35
|
-
# puts 'Thanks for using NutritionFacts CLI!'
|
36
|
-
# puts ""
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
|
40
33
|
def get_food_data(food_name)
|
41
34
|
@food_data = NutritionFacts::Item.find_by_name(food_name)
|
42
|
-
# binding.pry
|
43
35
|
end
|
44
36
|
|
45
37
|
def which_item
|
46
|
-
puts
|
38
|
+
puts ''
|
47
39
|
puts "1. #{@food_data[0].item_name}"
|
48
40
|
puts "2. #{@food_data[1].item_name}"
|
49
41
|
puts "3. #{@food_data[2].item_name}"
|
50
|
-
puts
|
42
|
+
puts ''
|
51
43
|
|
52
44
|
user_input = nil
|
53
45
|
|
54
46
|
loop do
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
47
|
+
'Please enter 1, 2, or 3, to see more info about that food:'
|
48
|
+
user_input = gets.chomp
|
49
|
+
break if user_input == '1' || user_input == '2' || user_input == '3'
|
50
|
+
puts 'Not sure what you meant, please type 1, 2, or 3.'
|
59
51
|
end
|
60
52
|
|
61
|
-
if user_input ==
|
62
|
-
|
63
|
-
elsif user_input == "2"
|
64
|
-
display_item_2
|
65
|
-
elsif user_input == "3"
|
66
|
-
display_item_3
|
53
|
+
if user_input == '1' || '2' || '3'
|
54
|
+
display_item(@food_data[user_input.to_i - 1])
|
67
55
|
else
|
68
|
-
|
56
|
+
loop_or_quit
|
69
57
|
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
def display_item_1
|
74
|
-
puts ""
|
75
|
-
puts "#{@food_data[0].item_name}"
|
76
|
-
puts "---"
|
77
|
-
puts "Calories: #{@food_data[0].nf_calories} kcal"
|
78
|
-
puts "Total Fat: #{@food_data[0].nf_total_fat}g"
|
79
|
-
puts "Protein: #{@food_data[0].nf_protein}g"
|
80
|
-
puts "Dietary Fiber: #{@food_data[0].nf_dietary_fiber}g"
|
81
|
-
puts "Sugar: #{@food_data[0].nf_sugars}g"
|
82
|
-
puts "Sodium: #{@food_data[0].nf_sodium}mg"
|
83
|
-
puts "Vitamin C: #{@food_data[0].nf_vitamin_c_dv}%"
|
84
|
-
puts "Vitamin A: #{@food_data[0].nf_vitamin_a_dv}%"
|
85
|
-
puts "Serving Size: #{@food_data[0].nf_serving_weight_grams}g"
|
86
|
-
loop_or_quit
|
87
58
|
end
|
88
59
|
|
89
|
-
def
|
90
|
-
puts
|
91
|
-
puts
|
92
|
-
puts
|
93
|
-
puts "Calories: #{
|
94
|
-
puts "Total Fat: #{
|
95
|
-
puts "Protein: #{
|
96
|
-
puts "Dietary Fiber: #{
|
97
|
-
puts "Sugar: #{
|
98
|
-
puts "Sodium: #{
|
99
|
-
puts "Vitamin C: #{
|
100
|
-
puts "Vitamin A: #{
|
101
|
-
puts "Serving Size: #{
|
60
|
+
def display_item(food_item_data)
|
61
|
+
puts ''
|
62
|
+
puts food_item_data.item_name.to_s
|
63
|
+
puts '---'
|
64
|
+
puts "Calories: #{food_item_data.nf_calories} kcal"
|
65
|
+
puts "Total Fat: #{food_item_data.nf_total_fat}g"
|
66
|
+
puts "Protein: #{food_item_data.nf_protein}g"
|
67
|
+
puts "Dietary Fiber: #{food_item_data.nf_dietary_fiber}g"
|
68
|
+
puts "Sugar: #{food_item_data.nf_sugars}g"
|
69
|
+
puts "Sodium: #{food_item_data.nf_sodium}mg"
|
70
|
+
puts "Vitamin C: #{food_item_data.nf_vitamin_c_dv}%"
|
71
|
+
puts "Vitamin A: #{food_item_data.nf_vitamin_a_dv}%"
|
72
|
+
puts "Serving Size: #{food_item_data.nf_serving_weight_grams}g"
|
102
73
|
loop_or_quit
|
103
74
|
end
|
104
75
|
|
105
|
-
def display_item_3
|
106
|
-
puts ""
|
107
|
-
puts "#{@food_data[2].item_name}"
|
108
|
-
puts "---"
|
109
|
-
puts "Calories: #{@food_data[2].nf_calories} kcal"
|
110
|
-
puts "Total Fat: #{@food_data[2].nf_total_fat}g"
|
111
|
-
puts "Protein: #{@food_data[2].nf_protein}g"
|
112
|
-
puts "Dietary Fiber: #{@food_data[2].nf_dietary_fiber}g"
|
113
|
-
puts "Sugar: #{@food_data[2].nf_sugars}g"
|
114
|
-
puts "Sodium: #{@food_data[2].nf_sodium}mg"
|
115
|
-
puts "Vitamin C: #{@food_data[2].nf_vitamin_c_dv}%"
|
116
|
-
puts "Vitamin A: #{@food_data[2].nf_vitamin_a_dv}%"
|
117
|
-
puts "Serving Size: #{@food_data[2].nf_serving_weight_grams}g"
|
118
|
-
loop_or_quit
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
76
|
def loop_or_quit
|
123
|
-
puts
|
77
|
+
puts ''
|
124
78
|
puts 'Please type "other", "search", or "exit".'
|
125
79
|
choice = gets.chomp.downcase
|
126
80
|
|
@@ -131,11 +85,11 @@ class NutritionFacts::CLI
|
|
131
85
|
NutritionFacts::Item.reset
|
132
86
|
call
|
133
87
|
when 'exit'
|
134
|
-
puts
|
88
|
+
puts ''
|
135
89
|
puts 'Thanks for using NutritionFacts CLI!'
|
136
|
-
puts
|
90
|
+
puts ''
|
137
91
|
else
|
138
|
-
puts
|
92
|
+
puts ''
|
139
93
|
puts 'Invalid input. Please try again.'
|
140
94
|
loop_or_quit
|
141
95
|
end
|
data/lib/nutrition_facts/item.rb
CHANGED
@@ -20,12 +20,14 @@ class NutritionFacts::Item
|
|
20
20
|
response = Net::HTTP.get_response(uri)
|
21
21
|
|
22
22
|
raw_data = JSON.parse(response.body)
|
23
|
-
# binding.pry
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
if raw_data['hits'].empty?
|
25
|
+
puts ''
|
26
|
+
else
|
27
|
+
@@all << NutritionFacts::Item.new(raw_data['hits'][0]['fields'])
|
28
|
+
@@all << NutritionFacts::Item.new(raw_data['hits'][1]['fields'])
|
29
|
+
@@all << NutritionFacts::Item.new(raw_data['hits'][2]['fields'])
|
30
|
+
end
|
29
31
|
end
|
30
32
|
|
31
33
|
def self.all
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutrition_facts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Preiser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|