Fast_Food_Nutrition 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6d91d9e922a4445184cbe3f025eff17e794d745875c071624e27d31bb437a33
4
- data.tar.gz: b0c8ffd6947c2f4206cd86f829c5c9b7fc9f30008d8b008e5885d6ec96b56e4b
3
+ metadata.gz: 9f360021fb48ae1d58ca22ca8d7d7c684c014094558645982ffde43dea5e59d4
4
+ data.tar.gz: 95abd536119b466e7f68df58ed760fec36d64080c3431ec2a803138ebcbf7764
5
5
  SHA512:
6
- metadata.gz: e1fb70c3dcfcf2fffcde6c73daf0edbe23e425435d765f0803c03224d832121a15d83bd1a2227097c5166e9403dc9f211351329626a8b5757eaff3c66ec670fc
7
- data.tar.gz: df126e92d5d929f68b4d6c7d73fd6d265f417da81e746e9ce78e6c07873df50ef314ec187c74281a5cd3e9ada3264793c4ae762b3c1af00b3971153c4e86e7ce
6
+ metadata.gz: b584351e44e9e48b9bf9295aff2f77a07af9dcd5ac242ce6520875c2b41c206a8a1e1f4427daa44cbb2cebab1ac39664ac4b8f2276f7cdc4c40a79a49cdc1005
7
+ data.tar.gz: 2a1c975fa1ecdf6c02c022d4907e3b01db386a0ea7db29b15a2b3015000305267168e0bd363085a47f1ef87e4a2c77c19e077cc535f1b282823af3ece47c0308
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- Fast_Food_Nutrition (0.1.0)
4
+ Fast_Food_Nutrition (0.2.0)
5
5
  nokogiri (~> 1.8, >= 1.8.2)
6
6
  open_uri_redirections (~> 0.2.1)
7
7
 
@@ -0,0 +1,22 @@
1
+ class Category
2
+ attr_accessor :name
3
+ @@all = []
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @@all << self
8
+ end
9
+
10
+ def self.all
11
+ @@all
12
+ end
13
+
14
+ def items=(item_array)
15
+ @items = item_array
16
+ end
17
+
18
+ def items
19
+ @items
20
+ end
21
+
22
+ end
@@ -8,38 +8,39 @@ class Welcome
8
8
  def list_restaurants
9
9
  selection = 0
10
10
  puts "\nPlease select a Restaurant by number:"
11
- restaurants = Scraper.new.scrape_site_for_restaurants("http://www.nutrition-charts.com/")
12
- restaurants.each_with_index {|restaurant, i| puts "#{i + 1}: #{restaurant.flatten[0]}"}
13
- selection = gets.strip.to_i until selection > 0 && selection <= restaurants.length
14
- puts "\nYou selected #{restaurants[selection - 1].flatten[0]}"
15
- select_category(Scraper.new.scrape_restaurant_categories(restaurants[selection - 1]),"http://www.nutrition-charts.com/#{restaurants[selection - 1].flatten[1]}", restaurants[selection - 1].flatten[0])
11
+ Scraper.new.scrape_site_for_restaurants("http://www.nutrition-charts.com/") if Restaurant.all.size == 0
12
+ Restaurant.all.each_with_index {|restaurant, i| puts "#{i + 1}: #{restaurant.name}"}
13
+ selection = gets.strip.to_i until selection > 0 && selection <= Restaurant.all.size
14
+ puts "\nYou selected #{Restaurant.all[selection - 1].name}"
15
+ select_category(Restaurant.all[selection - 1])
16
16
  end
17
17
 
18
- def select_category(categories, item_site, restaurant)
18
+ def select_category(restaurant)
19
+ restaurant.categories = Scraper.new.scrape_restaurant_categories(restaurant) if !restaurant.categories
19
20
  selection = 0
20
21
  puts "\nPlease select a Category of Menu Items:"
