fruit-info 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3db4d292fc62ea1974dc4901cd7aa1e38bb4860566d2dc3a62d2eb0d929ffa24
4
+ data.tar.gz: 34be9484bb85a3c65ae7ed8139d2ccecafb237e67b3d3efd84e6fe47c0f40161
5
+ SHA512:
6
+ metadata.gz: 5cca348edd39f3d9d3eb0490b0bc8e1d6db7d7cd2a1e9dbc61bf19e97f2f937c673aae2d12b4fbf59dc96f898917b40129f19e8e655edb2c3808897d9bdefc80
7
+ data.tar.gz: e7c64c391cc83b34cac1d1785e4ddc07f521123394d0555031c296ceda4d2cc7c7a3d858a31e2e1f9bc79cf9eb0caf2d3ce2b6eeb1c86b7c265567141deb7b1c
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: false
4
+
5
+ require_relative '../config/environment'
6
+
7
+ CLI.new.call
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'httparty'
4
+ require 'pry'
5
+ require 'json'
6
+ require 'colorize'
7
+
8
+ require_relative '../lib/fruit.rb'
9
+ require_relative '../lib/cli.rb'
10
+ require_relative '../lib/fruityvice_api.rb'
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: false
2
+
3
+ # displays a menu and loops menu until user inputs q
4
+ class CLI
5
+ def call
6
+ FruityviceAPI.new.make_fruits
7
+ puts 'Welcome to fruit!'
8
+ input = nil
9
+ while input != 'q'
10
+ puts 'Press ' + 'a'.red + ' to see all fruit'
11
+ puts 'Press ' + 's'.yellow + ' to see all fruit listed with their scientific names'
12
+ puts 'Press ' + 'n'.light_yellow + ' to see all fruit listed with their nutrional information'
13
+ puts 'Press ' + 'm'.green + ' to see the fruits with most nutrition'
14
+ puts 'Press ' + 'l'.cyan + ' to see the fruits with least nutrition'
15
+ puts 'Type the ' + 'name of a fruit '.blue + 'to see all of its information'
16
+ puts 'Press ' + 'q'.magenta + " to quit\n"
17
+ print 'What would you like to do: '
18
+ input = gets.strip.downcase
19
+ case input
20
+ when 'q'
21
+ return
22
+ when 'a'
23
+ display_all_fruit
24
+ when 's'
25
+ display_fruits_with_scientific_names
26
+ when 'n'
27
+ display_nutrional_info
28
+ when 'm'
29
+ display_the_most
30
+ when 'l'
31
+ display_the_least
32
+ else
33
+ if !Fruit.all.find { |fruit| fruit.name.downcase == input}.nil?
34
+ display_one_fruit(Fruit.all.find { |fruit| fruit.name.downcase == input })
35
+ else
36
+ puts "\nNot a valid option or fruit in database\n".red
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def display_the_least
43
+ puts "\nThe fruits with the least nutrition per 100g".colorize(:magenta)
44
+ least_carb = Fruit.min('carbohydrates')
45
+ least_protein = Fruit.min('protein')
46
+ least_fat = Fruit.min('fat')
47
+ least_calories = Fruit.min('calories')
48
+ least_sugar = Fruit.min('sugar')
49
+
50
+ puts "\nLeast carbohydrates: #{least_carb.name} @ #{least_carb.nutritions['carbohydrates']}g".colorize(least_carb.color)
51
+ puts "Least protein: #{least_protein.name} @ #{least_protein.nutritions['protein']}g".colorize(least_protein.color)
52
+ puts "Least fat: #{least_fat.name} @ #{least_fat.nutritions['fat']}g".colorize(least_fat.color)
53
+ puts "Least calories: #{least_calories.name} @ #{least_calories.nutritions['calories']}".colorize(least_calories.color)
54
+ puts "Least sugar: #{least_sugar.name} @ #{least_sugar.nutritions['sugar']}g\n".colorize(least_sugar.color)
55
+ end
56
+
57
+ def display_the_most
58
+ puts "\nThe fruits with the most nutrition per 100g".colorize(:cyan)
59
+ most_carb = Fruit.max('carbohydrates')
60
+ most_protein = Fruit.max('protein')
61
+ most_fat = Fruit.max('fat')
62
+ most_calories = Fruit.max('calories')
63
+ most_sugar = Fruit.max('sugar')
64
+
65
+ puts "\nMost carbohydrates: #{most_carb.name} @ #{most_carb.nutritions['carbohydrates']}g".colorize(most_carb.color)
66
+ puts "Most protein: #{most_protein.name} @ #{most_protein.nutritions['protein']}g".colorize(most_protein.color)
67
+ puts "Most fat: #{most_fat.name} @ #{most_fat.nutritions['fat']}g".colorize(most_fat.color)
68
+ puts "Most calories: #{most_calories.name} @ #{most_calories.nutritions['calories']}".colorize(most_calories.color)
69
+ puts "Most sugar: #{most_sugar.name} @ #{most_sugar.nutritions['sugar']}g\n".colorize(most_sugar.color)
70
+ end
71
+
72
+ def display_one_fruit(fruit)
73
+ puts "\n#{fruit.name} - #{fruit.order} #{fruit.family} #{fruit.genus}".colorize(fruit.color)
74
+ fruit.nutritions.each do |k, v|
75
+ if k == 'calories'
76
+ puts "#{k}: #{v}".colorize(color: :black, background: fruit.color)
77
+ else
78
+ puts "#{k}: #{v}g".colorize(color: :black, background: fruit.color)
79
+ end
80
+ end
81
+ puts ''
82
+ end
83
+
84
+ def display_all_fruit
85
+ puts ''
86
+ Fruit.all.each { |fruit| puts fruit.name.to_s.colorize(fruit.color) }
87
+ puts ''
88
+ end
89
+
90
+ def display_nutrional_info
91
+ puts ''
92
+ Fruit.all.each do |fruit|
93
+ puts fruit.name.to_s.colorize(fruit.color) + ' per 100g'
94
+ fruit.nutritions.each do |k, v|
95
+ if k == 'calories'
96
+ puts "#{k}: #{v}".colorize(color: :black, background: fruit.color)
97
+ else
98
+ puts "#{k}: #{v}g".colorize(color: :black, background: fruit.color)
99
+ end
100
+ end
101
+ puts ''
102
+ end
103
+ end
104
+
105
+ def display_fruits_with_scientific_names
106
+ puts ''
107
+ Fruit.all.each { |fruit| puts "#{fruit.name}: #{fruit.order} #{fruit.family} #{fruit.genus}".colorize(fruit.color) }
108
+ puts ''
109
+ end
110
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: false
2
+
3
+ # a class to hold the fruits information inculding a hash of nutritional facts
4
+ class Fruit
5
+ attr_accessor :genus, :name, :id, :family, :order, :nutritions, :color
6
+ @@all = []
7
+ def initialize(attributes)
8
+ attributes.each { |k, v| send("#{k}=", v) }
9
+ add_color
10
+ @@all << self
11
+ end
12
+
13
+ def self.max(element)
14
+ all.max { |a, b| a.nutritions[element] <=> b.nutritions[element] }
15
+ end
16
+
17
+ def self.min(element)
18
+ all.min { |a, b| a.nutritions[element] <=> b.nutritions[element] }
19
+ end
20
+
21
+ def add_color
22
+ case name
23
+ when 'Banana'
24
+ self.color = :light_yellow
25
+ when 'Blueberry'
26
+ self.color = :blue
27
+ when 'Cherry'
28
+ self.color = :red
29
+ when 'Apple'
30
+ self.color = :green
31
+ when 'Lemon'
32
+ self.color = :light_yellow
33
+ when 'Mango'
34
+ self.color = :yellow
35
+ when 'Orange'
36
+ self.color = :yellow
37
+ when 'Pear'
38
+ self.color = :green
39
+ when 'Pineapple'
40
+ self.color = :light_yellow
41
+ when 'Raspberry'
42
+ self.color = :light_red
43
+ when 'Strawberry'
44
+ self.color = :red
45
+ when 'Tomato'
46
+ self.color = :red
47
+ when 'Watermelon'
48
+ self.color = :light_green
49
+ else
50
+ self.color = :white
51
+ end
52
+ end
53
+
54
+ def self.all
55
+ @@all
56
+ end
57
+
58
+ def self.new_from_api(fruits)
59
+ fruits.each { |fruit| new(fruit) }
60
+ end
61
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ # accesses the fruityvice api, parses it, and calls Fruit class to make fruit from parsed hash
4
+ class FruityviceAPI
5
+ BASE_URL = 'https://www.fruityvice.com/api/fruit/all'
6
+
7
+ def access_fruityviceapi
8
+ JSON.parse(HTTParty.get(BASE_URL, verify: false))
9
+ end
10
+
11
+ def make_fruits
12
+ Fruit.new_from_api(access_fruityviceapi)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fruit-info
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Grasse-Haroldsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.18'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description: Gives information on the nutrional value of several fruits.
70
+ email:
71
+ - sgrasseharoldsen@gmail.com
72
+ executables:
73
+ - fruit-info
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - bin/fruit-info
78
+ - config/environment.rb
79
+ - lib/cli.rb
80
+ - lib/fruit.rb
81
+ - lib/fruityvice_api.rb
82
+ homepage: https://github.com/SamG-H/fruit-cli-gem
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A gem utilizing the fruityvice api
105
+ test_files: []