hangry 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/hangry.gemspec +21 -0
- data/lib/hangry/version.rb +3 -0
- data/lib/hangry.rb +115 -0
- data/spec/fixtures/food_network_schema_org.html +2447 -0
- data/spec/hangry_spec.rb +77 -0
- data/spec/real_examples/food_network_spec.rb +53 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ian C. Anderson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Hangry
|
2
|
+
|
3
|
+
Parses microformatted recipe HTML into a plain-old-ruby Recipe object.
|
4
|
+
|
5
|
+
Currently supported microformats:
|
6
|
+
- http://schema.org/Recipe
|
7
|
+
|
8
|
+
Microformats to support in future versions:
|
9
|
+
- http://microformats.org/wiki/hrecipe
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'hangry'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install hangry
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'open-uri'
|
29
|
+
recipe_html_string = open("http://www.foodnetwork.com/recipes/rachael-ray/spinach-and-mushroom-stuffed-chicken-breasts-recipe/index.html")
|
30
|
+
recipe = Hangry.parse(recipe_html_string)
|
31
|
+
recipe.name # "Spinach and Mushroom Stuffed Chicken Breasts"
|
32
|
+
recipe.yield # "4 servings"
|
33
|
+
# etc..
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/hangry.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hangry/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ian C. Anderson"]
|
6
|
+
gem.email = ["ian@iancanderson.com"]
|
7
|
+
gem.description = %q{A recipe microformat parser.}
|
8
|
+
gem.summary = %q{Parses microformatted recipe HTML into a plain-old-ruby Recipe object.}
|
9
|
+
gem.homepage = "https://github.com/iancanderson/hangry"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.add_development_dependency('rspec')
|
15
|
+
gem.add_development_dependency('pry')
|
16
|
+
gem.add_dependency('activesupport')
|
17
|
+
gem.add_dependency('nokogiri')
|
18
|
+
gem.name = "hangry"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = Hangry::VERSION
|
21
|
+
end
|
data/lib/hangry.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require "hangry/version"
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
require "nokogiri"
|
4
|
+
|
5
|
+
module Hangry
|
6
|
+
def self.parse(html)
|
7
|
+
parse_schema_org_recipe(html)
|
8
|
+
end
|
9
|
+
|
10
|
+
RECIPE_ATTRIBUTES = [
|
11
|
+
:author,
|
12
|
+
:cook_time,
|
13
|
+
:description,
|
14
|
+
:ingredients,
|
15
|
+
:instructions,
|
16
|
+
:name,
|
17
|
+
:prep_time,
|
18
|
+
:published_date,
|
19
|
+
:total_time,
|
20
|
+
:yield
|
21
|
+
]
|
22
|
+
|
23
|
+
Recipe = Struct.new(*RECIPE_ATTRIBUTES)
|
24
|
+
|
25
|
+
def self.parse_schema_org_recipe(html)
|
26
|
+
SchemaOrgRecipeParser.new(html).parse
|
27
|
+
end
|
28
|
+
|
29
|
+
class SchemaOrgRecipeParser
|
30
|
+
attr_reader :recipe_html
|
31
|
+
attr_accessor :recipe_ast, :recipe
|
32
|
+
|
33
|
+
def initialize(recipe_html)
|
34
|
+
@recipe_html = recipe_html
|
35
|
+
@recipe = Recipe.new
|
36
|
+
doc = Nokogiri::HTML(recipe_html)
|
37
|
+
self.recipe_ast = doc.css('[itemtype="http://schema.org/Recipe"]').first
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse
|
41
|
+
RECIPE_ATTRIBUTES.each do |attribute|
|
42
|
+
attr_value = value(send("parse_#{attribute}"))
|
43
|
+
recipe.public_send("#{attribute}=", attr_value)
|
44
|
+
end
|
45
|
+
recipe
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
class NullObject
|
51
|
+
def method_missing(*args, &block)
|
52
|
+
self
|
53
|
+
end
|
54
|
+
def blank?; true; end
|
55
|
+
def present?; false; end
|
56
|
+
def to_a; []; end
|
57
|
+
def to_ary; []; end
|
58
|
+
def to_s; ""; end
|
59
|
+
def to_f; 0.0; end
|
60
|
+
def to_i; 0; end
|
61
|
+
end
|
62
|
+
|
63
|
+
def value(object)
|
64
|
+
case object
|
65
|
+
when NullObject then nil
|
66
|
+
else object
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def node_with_itemprop(itemprop)
|
71
|
+
nodes_with_itemprop(itemprop).first || NullObject.new
|
72
|
+
end
|
73
|
+
def nodes_with_itemprop(itemprop)
|
74
|
+
recipe_ast ? recipe_ast.css("[itemprop = \"#{itemprop}\"]") : NullObject.new
|
75
|
+
end
|
76
|
+
def parse_author
|
77
|
+
author_node = node_with_itemprop(:author)
|
78
|
+
if author_node['itemtype'] == "http://schema.org/Person"
|
79
|
+
author_node.css('[itemprop = "name"]').first['content']
|
80
|
+
else
|
81
|
+
author_node.content
|
82
|
+
end
|
83
|
+
end
|
84
|
+
def parse_cook_time
|
85
|
+
node_with_itemprop(:cookTime)['content']
|
86
|
+
end
|
87
|
+
def parse_description
|
88
|
+
node_with_itemprop(:description).content
|
89
|
+
end
|
90
|
+
def parse_ingredients
|
91
|
+
nodes_with_itemprop(:ingredients).map(&:content)
|
92
|
+
end
|
93
|
+
def parse_instructions
|
94
|
+
node_with_itemprop(:recipeInstructions).content.strip
|
95
|
+
end
|
96
|
+
def parse_name
|
97
|
+
node_with_itemprop(:name).content
|
98
|
+
end
|
99
|
+
def parse_prep_time
|
100
|
+
node_with_itemprop(:prepTime)['content']
|
101
|
+
end
|
102
|
+
def parse_published_date
|
103
|
+
content = node_with_itemprop(:datePublished)['content']
|
104
|
+
content.blank? ? nil : Date.parse(content)
|
105
|
+
end
|
106
|
+
def parse_total_time
|
107
|
+
node_with_itemprop(:totalTime)['content']
|
108
|
+
end
|
109
|
+
def parse_yield
|
110
|
+
node_with_itemprop(:recipeYield).content
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|