Class: OpenGraphReader::Parser::Graph Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/open_graph_reader/parser/graph.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.

A Graph to represent OpenGraph tags.

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

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 new graph.



90
91
92
# File 'lib/open_graph_reader/parser/graph.rb', line 90

def initialize
  @root = Node.new
end

Instance Attribute Details

#rootNode? (readonly)

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.

The initial node.

Returns:



81
82
83
# File 'lib/open_graph_reader/parser/graph.rb', line 81

def root
  @root
end

Instance Method Details

#each {|Node| ... } ⇒ Object

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.

Iterate through all nodes that have a value.

Yields:



97
98
99
100
101
# File 'lib/open_graph_reader/parser/graph.rb', line 97

def each
  root.each do |child|
    yield child if child.content
  end
end

#empty?Bool

Does this graph have any nodes?

Returns:

  • (Bool)


87
# File 'lib/open_graph_reader/parser/graph.rb', line 87

def_delegators :root, :empty?

#exist?(property) ⇒ Bool

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.

Whether the property exists in the graph. property doesn’t have to be a leave node but is only considered existing when there’s a leaf node with content below it.

Parameters:

  • property (String)

    The fully qualified name, for example og:type or og.

Returns:

  • (Bool)

    Whether the given property exists in the graph.



110
111
112
113
114
115
116
# File 'lib/open_graph_reader/parser/graph.rb', line 110

def exist? property
  path = property.split(":")
  child = path.inject(root) {|node, name|
    node.children.find {|child| child.name == name } || break
  }
  !child.nil? && !child.empty?
end

#fetch(property, default = nil) { ... } ⇒ String, ...

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.

Fetch first node’s value.

Parameters:

  • property (String)

    The fully qualified name, for example og:type.

  • default (String) (defaults to: nil)

    The default in case the a value is not found.

Yields:

  • Return a default in case the value is not found. Supersedes the default parameter.

Returns:

  • (String, Bool, Integer, Float, DateTime, nil)


124
125
126
127
128
129
# File 'lib/open_graph_reader/parser/graph.rb', line 124

def fetch property, default=nil
  node = find_by(property)
  return yield if node.nil? && block_given?
  return default if node.nil?
  node.content
end

#find_by(property) ⇒ Node?

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.

Fetch first node

Parameters:

  • property (String)

    The fully qualified name, for example og:type.

Returns:



135
136
137
138
# File 'lib/open_graph_reader/parser/graph.rb', line 135

def find_by property
  property = normalize_property property
  find {|node| node.fullname == property }
end

#find_or_create_path(path) ⇒ Object

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.



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/open_graph_reader/parser/graph.rb', line 149

def find_or_create_path path
  path.inject(root) {|node, name|
    child = node.children.reverse.find {|child| child.name == name }

    unless child
      child = Node.new name
      node << child
    end

    child
  }
end

#select_by(property) ⇒ Array<Node>

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.

Fetch all nodes

Parameters:

  • property (String)

    The fully qualified name, for example og:type.

Returns:



144
145
146
147
# File 'lib/open_graph_reader/parser/graph.rb', line 144

def select_by property
  property = normalize_property property
  select {|node| node.fullname == property }
end