Class: OpenGraphReader::Builder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/open_graph_reader/builder.rb

Overview

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

TODO:

validate required, verticals

Convert a Parser::Graph into the right hierarchy of Objects attached to a Base.

Constant Summary collapse

KNOWN_TYPES =

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

Well-known types from

See Also:

%w(
  website
  music.song
  music.album
  music.playlist
  music.radio_station
  video.movie
  video.episode
  video.tv_show
  video.other
  article
  book
  profile
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(graph, additional_namespaces = []) ⇒ Builder

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.

Create a new builder.

Parameters:

  • graph (Parser::Graph)
  • additional_namespaces (Array<String>) (defaults to: [])

    Namespaces found in the prefix attribute of the head tag of the HTML document

See Also:



33
34
35
36
# File 'lib/open_graph_reader/builder.rb', line 33

def initialize graph, additional_namespaces=[]
  @graph = graph
  @additional_namespaces = additional_namespaces
end

Instance Method Details

#baseBase

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.

Build and return the base.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/open_graph_reader/builder.rb', line 41

def base
  base = Base.new

  type = @graph.fetch 'og:type', 'website'

  validate_type type

  @graph.each do |property|
    root, *path, name = property.path
    base[root] ||= Object::Registry[root].new
    object = resolve base[root], root, path

    if object.respond_to? "#{name}s" # Collection # TODO
      collection = object.public_send "#{name}s" #TODO
      if Object::Registry.registered? property.fullname # of subobjects
        object = Object::Registry[property.fullname].new
        collection << object
        object.content = property.content
      else # of type
        collection << property.content
      end
    elsif Object::Registry.registered? property.fullname # Subobject
      object[name] ||= Object::Registry[property.fullname].new
      object[name].content = property.content
    else # Direct attribute
      object[name] = property.content
    end
  end

  base
end