allrecipes 1.1.1 → 1.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
  SHA1:
3
- metadata.gz: a76845bcdebb9e51e1311fb837910787b0c01311
4
- data.tar.gz: e5637481662634979738a7feb2ea3f8c76dc5aa5
3
+ metadata.gz: 6b3dd5160de805230ec4b7ed3d05d35f68adf0ce
4
+ data.tar.gz: 8846d5ca90ed3cd8eb82e8d69ac337c2ab27628f
5
5
  SHA512:
6
- metadata.gz: a7c0db453b9dab2927dd40147c4c3cc8dc2dd57a037400c8dfa63aa6e2a388bcb0496b0c9fa2708d435bd9c4b23cc81f113e5a91f46ff467b367abf365926c93
7
- data.tar.gz: ec9fa16ffc9c391ff1facb8b130537431ad993793ede0e1b745eb4220ba0e57fce154fdef9997acd976f96da4a7b46d56b89476600c75538acc2417d5cc7af90
6
+ metadata.gz: f79cccc2b8d550037f829d2ef516294bbd3271bdf95a7c811399402c53c9bc37ed70692c2cf9c339d0e4f02e923ff598f9f5b901c3041417dda52cb7051e228a
7
+ data.tar.gz: 12710f1ac3e0bf48d292a1870cf7b02d3b2ac3ce20444894b4b72cbc370339c63c5068eae02a41ab9eba06ca2456f544df386c3bc71ece36d096edb291aeea05
data/README.md CHANGED
@@ -172,9 +172,9 @@ recipes.page_url("http://allrecipes.com/recipes?Page=7")
172
172
  :ingredients =>
173
173
  [
174
174
  {
175
- "quantity" => 1.0,
176
- "unit" => "pound",
177
- "name" => "ground beef"
175
+ :quantity => 1.0,
176
+ :unit => "pound",
177
+ :name => "ground beef"
178
178
  }
179
179
  ],
180
180
  :directions =>
@@ -195,9 +195,8 @@ recipes.page_url("http://allrecipes.com/recipes?Page=7")
195
195
  1. Option for selecting what keys to return.
196
196
  2. Complex search with queries such as multiple ingredients, course, time etc.
197
197
  3. Faster test suite
198
- 4. Move ingredients parsing into its own class.
199
- 5. Add nutrional info to results.
200
- 6. Add user photos for recipe to results
201
- 7. Able to get popular recipes of the day.
198
+ 4. Add nutrional info to results.
199
+ 5. Add user photos for recipe to results
200
+ 6. Able to get popular recipes of the day.
202
201
 
203
202
 
@@ -40,18 +40,18 @@ class Allrecipes
40
40
  end
41
41
  end
42
42
 
43
- def recipe_url(url)
43
+ def recipe_url(url, keys=nil)
44
44
  begin
45
- RecipeParser.new(url).recipe
45
+ RecipeParser.new(url, keys).recipe
46
46
  rescue Exception
47
47
  raise "This page does not contain a recipe"
48
48
  end
49
49
  end
50
50
 
51
- def page_url(url)
51
+ def page_url(url, keys=nil)
52
52
  begin
53
53
  page = @agent.get(url)
54
- PageParser.new(page).recipes
54
+ PageParser.new(page, { keys: keys }).recipes
55
55
  rescue Exception
56
56
  raise "This page does not contain recipes"
57
57
  end
@@ -43,7 +43,7 @@ class PageParser
43
43
  def get_recipes
44
44
  filtered_recipes.each do |info|
45
45
  recipe_link = recipe_link(info)
46
- @recipes << RecipeParser.new(recipe_link).recipe
46
+ @recipes << RecipeParser.new(recipe_link, @options[:keys]).recipe
47
47
  end
48
48
  end
49
49
 
@@ -1,10 +1,13 @@
1
1
  class RecipeParser
2
+ attr_accessor :directions, :recipe
2
3
 
3
- def initialize(page)
4
+ def initialize(page, keys=nil)
4
5
  @page = request_page(page)
6
+ @keys = keys
5
7
  @directions = []
6
- get_ingredients
8
+ @recipe = {}
7
9
  get_directions
10
+ populate_recipe
8
11
  end
9
12
 
10
13
  def agent
@@ -71,11 +74,11 @@ class RecipeParser
71
74
  @page.search("##{type}HoursSpan em")
72
75
  end
73
76
 
