Module: OpenGraph

Defined in:
lib/opengraph.rb

Defined Under Namespace

Classes: Object

Constant Summary collapse

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)
}

Class Method Summary collapse

Class Method Details

.fetch(uri, strict = true) ⇒ Object

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 false if there isn’t.

Pass false for the second argument if you want to see invalid (i.e. missing a required attribute) data.



12
13
14
15
16
# File 'lib/opengraph.rb', line 12

def self.fetch(uri, strict = true)
  parse(RestClient.get(uri).body, strict)
rescue RestClient::Exception, SocketError
  false
end

.parse(html, strict = true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/opengraph.rb', line 18

def self.parse(html, strict = true)
  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 if page.keys.empty?
  return false unless page.valid? if strict
  page
end