Class: Webhookdb::Xml::Atom::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/webhookdb/xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thing) ⇒ Parser

Returns a new instance of Parser.



17
18
19
# File 'lib/webhookdb/xml.rb', line 17

def initialize(thing)
  @doc = Nokogiri::XML.parse(thing, &:noblanks)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



15
16
17
# File 'lib/webhookdb/xml.rb', line 15

def doc
  @doc
end

Instance Method Details

#parse_entry(e) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webhookdb/xml.rb', line 52

def parse_entry(e)
  h = {}
  e.children.each do |c|
    if c.name == "content"
      content = {}
      h["content"] = content
      content["value"] = c.children.to_s if c.children.to_s.present?
      c.attributes.each do |k, v|
        content[k] = v.value
      end
    elsif self.spec_attr?(c)
      h[self.fqn(c)] = self.parse_spec_attr(c)
    else
      h[self.fqn(c)] = c.text
    end
  end
  return h
end

#to_hashObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/webhookdb/xml.rb', line 21

def to_hash
  entries = []
  feed = {"entries" => entries}
  @doc.root.children.each do |c|
    if c.is_a?(Nokogiri::XML::Text)
      next
    elsif c.name == "entry"
      entries << self.parse_entry(c)
    elsif self.spec_attr?(c)
      feed[self.fqn(c)] = self.parse_spec_attr(c)
    elsif self.simple_text?(c)
      feed[self.fqn(c)] = self.text(c)
    else
      feed[self.fqn(c)] = self.parse_to_hash(c)
    end
  end
  return feed
end