forked from jcouture/ogp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create basic OpenGraph implementation.
- Loading branch information
Showing
8 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format documentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new('spec') | ||
|
||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require "ogp/version" | ||
require 'ogp/version' | ||
require 'ogp/open_graph' | ||
|
||
module OGP | ||
# Your code goes here... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'oga' | ||
|
||
REQUIRED_ATTRIBUTES = %w(title type image url) | ||
|
||
module OGP | ||
class OpenGraph | ||
attr_accessor :title, :type, :image, :url | ||
|
||
def initialize(source) | ||
document = Oga.parse_html(source) | ||
parse_required_attributes(document) | ||
end | ||
|
||
private | ||
|
||
def parse_required_attributes(document) | ||
REQUIRED_ATTRIBUTES.each do |attribute_name| | ||
instance_variable_set("@#{attribute_name}", parse_attribute(document, attribute_name)) | ||
end | ||
end | ||
|
||
def parse_attribute(document, name) | ||
document.at_xpath("//head/meta[@property='og:#{name}']").get('content') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
describe OGP::OpenGraph do | ||
describe '#initialize' do | ||
context 'with strictly the required attributes' do | ||
it 'should parse attributes properly' do | ||
content = File.read("#{File.dirname(__FILE__)}/../view/required_attributes.html") | ||
open_graph = OGP::OpenGraph.new(content) | ||
expect(open_graph.title).to eql('The Rock') | ||
expect(open_graph.type).to eql('video.movie') | ||
expect(open_graph.url).to eql('http://www.imdb.com/title/tt0117500/') | ||
expect(open_graph.image).to eql('http://ia.media-imdb.com/images/rock.jpg') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
require 'ogp' | ||
|
||
RSpec.configure do |config| | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta content="The Rock" property="og:title"> | ||
<meta content="video.movie" property="og:type"> | ||
<meta content="http://www.imdb.com/title/tt0117500/" property="og:url"> | ||
<meta content="http://ia.media-imdb.com/images/rock.jpg" property="og:image"> | ||
<title></title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |