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/object/registry.rb,
lib/open_graph_reader/object/dsl/types.rb

Overview

TODO:

quirks mode where invalid attributes don’t raise?

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, Fetcher, InvalidObjectError, Music, NoOpenGraphDataError, Og, Parser, Profile, Video

Constant Summary collapse

VERSION =

Tbe library version

"0.1.0"

Class Method Summary collapse

Class Method Details

.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:



58
59
60
61
# File 'lib/open_graph_reader.rb', line 58

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.



26
27
28
29
30
31
32
33
34
35
# File 'lib/open_graph_reader.rb', line 26

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:



70
71
72
73
# File 'lib/open_graph_reader.rb', line 70

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.



44
45
46
47
48
49
50
# File 'lib/open_graph_reader.rb', line 44

def self.parse! html, origin=nil
  parser = Parser.new html
  raise NoOpenGraphDataError, "#{origin || html} does not contain any OpenGraph tags" unless parser.has_tags?
  Builder.new(parser.graph, parser.additional_namespaces).base.tap {|base|
    base.origin = origin.to_s if origin
  }
end