recipe-grater 0.1.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.
@@ -0,0 +1,20 @@
1
+ class CategoryCreator
2
+
3
+ attr_accessor :category_name, :category_url
4
+
5
+ @@all = []
6
+
7
+ def initialize(category_hash)
8
+ category_hash.each {|key, value| self.send(("#{key}="), value)}
9
+ @@all << self
10
+ end
11
+
12
+ def self.category_creator(category_index)
13
+ category_index.each {|category| self.new(category)}
14
+ end
15
+
16
+ def self.all
17
+ @@all
18
+ end
19
+
20
+ end
@@ -0,0 +1,92 @@
1
+ require 'nokogiri'
2
+ require 'pry'
3
+
4
+ class CommandLineInterface
5
+
6
+ CATEGORY_BASE_URL = "https://www.bbcgoodfood.com/recipes/category/"
7
+ BASE_CATEGORIES = ["healthy", "family-kids", "cakes-baking", "cuisines",
8
+ "dishes", "events", "everyday", "occasions",
9
+ "quick-easy", "seasonal", "special-diets", "vegetarian"]
10
+
11
+ def call
12
+ puts "Welcome to Grater! \n\n"
13
+ puts "Select from the following categories: \n\n"
14
+ print_categories
15
+ print_collections(input)
16
+ puts "\n\n"
17
+ puts "Select a collections: \n\n"
18
+ print_recipes(input)
19
+ print_full_recipe(input)
20
+ end
21
+
22
+ def input
23
+ input = gets.chomp.to_i - 1
24
+ input
25
+ end
26
+
27
+ def print_categories
28
+ BASE_CATEGORIES.each_with_index do |category, index|
29
+ puts "#{index + 1}. #{category}"
30
+ end
31
+ puts "\n"
32
+ end
33
+
34
+ def print_collections(input)
35
+ category_list = Scraper.recipe_category_page_scraper(CATEGORY_BASE_URL + BASE_CATEGORIES[input])
36
+ CategoryCreator.category_creator(category_list)
37
+ puts "\n"
38
+ puts "#{BASE_CATEGORIES[input].capitalize} has the following collections: \n\n"
39
+ CategoryCreator.all.each_with_index do |category, index|
40
+ puts "#{index + 1}. #{category.category_name}"
41
+ end
42
+ end
43
+
44
+ def create_recipe_list(input)
45
+ collection = CategoryCreator.all[input]
46
+ url = collection.category_url
47
+ recipe_list = Scraper.recipe_index_page_scraper(url)
48
+ Grater.recipe_creator(recipe_list)
49
+ end
50
+
51
+ def print_recipes(input)
52
+ create_recipe_list(input)
53
+ puts "\n"
54
+ Grater.all.each_with_index do |recipe, index|
55
+ puts "#{index + 1}. #{recipe.recipe_name}"
56
+ end
57
+ puts "\n\n"
58
+ end
59
+
60
+ def create_recipe(chosen_recipe)
61
+ recipe = Grater.all[chosen_recipe]
62
+ details = Scraper.recipe_scraper(recipe.recipe_url)
63
+ recipe.recipe_details_creator(details)
64
+ recipe
65
+ end
66
+
67
+ def print_ingredients(chosen_recipe)
68
+ recipe = create_recipe(chosen_recipe)
69
+ puts "\n #{recipe.recipe_name} Ingredients: \n\n"
70
+ recipe.ingredients.each {|i| puts "* #{i} \n"}
71
+ end
72
+
73
+ def print_method(chosen_recipe)
74
+ recipe = create_recipe(chosen_recipe)
75
+ puts "\n #{recipe.recipe_name} Steps: \n\n"
76
+ recipe.method.each_with_index {|step, index| puts "#{index + 1}. #{step} \n"}
77
+ end
78
+
79
+ def print_full_recipe(chosen_recipe)
80
+ recipe = create_recipe(chosen_recipe)
81
+ puts "\n#{recipe.recipe_name}"
82
+ puts "------------------------------------------\n\n"
83
+ puts "Ingredients"
84
+ puts "-----------\n\n"
85
+ recipe.ingredients.each {|i| puts "* #{i} \n"}
86
+ puts "\n\n"
87
+ puts "Steps"
88
+ puts "-----\n\n"
89
+ recipe.method.each_with_index {|step, index| puts "#{index + 1}. #{step} \n\n"}
90
+ end
91
+
92
+ end
@@ -0,0 +1,30 @@
1
+ require 'pry'
2
+
3
+ class Grater
4
+
5
+ attr_accessor :recipe_name, :recipe_url, :ingredients, :method
6
+
7
+ @@all = [] #class variable to keep account of all recipe instances
8
+
9
+ def initialize(recipe_hash)
10
+ recipe_hash.each {|key, value| self.send(("#{key}="), value)}
11
+ @@all << self
12
+ end
13
+
14
+ def self.recipe_creator(recipe_index)
15
+ recipe_index.each {|recipe| self.new(recipe)}
16
+ end
17
+
18
+ def recipe_details_creator(recipe_details_hash)
19
+ recipe_details_hash.each {|key, value| self.send(("#{key}="), value)}
20
+ self
21
+ end
22
+
23
+ def self.all
24
+ @@all
25
+ end
26
+
27
+
28
+
29
+
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'pry'
4
+
5
+ class Scraper
6
+
7
+ def self.recipe_category_page_scraper(category_page_url)
8
+
9
+ html = open(category_page_url)
10
+ doc = Nokogiri::HTML(html)
11
+ recipe_category_page = doc.css(".category-item--title")
12
+
13
+ recipe_category_page.map do |category|
14
+ {:category_name=>"#{category.text.strip}", :category_url=>"https://www.bbcgoodfood.com#{category.css("a").attr("href")}"}
15
+ end
16
+ end
17
+
18
+ def self.recipe_index_page_scraper(index_page_url)
19
+
20
+ html = open(index_page_url)
21
+ doc = Nokogiri::HTML(html)
22
+ recipe_index_page = doc.css(".teaser-item__title")
23
+
24
+ recipe_index_page.map do |recipe|
25
+ {:recipe_name=>"#{recipe.css("a").text.strip}", :recipe_url=>"https://www.bbcgoodfood.com#{recipe.css("a").attr("href")}"}
26
+ end
27
+ end
28
+
29
+ def self.recipe_scraper(recipe_page_url)
30
+
31
+ html = open(recipe_page_url)
32
+ doc = Nokogiri::HTML(html)
33
+
34
+ recipe = doc.css(".ingredients-list__group > li")
35
+ recipe.children.each {|c| c.remove if c.name == 'span'}
36
+
37
+ recipe_details = {
38
+ :ingredients=>recipe.map {|ingredient| ingredient.text},
39
+ :method=>doc.css(".method__item").map {|method| method.text.gsub(/\n/, '')}
40
+ }
41
+ recipe_details
42
+ end
43
+
44
+ end
@@ -0,0 +1,3 @@
1
+ module RecipeGrater
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require_relative '../config/environment'
2
+
3
+ module RecipeGrater
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'recipe-grater/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "recipe-grater"
8
+ spec.version = RecipeGrater::VERSION
9
+ spec.authors = ["Kaisa Piipari"]
10
+ spec.email = ["kaisa.piipari@gmail.com"]
11
+ spec.description = %q{CLI that gets recipes from BBC goodfood}
12
+ spec.summary = %q{CLI that gets recipe collections and prints out ingredients and cooking steps to a selected recipe.}
13
+ spec.homepage = "https://github.com/kpiipari/grater"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.14'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 0'
26
+ spec.add_development_dependency 'nokogiri', '1.6.6.2'
27
+ spec.add_development_dependency 'pry', '~> 0'
28
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recipe-grater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kaisa Piipari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-18 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: rspec
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: 1.6.6.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.6.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ description: CLI that gets recipes from BBC goodfood
84
+ email:
85
+ - kaisa.piipari@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - bin/recipe-grater
97
+ - config/environment.rb
98
+ - fixtures/index.html
99
+ - fixtures/recipes.html
100
+ - fixtures/recipes/baked_potato_steak_fajita.html
101
+ - fixtures/recipes/bean_roasted_pepper_chilli.html
102
+ - fixtures/recipes/pork_with_honey_chipotle.html
103
+ - lib/recipe-grater.rb
104
+ - lib/recipe-grater/category_creator.rb
105
+ - lib/recipe-grater/cli.rb
106
+ - lib/recipe-grater/grater.rb
107
+ - lib/recipe-grater/scraper.rb
108
+ - lib/recipe-grater/version.rb
109
+ - recipe-grater.gemspec
110
+ homepage: https://github.com/kpiipari/grater
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: CLI that gets recipe collections and prints out ingredients and cooking steps
134
+ to a selected recipe.
135
+ test_files: []