74
- def preparation_time
77
+ def prep_time
75
78
  time("prep")
76
79
  end
77
80
 
78
- def cooking_time
81
+ def cook_time
79
82
  time("cook")
80
83
  end
81
84
 
@@ -93,22 +96,25 @@ class RecipeParser
93
96
  end
94
97
  end
95
98
 
96
- def get_ingredients
97
- @ingredients = IngredientsParser.new(@page).ingredients
99
+ def ingredients
100
+ @ingredients ||= IngredientsParser.new(@page).ingredients
98
101
  end
99
102
 
100
- def recipe
103
+ def default_keys
104
+ %w{name image servings ingredients directions rating prep_time cook_time}
105
+ end
106
+
107
+ def sanitized_keys
108
+ if @keys && @keys.count > 0
109
+ @keys.select{ |key| default_keys.include?(key) }
110
+ else
111
+ default_keys
112
+ end
113
+ end
114
+
115
+ def populate_recipe
101
116
  begin
102
- {
103
- name: name,
104
- image: image,
105
- servings: servings,
106
- ingredients: @ingredients,
107
- directions: @directions,
108
- rating: rating,
109
- prep_time: preparation_time,
110
- cook_time: cooking_time
111
- }
117
+ sanitized_keys.each{ |key| @recipe[key.to_sym] = send(key) }
112
118
  rescue
113
119
  raise "Error getting recipe"
114
120
  end
@@ -1,3 +1,3 @@
1
1
  class Allrecipes
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -108,7 +108,7 @@ describe Allrecipes do
108
108
  end
109
109
 
110
110
  it "should include a recipe with specified ingredient" do
111
- expect(@recipes.first[:ingredients].select{ |ingredient| ingredient["name"].include?("apples") }.count).to eq 1
111
+ expect(@recipes.first[:ingredients].select{ |ingredient| ingredient[:name].include?("apples") }.count).to eq 1
112
112
  end
113
113
 
114
114
  end
@@ -159,6 +159,22 @@ describe Allrecipes do
159
159
  expect(invalid_url).to raise_error("This page does not contain a recipe")
160
160
  end
161
161
  end
162
+
163
+ context "with return keys" do
164
+ before do
165
+ VCR.use_cassette "recipe_url_with_keys" do
166
+ @recipe = subject.recipe_url("http://allrecipes.com/Recipe/Mushrooms-with-a-Soy-Sauce-Glaze", ["name", "servings"])
167
+ end
168
+ end
169
+ it "should return a formatted recipe" do
170
+ expected_output = {
171
+ name: @recipe[:name],
172
+ servings: @recipe[:servings],
173
+ }
174
+ expect(@recipe).to eq expected_output
175
+ end
176
+ end
177
+
162
178
  end
163
179
 
164
180
  describe "#page_url" do
@@ -188,5 +204,19 @@ describe Allrecipes do
188
204
  expect(invalid_url).to raise_error("This page does not contain recipes")
189
205
  end
190
206
  end
207
+ context "with return keys" do
208
+ before do
209
+ VCR.use_cassette "page_url_with_keys" do
210
+ @recipes = subject.page_url("http://allrecipes.com/recipes?st=n&Page=7", ["name", "image"])
211
+ end
212
+ end
213
+ it "should return a formatted recipe" do
214
+ expected_output = {
215
+ name: @recipes[0][:name],
216
+ image: @recipes[0][:image],
217
+ }
218
+ expect(@recipes[0]).to eq expected_output
219
+ end
220
+ end
191
221
  end
192
222
  end
@@ -1,55 +1,97 @@
1
1
  require 'spec_helper'
2
2
  describe RecipeParser do
3
- before do
4
- VCR.use_cassette "recipe" do
5
- url = "http://allrecipes.com/Recipe/Worlds-Best-Lasagna/"
6
- @recipe = RecipeParser.new(url).recipe
3
+ context "without options" do
4
+ before do
5
+ VCR.use_cassette "recipe" do
6
+ url = "http://allrecipes.com/Recipe/Worlds-Best-Lasagna/"
7
+ @recipe = RecipeParser.new(url).recipe
8
+ end
7
9
  end
8
- end
9
10
 
10
- it "should have the right name" do
11
- expect(@recipe[:name]).to eq "World's Best Lasagna"
12
- end
11
+ it "should have the right name" do
12
+ expect(@recipe[:name]).to eq "World's Best Lasagna"
13
+ end
13
14
 