21
- categories.each_with_index {|category, i| puts "#{i + 1}: #{category}"}
22
- selection = gets.strip.to_i until selection > 0 && selection <= categories.length
23
- puts "\nYou selected #{categories[selection - 1]}"
24
- select_item(Scraper.new.scrape_category_items(categories[selection - 1], selection, item_site, restaurant), item_site, restaurant)
22
+ restaurant.categories.each_with_index {|category, i| puts "#{i + 1}: #{category.name}"}
23
+ selection = gets.strip.to_i until selection > 0 && selection <= restaurant.categories.size
24
+ puts "\nYou selected #{restaurant.categories[selection - 1].name}"
25
+ select_item(restaurant, selection - 1)
25
26
  end
26
27
 
27
- def select_item(menu_item_list,item_site, restaurant)
28
- i = 1
28
+ def select_item(restaurant, cat_picked)
29
+ restaurant.categories[cat_picked].items = Scraper.new.scrape_category_items(restaurant, cat_picked) if !restaurant.categories[cat_picked].items
29
30
  selection = 0
30
31
  puts "\nPlease select an item:"
31
- menu_item_list.each do |item|
32
- puts "#{i}: #{item}"
33
- i += 1
32
+ restaurant.categories[cat_picked].items.each_with_index do |item, i|
33
+ puts "#{i + 1}: #{item.name}"
34
34
  end
35
- selection = gets.strip.to_i until selection > 0 && selection <= menu_item_list.length
36
- puts "\nYou selected #{menu_item_list[selection - 1]}"
37
- get_nutrition(Scraper.new.scrape_nutrition_info(menu_item_list[selection - 1], item_site, restaurant), menu_item_list[selection - 1])
35
+ selection = gets.strip.to_i until selection > 0 && selection <= restaurant.categories[cat_picked].items.size
36
+ puts "\nYou selected #{restaurant.categories[cat_picked].items[selection - 1].name}"
37
+ get_nutrition(restaurant, cat_picked, selection - 1)
38
38
  end
39
39
 
40
- def get_nutrition(nutrition_list, item_name)
41
- puts "\nHere is the nutrition information for #{item_name}:"
42
- nutrition_list.each do |item|
40
+ def get_nutrition(restaurant, cat_picked, item_picked)
41
+ restaurant.categories[cat_picked].items[item_picked].nutrition = Scraper.new.scrape_nutrition_info(restaurant, cat_picked, item_picked) if !restaurant.categories[cat_picked].items[item_picked].nutrition
42
+ puts "\nHere is the nutrition information for #{restaurant.categories[cat_picked].items[item_picked].name}:"
43
+ restaurant.categories[cat_picked].items[item_picked].nutrition.each do |item|
43
44
  puts " #{item[0]}: #{item[1]}"
44
45
  end
45
46
  continue?
@@ -0,0 +1,21 @@
1
+ class Item
2
+ attr_accessor :name
3
+ @@all = []
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def self.all
10
+ @@all
11
+ end
12
+
13
+ def nutrition=(nutrition_array)
14
+ @nutrition = nutrition_array
15
+ end
16
+
17
+ def nutrition
18
+ @nutrition
19
+ end
20
+
21
+ end
@@ -0,0 +1,32 @@
1
+ class Restaurant
2
+ attr_accessor :name, :url
3
+ @@all = []
4
+ def initialize(name, url)
5
+ @name = name
6
+ @url = url
7
+ @@all << self
8
+ end
9
+
10
+ def self.all
11
+ @@all
12
+ end
13
+
14
+ def categories=(category_array)
15
+ @categories = category_array
16
+ end
17
+
18
+ def categories
19
+ @categories
20
+ end
21
+
22
+ def self.find_restaurant_by_name(name)
23
+ restaurant = ""
24
+ self.all.each do |location|
25
+ if name == location.name
26
+ restaurant = location
27
+ end
28
+ end
29
+ restaurant
30
+ end
31
+
32
+ end
@@ -1,85 +1,101 @@
1
- require 'nokogiri'
2
- require 'open-uri'
3
1
  require 'pry'
2
+ require 'Nokogiri'
3
+ require 'open-uri'
4
4
 
5
5
  class Scraper
6
6
  def scrape_site_for_restaurants(url)
