class OpenGraphReader::Parser::Graph
A Graph
to represent OpenGraph tags.
Constants
- Node
A node in the graph.
Attributes
The initial node.
@return [Node, nil]
Public Class Methods
Create new graph.
# File lib/open_graph_reader/parser/graph.rb, line 90 def initialize @root = Node.new end
Public Instance Methods
Iterate through all nodes that have a value.
@yield [Node]
# File lib/open_graph_reader/parser/graph.rb, line 97 def each root.each do |child| yield child if child.content end end
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.
@param [String] property The fully qualified name, for example og:type
or <tt>og</tt>.
@return [Bool] Whether the given property exists in the graph.
# 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 first node's value.
@param [String] property The fully qualified name, for example og:type
. @param [String] default The default in case the a value is not found. @yield Return a default in case the value is not found. Supersedes the default parameter. @return [String, Bool, Integer, Float, DateTime, nil]
# 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
Fetch first node
@param [String] property The fully qualified name, for example og:type
. @return [Node, nil]
# File lib/open_graph_reader/parser/graph.rb, line 135 def find_by property property = normalize_property property find {|node| node.fullname == property } end
# 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
Fetch all nodes
@param [String] property The fully qualified name, for example og:type
. @return [Array<Node>]
# File lib/open_graph_reader/parser/graph.rb, line 144 def select_by property property = normalize_property property select {|node| node.fullname == property } end
Private Instance Methods
# File lib/open_graph_reader/parser/graph.rb, line 164 def normalize_property property property.is_a?(Enumerable) ? property.join(":") : property end