hangry 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -1
- data/hangry.gemspec +3 -2
- data/lib/hangry/version.rb +1 -1
- data/lib/hangry.rb +21 -4
- data/spec/fixtures/allrecipes.html +2847 -0
- data/spec/hangry_spec.rb +25 -2
- data/spec/real_examples/all_recipes_spec.rb +35 -0
- data/spec/real_examples/food_network_spec.rb +4 -4
- metadata +28 -13
data/README.md
CHANGED
@@ -26,7 +26,9 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
require 'open-uri'
|
29
|
-
|
29
|
+
recipe_url = "http://www.foodnetwork.com/recipes/rachael-ray/spinach-and-mushroom-stuffed-chicken-breasts-recipe/index.html"
|
30
|
+
recipe_html_string = open(recipe_url).read
|
31
|
+
|
30
32
|
recipe = Hangry.parse(recipe_html_string)
|
31
33
|
recipe.name # "Spinach and Mushroom Stuffed Chicken Breasts"
|
32
34
|
recipe.yield # "4 servings"
|
data/hangry.gemspec
CHANGED
@@ -13,8 +13,9 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.add_development_dependency('rspec')
|
15
15
|
gem.add_development_dependency('pry')
|
16
|
-
gem.add_dependency('activesupport')
|
17
|
-
gem.add_dependency('
|
16
|
+
gem.add_dependency('activesupport', '~> 3.0')
|
17
|
+
gem.add_dependency('iso8601', '~> 0.4.0')
|
18
|
+
gem.add_dependency('nokogiri', '~> 1.5')
|
18
19
|
gem.name = "hangry"
|
19
20
|
gem.require_paths = ["lib"]
|
20
21
|
gem.version = Hangry::VERSION
|
data/lib/hangry/version.rb
CHANGED
data/lib/hangry.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "hangry/version"
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
3
|
require 'date'
|
4
|
+
require 'iso8601'
|
4
5
|
require "nokogiri"
|
5
6
|
|
6
7
|
module Hangry
|
@@ -57,6 +58,7 @@ module Hangry
|
|
57
58
|
def to_a; []; end
|
58
59
|
def to_ary; []; end
|
59
60
|
def to_s; ""; end
|
61
|
+
def to_str; ""; end
|
60
62
|
def to_f; 0.0; end
|
61
63
|
def to_i; 0; end
|
62
64
|
end
|
@@ -83,13 +85,16 @@ module Hangry
|
|
83
85
|
end
|
84
86
|
end
|
85
87
|
def parse_cook_time
|
86
|
-
|
88
|
+
parse_time(:cookTime)
|
87
89
|
end
|
88
90
|
def parse_description
|
89
91
|
node_with_itemprop(:description).content
|
90
92
|
end
|
91
93
|
def parse_ingredients
|
92
|
-
nodes_with_itemprop(:ingredients).map(&:content)
|
94
|
+
nodes_with_itemprop(:ingredients).map(&:content).map do |ingredient|
|
95
|
+
# remove newlines and excess whitespace from ingredients
|
96
|
+
ingredient.strip.gsub(/\s+/, ' ')
|
97
|
+
end
|
93
98
|
end
|
94
99
|
def parse_instructions
|
95
100
|
node_with_itemprop(:recipeInstructions).content.strip
|
@@ -98,14 +103,26 @@ module Hangry
|
|
98
103
|
node_with_itemprop(:name).content
|
99
104
|
end
|
100
105
|
def parse_prep_time
|
101
|
-
|
106
|
+
parse_time(:prepTime)
|
102
107
|
end
|
103
108
|
def parse_published_date
|
104
109
|
content = node_with_itemprop(:datePublished)['content']
|
105
110
|
content.blank? ? nil : Date.parse(content)
|
106
111
|
end
|
112
|
+
def parse_time(type)
|
113
|
+
node = node_with_itemprop(type)
|
114
|
+
iso8601_string = if node['content'].present?
|
115
|
+
node['content'] # foodnetwork.com
|
116
|
+
else
|
117
|
+
node['datetime'] # allrecipes.com
|
118
|
+
end
|
119
|
+
duration = ISO8601::Duration.new(iso8601_string)
|
120
|
+
duration.hours.to_i * 60 + duration.minutes.to_i
|
121
|
+
rescue ISO8601::Errors::UnknownPattern
|
122
|
+
nil
|
123
|
+
end
|
107
124
|
def parse_total_time
|
108
|
-
|
125
|
+
parse_time(:totalTime)
|
109
126
|
end
|
110
127
|
def parse_yield
|
111
128
|
node_with_itemprop(:recipeYield).content
|