Permalink
Browse files

Initial import.

  • Loading branch information...
1 parent c4f5315 commit 3a7c3aff456acd96179632a90f8d4c0ed148de1e Michael Bleigh committed Apr 22, 2010
Showing with 1,937 additions and 8 deletions.
  1. +2 −2 README.rdoc
  2. +7 −3 Rakefile
  3. +76 −0 lib/opengraph.rb
  4. +1,798 −0 spec/examples/rottentomatoes.html
  5. +49 −3 spec/opengraph_spec.rb
  6. +5 −0 spec/spec_helper.rb
View
@@ -1,6 +1,6 @@
-= opengraph
+= OpenGraph
-Description goes here.
+OpenGraph is a very simple library for parsing Open Graph information from web sites.
== Note on Patches/Pull Requests
View
@@ -5,12 +5,16 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "opengraph"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{A very simple Ruby library for parsing Open Graph prototocol information from websites.}
+ gem.description = %Q{A very simple Ruby library for parsing Open Graph prototocol information from websites. See http://opengraphprotocol.org for more information.}
gem.email = "michael@intridea.com"
- gem.homepage = "http://github.com/mbleigh/opengraph"
+ gem.homepage = "http://github.com/intridea/opengraph"
gem.authors = ["Michael Bleigh"]
+ gem.add_dependency 'hashie'
+ gem.add_dependency 'nokogiri', '~> 1.4.0'
+ gem.add_dependency 'rest-client', '~> 1.4.0'
gem.add_development_dependency "rspec", ">= 1.2.9"
+ gem.add_development_dependency 'webmock'
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
@@ -0,0 +1,76 @@
+require 'hashie'
+require 'nokogiri'
+require 'restclient'
+
+module OpenGraph
+ # Fetch Open Graph data from the specified URI. Makes an
+ # HTTP GET request and returns an OpenGraph::Object if there
+ # is data to be found or <tt>false</tt> if there isn't.
+ def self.fetch(uri)
+ parse(RestClient.get(uri).body)
+ end
+
+ def self.parse(html)
+ doc = Nokogiri::HTML.parse(html)
+ page = OpenGraph::Object.new
+ doc.css('meta').each do |m|
+ if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i)
+ page[$1.gsub('-','_')] = m.attribute('content').to_s
+ end
+ end
+ return false unless page.valid?
+ page
+ rescue RestClient::Exception
+ false
+ end
+
+ TYPES = {
+ 'activity' => %w(activity sport),
+ 'business' => %w(bar company cafe hotel restaurant),
+ 'group' => %w(cause sports_league sports_team),
+ 'organization' => %w(band government non_profit school university),
+ 'person' => %w(actor athlete author director musician politician public_figure),
+ 'place' => %w(city country landmark state_province),
+ 'product' => %w(album book drink food game movie product song tv_show),
+ 'website' => %w(blog website)
+ }
+
+ # The OpenGraph::Object is a Hash with method accessors for
+ # all detected Open Graph attributes.
+ class Object < Hashie::Mash
+ MANDATORY_ATTRIBUTES = %w(title type image url)
+
+ # The object type.
+ def type
+ self['type']
+ end
+
+ # The schema under which this particular object lies. May be any of
+ # the keys of the TYPES constant.
+ def schema
+ OpenGraph::TYPES.each_pair do |schema, types|
+ return schema if types.include?(self.type)
+ end
+ nil
+ end
+
+ OpenGraph::TYPES.values.flatten.each do |type|
+ define_method "#{type}?" do
+ self.type == type
+ end
+ end
+
+ OpenGraph::TYPES.keys.each do |scheme|
+ define_method "#{scheme}?" do
+ self.type == scheme || OpenGraph::TYPES[scheme].include?(self.type)
+ end
+ end
+
+ # If the Open Graph information for this object doesn't contain
+ # the mandatory attributes, this will be <tt>false</tt>.
+ def valid?
+ MANDATORY_ATTRIBUTES.each{|a| return false unless self[a]}
+ true
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 3a7c3af

Please sign in to comment.