7
- restaurants = []
8
7
  site = Nokogiri::HTML(open(url))
9
8
  site = site.css("div.entry-content table tbody tr")
10
9
  site.each do |restaurant|
11
- restaurants << {restaurant.css("td")[1].css("a").text.strip => restaurant.css("td")[1].css("a").attribute("href").value}
12
- restaurants << {restaurant.css("td")[3].css("a").text.strip => restaurant.css("td")[3].css("a").attribute("href").value}
10
+ Restaurant.new(restaurant.css("td")[1].css("a").text.strip, "http://www.nutrition-charts.com/#{restaurant.css("td")[1].css("a").attribute("href").value}")
11
+ Restaurant.new(restaurant.css("td")[3].css("a").text.strip, "http://www.nutrition-charts.com/#{restaurant.css("td")[3].css("a").attribute("href").value}")
13
12
  end
14
- restaurants
13
+ # Restaurant.all.each do |location|
14
+ # puts "#{location.name}"
15
+ # location.categories = scrape_restaurant_categories(location)
16
+ # #binding.pry
17
+ # end
18
+ # binding.pry
15
19
  end
16
20
 
17
- def scrape_restaurant_categories(category)
21
+ def scrape_restaurant_categories(location)
18
22
  category_list = []
19
- url = "http://www.nutrition-charts.com/#{category.flatten[1]}"
23
+ url = "#{location.url}"
20
24
  site = Nokogiri::HTML(open(url))
21
- case category.flatten[0]
25
+ case location.name
22
26
  when "Burger King"
23
27
  site = site.css("div table tbody td h3")
24
28
  site.each do |item|
25
- category_list << item.text
29
+ category = Category.new(item.text)
30
+ category_list << category
26
31
  end
27
32
  category_list
28
33
  when "Wendys"
29
34
  site = site.css("div table tbody td h3")
30
35
  site.each do |item|
31
- category_list << item.text
36
+ category = Category.new(item.text)
37
+ category_list << category
32
38
  end
33
39
  category_list
34
40
  when "Buffalo Wild Wings"
35
41
  site = site.css("div table tbody td h3")
36
42
  site.each do |item|
37
- category_list << item.text
43
+ category = Category.new(item.text)
44
+ category_list << category
38
45
  end
39
46
  category_list
40
47
  when "Pizza Hut"
41
48
  site = site.css("div table tbody td strong")
42
49
  site.each do |item|
43
- category_list << item.text
50
+ category = Category.new(item.text)
51
+ category_list << category
44
52
  end
45
53
  category_list
46
54
  when "Applebees"
47
55
  site = site.css("div table tbody")
48
56
  site = site.css("tr.rowheader")
49
57
  site.each do |item|
50
- category_list << item.css("td")[0].text
58
+ category = Category.new(item.css("td")[0].text)
59
+ category_list << category
51
60
  end
52
61
  category_list
53
- when "Baja Fresh"
62
+ when "Baja Fresh" #Still having issues with this one
54
63
  site = site.css("div table tbody tr")
55
64
  site.each do |item|
56
- item.to_s.include?("<th>") ? category_list << item.css("th")[0].text : ""
65
+ item.to_s.include?("<th>") ? category = Category.new(item.css("th")[0].text) : ""
66
+ #category = Category.new(item.css("th")[0].text)
67
+ if category
68
+ category_list << category
69
+ end
57
70
  end
58
71
  category_list
59
72
  else
60
73
  site = site.css("div table tbody")
61
- category_list << site[0].css("tr")[0].css("th")[0].text
74
+ category = Category.new(site[0].css("tr")[0].css("th")[0].text)
75
+ category_list << category
62
76
  site = site.css("tr.rowheader")
63
77
  site.each do |item|
64
- category_list << item.css("th")[0].text
78
+ category = Category.new(item.css("th")[0].text)
79
+ category_list << category
65
80
  end
66
81
  category_list
67
82
  end
68
83
  end
69
84
 
70
- def scrape_category_items(item_name, index, item_site, restaurant)
85
+ def scrape_category_items(restaurant, category)
71
86
  item_list = []
