module OpenGraphReader

@todo 1.1 compatibility mode? This module provides the main entry to the library. Please see the {file:README.md} for usage examples.

Constants

VERSION

Tbe library version

Public Class Methods

config() click to toggle source

Get the current {Configuration} instance

@api private @return [Configuration]

# File lib/open_graph_reader.rb, line 87
def self.config
  Configuration.instance
end
configure() { |config| ... } click to toggle source

Configure the library, see {Configuration} for the list of available options and their defaults. Changing configuration at runtime is not thread safe.

@yieldparam [Configuration] the configuration object @see Configuration

# File lib/open_graph_reader.rb, line 79
def self.configure
  yield config
end
current_origin() click to toggle source

Thread local to retrieve the current origin if available. See {Base#origin} if you want to know the origin of a parsed object.

@api private @return [String,nil]

# File lib/open_graph_reader.rb, line 96
def self.current_origin
  Thread.current[:_open_graph_reader_current_origin]
end
current_origin=(value) click to toggle source
# File lib/open_graph_reader.rb, line 100
def self.current_origin= value
  Thread.current[:_open_graph_reader_current_origin] = value
end
fetch(url) click to toggle source

Convenience wrapper around {OpenGraphReader.fetch!} that swallows the esceptions and returns nil instead.

@param [URI,#to_s] url The URL of the OpenGraph object to retrieve. @return [Base, nil] The base object from which you can obtain the root objects. @see OpenGraphReader.fetch!

# File lib/open_graph_reader.rb, line 56
def self.fetch url
  fetch! url
rescue NoOpenGraphDataError, InvalidObjectError
end
fetch!(url) click to toggle source

Fetch the OpenGraph object at the given URL. Raise if there are any issues.

@param [URI,#to_s] url The URL of the OpenGraph object to retrieve. @return [Base] The base object from which you can obtain the root objects. @raise [NoOpenGraphDataError] {include:NoOpenGraphDataError} @raise [InvalidObjectError] {include:InvalidObjectError}

# File lib/open_graph_reader.rb, line 22
def self.fetch! url
  case url
  when URI
    target = Fetcher.new(url)
    raise NoOpenGraphDataError, "#{url} doesn't contain any HTML" unless target.html?
    parse! target.body, target.url
  else
    fetch! URI.parse(url.to_s)
  end
end
parse(html, origin=nil) click to toggle source

Convenience wrapper around {OpenGraphReader.parse!} that swallows the esceptions and returns nil instead.

@param [#to_s] html A HTML document that contains an OpenGraph object. @param [#to_s] origin The source from where the given document was fetched. @return [Base, nil] The base object from which you can obtain the root objects. @see OpenGraphReader.parse!

# File lib/open_graph_reader.rb, line 68
def self.parse html, origin=nil
  parse! html, origin
rescue NoOpenGraphDataError, InvalidObjectError
end
parse!(html, origin=nil) click to toggle source

Parse the OpenGraph object in the given HTML document. Raise if there are any issues.

@param [#to_s, Nokogiri::XML::Node] html A HTML document that contains an OpenGraph object. @param [#to_s] origin The source from where the given document was fetched. @return [Base] The base object from which you can obtain the root objects. @raise [NoOpenGraphDataError] {include:NoOpenGraphDataError} @raise [InvalidObjectError] {include:InvalidObjectError}

# File lib/open_graph_reader.rb, line 40
def self.parse! html, origin=nil
  self.current_origin = origin
  parser = Parser.new html
  raise NoOpenGraphDataError, "#{origin || html} does not contain any OpenGraph tags" unless parser.any_tags?
  Builder.new(parser).base.tap {|base|
    base.origin = origin.to_s if origin
    self.current_origin = nil
  }
end