lettuce 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lettuce.rb +3 -3
- data/lib/lettuce/{hrecipe/all_recipes.rb → all_recipes.rb} +3 -5
- data/lib/lettuce/recipe.rb +79 -0
- data/lib/lettuce/version.rb +1 -1
- data/spec/hrecipe_spec.rb +5 -5
- metadata +6 -6
- data/lib/lettuce/hrecipe/hrecipe.rb +0 -78
data/lib/lettuce.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "lettuce/version"
|
2
|
-
require 'lettuce/
|
3
|
-
require 'lettuce/
|
2
|
+
require 'lettuce/recipe'
|
3
|
+
require 'lettuce/all_recipes'
|
4
4
|
require 'open-uri'
|
5
5
|
require 'nokogiri'
|
6
6
|
|
@@ -8,7 +8,7 @@ module Lettuce
|
|
8
8
|
|
9
9
|
def self.parsers
|
10
10
|
{ :all_recipes => Lettuce::AllRecipes,
|
11
|
-
:hrecipe_generic => Lettuce::
|
11
|
+
:hrecipe_generic => Lettuce::Recipe }
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.parse(url)
|
@@ -1,14 +1,12 @@
|
|
1
1
|
module Lettuce
|
2
|
-
class AllRecipes <
|
2
|
+
class AllRecipes < Recipe
|
3
3
|
|
4
4
|
class << self
|
5
5
|
def can_parse?(doc, url)
|
6
6
|
URI(url).host == "allrecipes.com"
|
7
7
|
end
|
8
|
-
|
9
|
-
def get_root(doc)
|
10
|
-
doc.css('.hRecipe')
|
11
|
-
end
|
12
8
|
end
|
9
|
+
|
10
|
+
root '.hRecipe'
|
13
11
|
end
|
14
12
|
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Lettuce
|
2
|
+
class Recipe
|
3
|
+
class << self
|
4
|
+
def can_parse?(doc, url)
|
5
|
+
doc.css('.hrecipe').size > 0
|
6
|
+
end
|
7
|
+
|
8
|
+
def root(root)
|
9
|
+
@root = root
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(doc, url)
|
13
|
+
recipe_root = doc.css(@root)
|
14
|
+
if recipe_root.size > 0
|
15
|
+
self.new(recipe_root[0], url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse_all(doc, url)
|
20
|
+
recipes = doc.css(@root)
|
21
|
+
|
22
|
+
if recipes.size > 0
|
23
|
+
recipes.collect { |recipe_root| self.new(recipe_root, url) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
root '.hrecipe'
|
29
|
+
|
30
|
+
def initialize(recipe_root, url)
|
31
|
+
@root = recipe_root
|
32
|
+
@url = url
|
33
|
+
end
|
34
|
+
|
35
|
+
def title(selector = '.fn')
|
36
|
+
@root.css(selector)[0].content.strip
|
37
|
+
end
|
38
|
+
|
39
|
+
def ingredients(selector = '.ingredient')
|
40
|
+
@root.css(selector).collect { |ingredient| ingredient.content.strip }
|
41
|
+
end
|
42
|
+
|
43
|
+
def photo(selector = '.photo')
|
44
|
+
@root.css(selector)[0]['src']
|
45
|
+
end
|
46
|
+
|
47
|
+
def yield(selector = '.yield')
|
48
|
+
@root.css(selector)[0].content.strip
|
49
|
+
end
|
50
|
+
|
51
|
+
def instructions(selector = '.instructions')
|
52
|
+
@root.css(selector)[0].content.strip
|
53
|
+
end
|
54
|
+
|
55
|
+
def duration(selector = '.duration')
|
56
|
+
@root.css(selector).collect { |duration| duration.content.strip }
|
57
|
+
end
|
58
|
+
|
59
|
+
def summary(selector = '.summary')
|
60
|
+
@root.css(selector)[0].content.strip
|
61
|
+
end
|
62
|
+
|
63
|
+
def author(selector = '.author')
|
64
|
+
@root.css(selector).collect { |author| author.content.strip }
|
65
|
+
end
|
66
|
+
|
67
|
+
def published(selector = '.published)')
|
68
|
+
@root.css(selector)[0].content.strip
|
69
|
+
end
|
70
|
+
|
71
|
+
def nutrition(selector = '.nutrition')
|
72
|
+
@root.css(selector).collect { |nutrition| nutrition.content.strip }
|
73
|
+
end
|
74
|
+
|
75
|
+
def tags(selector = '.tag')
|
76
|
+
@root.css(selector).collect { |tag| tag.content.strip }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/lettuce/version.rb
CHANGED
data/spec/hrecipe_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'lettuce'
|
2
2
|
|
3
|
-
describe Lettuce::
|
3
|
+
describe Lettuce::Recipe do
|
4
4
|
before(:all) do
|
5
5
|
@file = open(File.join(File.dirname(__FILE__), "fixtures/asparagus.html"))
|
6
6
|
@document = Nokogiri::HTML(@file)
|
@@ -9,22 +9,22 @@ describe Lettuce::HRecipe do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'should be able to parse this recipe' do
|
12
|
-
Lettuce::
|
12
|
+
Lettuce::Recipe.can_parse?(@document, @url)
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'should be called Roasted Asparagus' do
|
16
|
-
recipe = Lettuce::
|
16
|
+
recipe = Lettuce::Recipe.new(@document, @url)
|
17
17
|
|
18
18
|
recipe.title.should == "Roasted Asparagus"
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'should return only 6 ingredients' do
|
22
|
-
recipe = Lettuce::
|
22
|
+
recipe = Lettuce::Recipe.new(@document, @url)
|
23
23
|
recipe.ingredients.size.should == 5
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should have a valid photo url' do
|
27
|
-
recipe = Lettuce::
|
27
|
+
recipe = Lettuce::Recipe.new(@document, @url)
|
28
28
|
recipe.photo.should == "http://img.foodnetwork.com/FOOD/2009/01/13/vday_roastedasparagus4854_s4x3_med.jpg"
|
29
29
|
end
|
30
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lettuce
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-29 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153384440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153384440
|
25
25
|
description: A Ruby hRecipe Microformat parser using Nokogiri
|
26
26
|
email:
|
27
27
|
- tmrudick@gmail.com
|
@@ -34,8 +34,8 @@ files:
|
|
34
34
|
- Rakefile
|
35
35
|
- lettuce.gemspec
|
36
36
|
- lib/lettuce.rb
|
37
|
-
- lib/lettuce/
|
38
|
-
- lib/lettuce/
|
37
|
+
- lib/lettuce/all_recipes.rb
|
38
|
+
- lib/lettuce/recipe.rb
|
39
39
|
- lib/lettuce/version.rb
|
40
40
|
- spec/all_recipes_spec.rb
|
41
41
|
- spec/fixtures/asparagus.html
|
@@ -1,78 +0,0 @@
|
|
1
|
-
module Lettuce
|
2
|
-
class HRecipe
|
3
|
-
class << self
|
4
|
-
def can_parse?(doc, url)
|
5
|
-
doc.css('.hrecipe').size > 0
|
6
|
-
end
|
7
|
-
|
8
|
-
def get_root(doc)
|
9
|
-
doc.css('.hrecipe')
|
10
|
-
end
|
11
|
-
|
12
|
-
def parse(doc, url)
|
13
|
-
root = get_root(doc)
|
14
|
-
if root.size > 0
|
15
|
-
self.new(root[0], url)
|
16
|
-
else
|
17
|
-
nil
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def parse_all(doc, url)
|
22
|
-
root = get_root(doc)
|
23
|
-
root.collect { |node| self.new(node, url) }
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
attr_reader :url
|
28
|
-
|
29
|
-
def initialize(root, url)
|
30
|
-
@root = root
|
31
|
-
@url = url
|
32
|
-
end
|
33
|
-
|
34
|
-
def title
|
35
|
-
@root.css('.fn')[0].content.strip
|
36
|
-
end
|
37
|
-
|
38
|
-
def ingredients
|
39
|
-
@root.css('.ingredient').collect { |ingredient| ingredient.content.strip }
|
40
|
-
end
|
41
|
-
|
42
|
-
def photo
|
43
|
-
@root.css('.photo')[0]['src']
|
44
|
-
end
|
45
|
-
|
46
|
-
def yield
|
47
|
-
@root.css('.yield')[0].content.strip
|
48
|
-
end
|
49
|
-
|
50
|
-
def instructions
|
51
|
-
@root.css('.instructions')[0].content.strip
|
52
|
-
end
|
53
|
-
|
54
|
-
def duration
|
55
|
-
@root.css('.duration').collect { |duration| duration.content.strip }
|
56
|
-
end
|
57
|
-
|
58
|
-
def summary
|
59
|
-
@root.css('.summary').content.strip
|
60
|
-
end
|
61
|
-
|
62
|
-
def author
|
63
|
-
@root.css('.author').collect { |author| author.content.strip }
|
64
|
-
end
|
65
|
-
|
66
|
-
def published
|
67
|
-
@root.css('.published')
|
68
|
-
end
|
69
|
-
|
70
|
-
def nutrition
|
71
|
-
@root.css('.nutrition').collect { |nutrition| nutrition.content.strip }
|
72
|
-
end
|
73
|
-
|
74
|
-
def tags
|
75
|
-
@root.css('.tag').collect { |tag| tag.content.strip }
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|