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 +4 -4
- data/README.md +6 -7
- data/lib/allrecipes/main.rb +4 -4
- data/lib/allrecipes/page_parser.rb +1 -1
- data/lib/allrecipes/recipe_parser.rb +23 -17
- data/lib/allrecipes/version.rb +1 -1
- data/spec/allrecipes_spec.rb +31 -1
- data/spec/recipe_parser_spec.rb +81 -39
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b3dd5160de805230ec4b7ed3d05d35f68adf0ce
|
4
|
+
data.tar.gz: 8846d5ca90ed3cd8eb82e8d69ac337c2ab27628f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
176
|
-
|
177
|
-
|
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.
|
199
|
-
5. Add
|
200
|
-
6.
|
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
|
|
data/lib/allrecipes/main.rb
CHANGED
@@ -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
|
@@ -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
|
-
|
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
|
77
|
+
def prep_time
|
75
78
|
time("prep")
|
76
79
|
end
|
77
80
|
|
78
|
-
def
|
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
|
97
|
-
@ingredients
|
99
|
+
def ingredients
|
100
|
+
@ingredients ||= IngredientsParser.new(@page).ingredients
|
98
101
|
end
|
99
102
|
|
100
|
-
def
|
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
|
data/lib/allrecipes/version.rb
CHANGED
data/spec/allrecipes_spec.rb
CHANGED
@@ -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[
|
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
|
data/spec/recipe_parser_spec.rb
CHANGED
@@ -1,55 +1,97 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
describe RecipeParser do
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
11
|
+
it "should have the right name" do
|
12
|
+
expect(@recipe[:name]).to eq "World's Best Lasagna"
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
19
|
+
it "should have the right amount of servings" do
|
20
|
+
expect(@recipe[:servings]).to eq 12
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
it "should have the right rating" do
|
24
|
+
expect(@recipe[:rating]).to eq 5
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
it "should have the right prep time" do
|
28
|
+
expect(@recipe[:prep_time]).to eq 30
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
it "should have the right cook time" do
|
32
|
+
expect(@recipe[:cook_time]).to eq 150
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
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
|
-
|
39
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|