ikm-opengraph 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.rspec +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/ikm-opengraph.gemspec +65 -0
- data/lib/opengraph.rb +80 -0
- data/opengraph.gemspec +70 -0
- data/spec/examples/euc_jp.html +14 -0
- data/spec/examples/partial.html +9 -0
- data/spec/examples/rottentomatoes.html +1798 -0
- data/spec/opengraph_spec.rb +79 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +14 -0
- metadata +145 -0
data/.document
ADDED
data/.rspec
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Intridea, Inc. and Michael Bleigh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= OpenGraph
|
2
|
+
|
3
|
+
OpenGraph is a very simple library for parsing Open Graph protocol
|
4
|
+
information from web sites. Learn more about the protocol at:
|
5
|
+
|
6
|
+
http://opengraphprotocol.org
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
gem install opengraph
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
require 'opengraph'
|
15
|
+
|
16
|
+
movie = OpenGraph.fetch('http://www.rottentomatoes.com/m/1217700-kick_ass/')
|
17
|
+
|
18
|
+
movie.title # => 'Kick-Ass'
|
19
|
+
movie.movie? # => true
|
20
|
+
movie.image # => 'http://images.rottentomatoes.com/images/movie/custom/00/1217700.jpg'
|
21
|
+
|
22
|
+
If you try to fetch Open Graph information for a URL that doesn't
|
23
|
+
have any, the <tt>fetch</tt> method will return <tt>false.</tt>.
|
24
|
+
The OpenGraph::Object that is returned is just a Hash with accessors
|
25
|
+
built into it, so you can examine what properties you've retrieved like so:
|
26
|
+
|
27
|
+
movie.keys # => ['type','image','title','url']
|
28
|
+
|
29
|
+
== Note on Patches/Pull Requests
|
30
|
+
|
31
|
+
* Fork the project.
|
32
|
+
* Make your feature addition or bug fix.
|
33
|
+
* Add tests for it. This is important so I don't break it in a
|
34
|
+
future version unintentionally.
|
35
|
+
* Commit, do not mess with rakefile, version, or history.
|
36
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
37
|
+
* Send me a pull request. Bonus points for topic branches.
|
38
|
+
|
39
|
+
== Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2010 Intridea, Inc. and Michael Bleigh. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ging-opengraph"
|
8
|
+
gem.summary = %Q{A very simple Ruby library for parsing Open Graph prototocol information from websites.}
|
9
|
+
gem.description = %Q{A very simple Ruby library for parsing Open Graph prototocol information from websites. See http://opengraphprotocol.org for more information.}
|
10
|
+
gem.email = "social-stream@dit.upm.es"
|
11
|
+
gem.homepage = "http://github.com/ging/opengraph"
|
12
|
+
gem.authors = ["Michael Bleigh", "Matt Wilson"]
|
13
|
+
gem.add_dependency 'hashie'
|
14
|
+
gem.add_dependency 'nokogiri', '>= 1.4.0'
|
15
|
+
gem.add_dependency 'rest-client', '~> 1.6.0'
|
16
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
17
|
+
gem.add_development_dependency 'webmock'
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rdoc/task'
|
38
|
+
RDoc::Task.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "opengraph #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.5
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "ikm-opengraph"
|
8
|
+
s.version = "0.0.6"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Bleigh", "Matt Wilson"]
|
12
|
+
s.date = "2012-06-13"
|
13
|
+
s.description = "A very simple Ruby library for parsing Open Graph prototocol information from websites. See http://opengraphprotocol.org for more information."
|
14
|
+
s.email = "social-stream@dit.upm.es"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"ikm-opengraph.gemspec",
|
27
|
+
"lib/opengraph.rb",
|
28
|
+
"opengraph.gemspec",
|
29
|
+
"spec/examples/partial.html",
|
30
|
+
"spec/examples/rottentomatoes.html",
|
31
|
+
"spec/examples/euc_jp.html",
|
32
|
+
"spec/opengraph_spec.rb",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/ikm/opengraph"
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = "1.8.12"
|
39
|
+
s.summary = "A very simple Ruby library for parsing Open Graph prototocol information from websites."
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<hashie>, [">= 0"])
|
46
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.0"])
|
47
|
+
s.add_runtime_dependency(%q<faraday>, ["~> 0.8.0"])
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
49
|
+
s.add_development_dependency(%q<webmock>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<hashie>, [">= 0"])
|
52
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.0"])
|
53
|
+
s.add_dependency(%q<faraday>, ["~> 0.8.0"])
|
54
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
55
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<hashie>, [">= 0"])
|
59
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.0"])
|
60
|
+
s.add_dependency(%q<faraday>, ["~> 0.8.0"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
62
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
data/lib/opengraph.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module OpenGraph
|
6
|
+
# Fetch Open Graph data from the specified URI. Makes an
|
7
|
+
# HTTP GET request and returns an OpenGraph::Object if there
|
8
|
+
# is data to be found or <tt>false</tt> if there isn't.
|
9
|
+
#
|
10
|
+
# Pass <tt>false</tt> for the second argument if you want to
|
11
|
+
# see invalid (i.e. missing a required attribute) data.
|
12
|
+
def self.fetch(uri, strict = true)
|
13
|
+
parse(Faraday.get(uri).body, strict)
|
14
|
+
rescue Faraday::Error::ClientError, SocketError
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.parse(html, strict = true)
|
19
|
+
doc = Nokogiri::HTML.parse(html)
|
20
|
+
page = OpenGraph::Object.new
|
21
|
+
doc.css('meta').each do |m|
|
22
|
+
if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i)
|
23
|
+
page[$1.gsub('-','_')] = m.attribute('content').to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
return false if page.keys.empty?
|
27
|
+
return false unless page.valid? if strict
|
28
|
+
page
|
29
|
+
end
|
30
|
+
|
31
|
+
TYPES = {
|
32
|
+
'activity' => %w(activity sport),
|
33
|
+
'business' => %w(bar company cafe hotel restaurant),
|
34
|
+
'group' => %w(cause sports_league sports_team),
|
35
|
+
'organization' => %w(band government non_profit school university),
|
36
|
+
'person' => %w(actor athlete author director musician politician public_figure),
|
37
|
+
'place' => %w(city country landmark state_province),
|
38
|
+
'product' => %w(album book drink food game movie product song tv_show),
|
39
|
+
'website' => %w(blog website)
|
40
|
+
}
|
41
|
+
|
42
|
+
# The OpenGraph::Object is a Hash with method accessors for
|
43
|
+
# all detected Open Graph attributes.
|
44
|
+
class Object < Hashie::Mash
|
45
|
+
MANDATORY_ATTRIBUTES = %w(title type image url)
|
46
|
+
|
47
|
+
# The object type.
|
48
|
+
def type
|
49
|
+
self['type']
|
50
|
+
end
|
51
|
+
|
52
|
+
# The schema under which this particular object lies. May be any of
|
53
|
+
# the keys of the TYPES constant.
|
54
|
+
def schema
|
55
|
+
OpenGraph::TYPES.each_pair do |schema, types|
|
56
|
+
return schema if types.include?(self.type)
|
57
|
+
end
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
OpenGraph::TYPES.values.flatten.each do |type|
|
62
|
+
define_method "#{type}?" do
|
63
|
+
self.type == type
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
OpenGraph::TYPES.keys.each do |scheme|
|
68
|
+
define_method "#{scheme}?" do
|
69
|
+
self.type == scheme || OpenGraph::TYPES[scheme].include?(self.type)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# If the Open Graph information for this object doesn't contain
|
74
|
+
# the mandatory attributes, this will be <tt>false</tt>.
|
75
|
+
def valid?
|
76
|
+
MANDATORY_ATTRIBUTES.each{|a| return false unless self[a]}
|
77
|
+
true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/opengraph.gemspec
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ging-opengraph}
|
8
|
+
s.version = "0.0.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Bleigh", "Matt Wilson"]
|
12
|
+
s.date = %q{2010-11-04}
|
13
|
+
s.description = %q{A very simple Ruby library for parsing Open Graph prototocol information from websites. See http://opengraphprotocol.org for more information.}
|
14
|
+
s.email = %q{social-stream@dit.upm.es}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/opengraph.rb",
|
28
|
+
"opengraph.gemspec",
|
29
|
+
"spec/examples/partial.html",
|
30
|
+
"spec/examples/rottentomatoes.html",
|
31
|
+
"spec/opengraph_spec.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/intridea/opengraph}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{A very simple Ruby library for parsing Open Graph prototocol information from websites.}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/opengraph_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<hashie>, [">= 0"])
|
51
|
+
s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.0"])
|
52
|
+
s.add_runtime_dependency(%q<rest-client>, ["~> 1.6.0"])
|
53
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
s.add_development_dependency(%q<webmock>, [">= 0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<hashie>, [">= 0"])
|
57
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.4.0"])
|
58
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.0"])
|
59
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
60
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<hashie>, [">= 0"])
|
64
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.4.0"])
|
65
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.0"])
|
66
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
67
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="ja">
|
3
|
+
<head>
|
4
|
+
<meta charset="EUC-JP" />
|
5
|
+
<title>���Υڡ�����EUC-JP�ǵ��Ҥ���Ƥ��ޤ�- This page is encoded in EUC-JP.</title>
|
6
|
+
<meta property="og:type" content="website" />
|
7
|
+
<meta property="og:title" content="���Υڡ�����EUC-JP�ǵ��Ҥ���Ƥ��ޤ�" />
|
8
|
+
<meta property="og:image" content="http://ogp.me/logo.png" />
|
9
|
+
<meta property="og:description" content="���Υڡ�����EUC-JP�ǵ��Ҥ���Ƥ��ޤ��� - This page is encoded in EUC-JP." />
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
<h1>���Υڡ�����EUC-JP�ǵ��Ҥ���Ƥ��ޤ�</h1>
|
13
|
+
</body>
|
14
|
+
</html>
|