opengraph 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/opengraph.rb +8 -4
- data/opengraph.gemspec +1 -1
- data/spec/opengraph_spec.rb +9 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/opengraph.rb
CHANGED
@@ -6,13 +6,16 @@ module OpenGraph
|
|
6
6
|
# Fetch Open Graph data from the specified URI. Makes an
|
7
7
|
# HTTP GET request and returns an OpenGraph::Object if there
|
8
8
|
# is data to be found or <tt>false</tt> if there isn't.
|
9
|
-
|
10
|
-
|
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(RestClient.get(uri).body, strict)
|
11
14
|
rescue RestClient::Exception, SocketError
|
12
15
|
false
|
13
16
|
end
|
14
17
|
|
15
|
-
def self.parse(html)
|
18
|
+
def self.parse(html, strict = true)
|
16
19
|
doc = Nokogiri::HTML.parse(html)
|
17
20
|
page = OpenGraph::Object.new
|
18
21
|
doc.css('meta').each do |m|
|
@@ -20,7 +23,8 @@ module OpenGraph
|
|
20
23
|
page[$1.gsub('-','_')] = m.attribute('content').to_s
|
21
24
|
end
|
22
25
|
end
|
23
|
-
return false
|
26
|
+
return false if page.keys.empty?
|
27
|
+
return false unless page.valid? if strict
|
24
28
|
page
|
25
29
|
end
|
26
30
|
|
data/opengraph.gemspec
CHANGED
data/spec/opengraph_spec.rb
CHANGED
@@ -2,15 +2,24 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe OpenGraph do
|
4
4
|
let(:rotten){ File.open(File.dirname(__FILE__) + '/examples/rottentomatoes.html').read }
|
5
|
+
let(:partial){ File.open(File.dirname(__FILE__) + '/examples/partial.html').read }
|
5
6
|
|
6
7
|
describe '.parse' do
|
7
8
|
it 'should return false if there isnt valid Open Graph info' do
|
8
9
|
OpenGraph.parse("").should be_false
|
10
|
+
OpenGraph.parse(partial).should be_false
|
9
11
|
end
|
10
12
|
|
11
13
|
it 'should otherwise return an OpenGraph::Object' do
|
12
14
|
OpenGraph.parse(rotten).should be_kind_of(OpenGraph::Object)
|
13
15
|
end
|
16
|
+
|
17
|
+
context ' without strict mode' do
|
18
|
+
subject{ OpenGraph.parse(partial, false) }
|
19
|
+
|
20
|
+
it { should_not be_false }
|
21
|
+
it { subject.title.should == 'Partialized' }
|
22
|
+
end
|
14
23
|
end
|
15
24
|
|
16
25
|
describe '.fetch' do
|