Skip to content

Commit

Permalink
Create basic OpenGraph implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouture committed Sep 9, 2017
1 parent 9251938 commit 023ce7d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format documentation
6 changes: 5 additions & 1 deletion Rakefile
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
3 changes: 2 additions & 1 deletion lib/ogp.rb
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...
Expand Down
26 changes: 26 additions & 0 deletions lib/ogp/open_graph.rb
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
2 changes: 2 additions & 0 deletions ogp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
spec.require_paths = ['lib']

spec.add_dependency 'oga', '~> 2.10'

spec.add_development_dependency 'bundler', '~> 1.13'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.6'
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/open_graph_spec.rb
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
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
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
12 changes: 12 additions & 0 deletions spec/view/required_attributes.html
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>

0 comments on commit 023ce7d

Please sign in to comment.