Module: OpenGraphReader

Defined in:
lib/open_graph_reader.rb,
lib/open_graph_reader/base.rb,
lib/open_graph_reader/object.rb,
lib/open_graph_reader/parser.rb,
lib/open_graph_reader/builder.rb,
lib/open_graph_reader/fetcher.rb,
lib/open_graph_reader/version.rb,
lib/open_graph_reader/object/dsl.rb,
lib/open_graph_reader/definitions.rb,
lib/open_graph_reader/parser/graph.rb,
lib/open_graph_reader/configuration.rb,
lib/open_graph_reader/object/registry.rb,
lib/open_graph_reader/object/dsl/types.rb

Overview

TODO:

1.1 compatibility mode?

This module provides the main entry to the library. Please see the README for usage examples.

Defined Under Namespace

Modules: Object Classes: Article, Base, Book, Builder, Configuration, Fetcher, InvalidObjectError, Music, NoOpenGraphDataError, Og, Parser, Profile, UndefinedPropertyError, UnknownNamespaceError, Video

Constant Summary collapse

VERSION =

Tbe library version

"0.7.2"

Class Method Summary collapse

Class Method Details

.configConfiguration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the current Configuration instance

Returns:



87
88
89
# File 'lib/open_graph_reader.rb', line 87

def self.config
  Configuration.instance
end

.configure {|the| ... } ⇒ Object

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

Yield Parameters:

See Also:



79
80
81
# File 'lib/open_graph_reader.rb', line 79

def self.configure
  yield config
end

.current_originString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String, nil)


96
97
98
# File 'lib/open_graph_reader.rb', line 96

def self.current_origin
  Thread.current[:_open_graph_reader_current_origin]
end

.current_origin=(value) ⇒ Object



100
101
102
# File 'lib/open_graph_reader.rb', line 100

def self.current_origin= value
  Thread.current[:_open_graph_reader_current_origin] = value
end

.fetch(url) ⇒ Base?

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

Parameters:

  • url (URI, #to_s)

    The URL of the OpenGraph object to retrieve.

Returns:

  • (Base, nil)

    The base object from which you can obtain the root objects.

See Also:



56
57
58
59
# File 'lib/open_graph_reader.rb', line 56

def self.fetch url
  fetch! url
rescue NoOpenGraphDataError, InvalidObjectError
end

.fetch!(url) ⇒ Base

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

Parameters:

  • url (URI, #to_s)

    The URL of the OpenGraph object to retrieve.

Returns:

  • (Base)

    The base object from which you can obtain the root objects.

Raises:

  • (NoOpenGraphDataError)

    The target couldn’t be fetched, didn’t contain any HTML or any OpenGraph tags.

  • (InvalidObjectError)

    The target did contain OpenGraph tags, but they’re not valid.



22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Base?

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

Parameters:

  • html (#to_s)

    A HTML document that contains an OpenGraph object.

  • origin (#to_s) (defaults to: nil)

    The source from where the given document was fetched.

Returns:

  • (Base, nil)

    The base object from which you can obtain the root objects.

See Also:



68
69
70
71
# 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) ⇒ Base

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

Parameters:

  • html (#to_s, Nokogiri::XML::Node)

    A HTML document that contains an OpenGraph object.

  • origin (#to_s) (defaults to: nil)

    The source from where the given document was fetched.

Returns:

  • (Base)

    The base object from which you can obtain the root objects.

Raises:

  • (NoOpenGraphDataError)

    The target couldn’t be fetched, didn’t contain any HTML or any OpenGraph tags.

  • (InvalidObjectError)

    The target did contain OpenGraph tags, but they’re not valid.



40
41
42
43
44
45
46
47
48
# 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