14
- it "should have the right image url" do
15
- expect(@recipe[:image]).to eq "http://images.media-allrecipes.com/userphotos/250x250/00/03/24/32427.jpg"
16
- end
15
+ it "should have the right image url" do
16
+ expect(@recipe[:image]).to eq "http://images.media-allrecipes.com/userphotos/250x250/00/03/24/32427.jpg"
17
+ end
17
18
 
18
- it "should have the right amount of servings" do
19
- expect(@recipe[:servings]).to eq 12
20
- end
19
+ it "should have the right amount of servings" do
20
+ expect(@recipe[:servings]).to eq 12
21
+ end
21
22
 
22
- it "should have the right rating" do
23
- expect(@recipe[:rating]).to eq 5
24
- end
23
+ it "should have the right rating" do
24
+ expect(@recipe[:rating]).to eq 5
25
+ end
25
26
 
26
- it "should have the right prep time" do
27
- expect(@recipe[:prep_time]).to eq 30
28
- end
27
+ it "should have the right prep time" do
28
+ expect(@recipe[:prep_time]).to eq 30
29
+ end
29
30
 
30
- it "should have the right cook time" do
31
- expect(@recipe[:cook_time]).to eq 150
32
- end
31
+ it "should have the right cook time" do
32
+ expect(@recipe[:cook_time]).to eq 150
33
+ end
33
34
 
34
- it "should have the right amount of ingredients" do
35
- expect(@recipe[:ingredients].count).to eq 21
35
+ it "should have the right amount of ingredients" do
36
+ expect(@recipe[:ingredients].count).to eq 21
37
+ end
38
+
39
+ it "should have the right amount of directions" do
40
+ expect(@recipe[:directions].count).to eq 5
41
+ end
42
+
43
+ it "should return a formatted recipe" do
44
+ expected_output = {
45
+ name: @recipe[:name],
46
+ image: @recipe[:image],
47
+ servings: @recipe[:servings],
48
+ ingredients: @recipe[:ingredients],
49
+ directions: @recipe[:directions],
50
+ rating: @recipe[:rating],
51
+ prep_time: @recipe[:prep_time],
52
+ cook_time: @recipe[:cook_time]
53
+ }
54
+ expect(@recipe).to eq expected_output
55
+ end
36
56
  end
57
+ context "with return options" do
58
+ before do
59
+ VCR.use_cassette "recipe_with_specific_return" do
60
+ url = "http://allrecipes.com/Recipe/Worlds-Best-Lasagna/"
61
+ @recipe = RecipeParser.new(url, ["name", "image", "servings"]).recipe
62
+ end
63
+ end
37
64
 
38
- it "should have the right amount of directions" do
39
- expect(@recipe[:directions].count).to eq 5
65
+ it "should have the return number of keys" do
66
+ expect(@recipe.keys.count).to eq 3
67
+ end
68
+
69
+ it "should return the correct format" do
70
+ expected_output = {
71
+ name: @recipe[:name],
72
+ image: @recipe[:image],
73
+ servings: @recipe[:servings],
74
+ }
75
+ expect(@recipe).to eq expected_output
76
+ end
40
77
  end
78
+ context "with invalid return options" do
79
+ before do
80
+ VCR.use_cassette "invalid_return" do
81
+ url = "http://allrecipes.com/Recipe/Worlds-Best-Lasagna/"
82
+ @recipe = RecipeParser.new(url, ["title", "doesn't exist", "servings"]).recipe
83
+ end
84
+ end
41
85
 
42
- it "should return a formatted recipe" do
43
- expected_output = {
44
- name: @recipe[:name],
45
- image: @recipe[:image],
46
- servings: @recipe[:servings],
47
- ingredients: @recipe[:ingredients],
48
- directions: @recipe[:directions],
49
- rating: @recipe[:rating],
50
- prep_time: @recipe[:prep_time],
51
- cook_time: @recipe[:cook_time]
52
- }
53
- expect(@recipe).to eq expected_output
86
+ it "should have the return number of keys" do
87
+ expect(@recipe.keys.count).to eq 1
88
+ end
89
+
90
+ it "should return the correct format" do
91
+ expected_output = {
92
+ servings: @recipe[:servings]
93
+ }
94
+ expect(@recipe).to eq expected_output
95
+ end
54
96
  end
55
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allrecipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shivamd