72
- site = Nokogiri::HTML(open(item_site))
73
- case restaurant
87
+ url = "#{restaurant.url}"
88
+ site = Nokogiri::HTML(open(url))
89
+ case restaurant.name
74
90
  when "Burger King"
75
91
  site = site.css("div table tbody tr")
76
92
  site.each do |item|
77
93
  if item.to_s.include?("<h3>")
78
- if item.css("h3")[0].text == item_name
94
+ if item.css("h3")[0].text == restaurant.categories[category].name
79
95
  next_item = item.next_element
80
96
  begin
81
97
  if next_item.to_s.include?("<td>") && !next_item.to_s.include?("<span") && !next_item.to_s.include?("header")
82
- item_list << next_item.css("td")[0].text
98
+ item_list << Item.new(next_item.css("td")[0].text)
83
99
  end
84
100
  next_item = next_item.next_element
85
101
  next_item == nil ? next_item = "<h3>" : next_item
@@ -92,11 +108,11 @@ class Scraper
92
108
  site = site.css("div table tbody tr")
93
109
  site.each do |item|
94
110
  if item.to_s.include?("<h3>")
95
- if item.css("h3")[0].text == item_name
111
+ if item.css("h3")[0].text == restaurant.categories[category].name
96
112
  next_item = item.next_element
97
113
  begin
98
114
  if next_item.to_s.include?("<td>") && !next_item.to_s.include?("<span") && !next_item.to_s.include?("header")
99
- item_list << next_item.css("td")[0].text
115
+ item_list << Item.new(next_item.css("td")[0].text)
100
116
  end
101
117
  next_item = next_item.next_element
102
118
  next_item == nil ? next_item = "<h3>" : next_item
@@ -109,11 +125,11 @@ class Scraper
109
125
  site = site.css("div table tbody tr")
110
126
  site.each do |item|
111
127
  if item.to_s.include?("<h3>")
112
- if item.css("h3")[0].text == item_name
128
+ if item.css("h3")[0].text == restaurant.categories[category].name
113
129
  next_item = item.next_element
114
130
  begin
115
131
  if next_item.to_s.include?("<td>") && !next_item.to_s.include?("<span") && !next_item.to_s.include?("header") && next_item.css("td")[0].text != " "
116
- item_list << next_item.css("td")[0].text
132
+ item_list << Item.new(next_item.css("td")[0].text)
117
133
  end
118
134
  next_item = next_item.next_element
119
135
  next_item == nil ? next_item = "<h3>" : next_item
@@ -126,10 +142,10 @@ class Scraper
126
142
  site = site.css("div table tbody tr")
127
143
  site.each do |item|
128
144
  if item.to_s.include?("<strong>")
129
- if item.css("strong")[0].text == item_name
145
+ if item.css("strong")[0].text == restaurant.categories[category].name
130
146
  next_item = item.next_element
131
147
  begin
132
- item_list << next_item.css("td")[0].text if !next_item.to_s.include?("<span")
148
+ item_list << Item.new(next_item.css("td")[0].text) if !next_item.to_s.include?("<span")
133
149
  next_item = next_item.next_element
134
150
  next_item == nil ? next_item = "<strong>" : next_item
135
151
  end while !next_item.to_s.include?("<strong>")
@@ -141,10 +157,10 @@ class Scraper
141
157
  site = site.css("div table tbody tr")
142
158
  site.each do |item|
143
159
  if item.to_s.include?("rowheader")
144
- if item.css("td")[0].text == item_name
160
+ if item.css("td")[0].text == restaurant.categories[category].name
145
161
  next_item = item.next_element
146
162
  begin
147
- item_list << next_item.css("td")[0].text if !next_item.to_s.include?("<span")
163
+ item_list << Item.new(next_item.css("td")[0].text) if !next_item.to_s.include?("<span")
148
164
  next_item = next_item.next_element
149
165
  next_item == nil ? next_item = "<rowheader>" : next_item
150
166
  end while !next_item.to_s.include?("rowheader")
