class TelegraphApi::DomToNode

Attributes

data[R]

Public Class Methods

call(*args) click to toggle source
# File lib/telegraph_api_ruby/dom_to_node.rb, line 7
def self.call(*args)
  new(*args).call
end
new(data) click to toggle source
# File lib/telegraph_api_ruby/dom_to_node.rb, line 11
def initialize(data)
  @data = data
end

Public Instance Methods

call() click to toggle source
# File lib/telegraph_api_ruby/dom_to_node.rb, line 15
def call
  dom_to_node(parse_html(data))[:children]
end

Private Instance Methods

dom_to_node(dom_node) click to toggle source
# File lib/telegraph_api_ruby/dom_to_node.rb, line 25
def dom_to_node(dom_node)
  return dom_node.text if dom_node.is_a?(Oga::XML::Text)

  node_element = {
    children: []
  }

  if dom_node.is_a?(Oga::XML::Element)
    node_element[:tag] = dom_node.name
    node_element[:attrs] = {}

    Array(dom_node.attributes).each do |attr|
      node_element[:attrs][attr.name] = attr.value
    end
  end

  Array(dom_node.children).each do |child|
    node_element[:children].push(dom_to_node(child))
  end

  node_element
end
parse_html(data) click to toggle source
# File lib/telegraph_api_ruby/dom_to_node.rb, line 21
def parse_html(data)
  Oga.parse_html(data)
end