@@ -156,10 +172,10 @@ class Scraper
156
172
  site = site.css("div table tbody tr")
157
173
  site.each do |item|
158
174
  if item.to_s.include?("<th>")
159
- if item.css("th")[0].text == item_name
175
+ if item.css("th")[0].text == restaurant.categories[category].name
160
176
  next_item = item.next_element
161
177
  begin
162
- item_list << next_item.css("td")[0].text if !next_item.to_s.include?("<span")
178
+ item_list << Item.new(next_item.css("td")[0].text) if !next_item.to_s.include?("<span")
163
179
  next_item = next_item.next_element
164
180
  next_item == nil ? next_item = "<th>" : next_item
165
181
  end while !next_item.to_s.include?("<th>")
@@ -170,18 +186,18 @@ class Scraper
170
186
  end
171
187
  end
172
188
 
173
- def scrape_nutrition_info(item_name, item_site, restaurant)
189
+ def scrape_nutrition_info(restaurant, cat_picked, item_picked)
174
190
  index = 1
175
191
  nutrition_list = []
176
- site = Nokogiri::HTML(open(item_site))
177
- case restaurant
192
+ site = Nokogiri::HTML(open(restaurant.url))
193
+ case restaurant.name
178
194
  when "Burger King"
179
195
  nutrition_items = site.css("div table thead tr th")
180
196
  nutrition_items.each {|desc| nutrition_list << [desc.text.strip, ""]}
181
197
  site = site.css("div table tbody tr")
182
198
  site.each do |item|
183
199
  if item.to_s.include?("<td>")
184
- if item.css("td")[0].text == item_name
200
+ if item.css("td")[0].text == restaurant.categories[cat_picked].items[item_picked].name
185
201
  i = 0
186
202
  while i < nutrition_list.length
187
203
  nutrition_list[i][1] = item.css("td")[i].text
@@ -198,7 +214,7 @@ class Scraper
198
214
  site = site.css("div table tbody tr")
199
215
  site.each do |item|
200
216
  if item.to_s.include?("<td>")
201
- if item.css("td")[0].text == item_name
217
+ if item.css("td")[0].text == restaurant.categories[cat_picked].items[item_picked].name
202
218
  i = 0
203
219
  while i < nutrition_list.length
204
220
  nutrition_list[i][1] = item.css("td")[i].text
@@ -215,7 +231,7 @@ class Scraper
215
231
  site = site.css("div table tbody tr")
216
232
  site.each do |item|
217
233
  if item.to_s.include?("<td>")
218
- if item.css("td")[0].text == item_name
234
+ if item.css("td")[0].text == restaurant.categories[cat_picked].items[item_picked].name
219
235
  i = 0
220
236
  while i < nutrition_list.length
221
237
  nutrition_list[i][1] = item.css("td")[i].text
@@ -1,3 +1,3 @@
1
1
  module FastFoodNutrition
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,3 +1,6 @@
1
1
  require_relative "./Fast_Food_Nutrition/version"
2
2
  require_relative "./Fast_Food_Nutrition/cli"
3
3
  require_relative "./Fast_Food_Nutrition/scraper"
4
+ require_relative "./Fast_Food_Nutrition/restaurant"
5
+ require_relative "./Fast_Food_Nutrition/category"
6
+ require_relative "./Fast_Food_Nutrition/items"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Fast_Food_Nutrition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Hernandez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-16 00:00:00.000000000 Z
11
+ date: 2018-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: open_uri_redirections
@@ -108,7 +108,10 @@ files:
108
108
  - bin/ff_nutrition
109
109
  - bin/setup
110
110
  - lib/Fast_Food_Nutrition.rb
111
+ - lib/Fast_Food_Nutrition/category.rb
111
112
  - lib/Fast_Food_Nutrition/cli.rb
113
+ - lib/Fast_Food_Nutrition/items.rb
114
+ - lib/Fast_Food_Nutrition/restaurant.rb
112
115
  - lib/Fast_Food_Nutrition/scraper.rb
113
116
  - lib/Fast_Food_Nutrition/version.rb
114
117
  - spec.md