Module: Recurly::XML::NokogiriAdapter

Included in:
Recurly::XML
Defined in:
lib/recurly/xml/nokogiri.rb

Instance Method Summary collapse

Instance Method Details

#[](xpath) ⇒ Object



30
31
32
# File 'lib/recurly/xml/nokogiri.rb', line 30

def [] xpath
  root.at_xpath xpath
end

#add_element(name, value = nil) ⇒ Object



8
9
10
11
12
# File 'lib/recurly/xml/nokogiri.rb', line 8

def add_element name, value = nil
  root.add_child(node = ::Nokogiri::XML::Element.new(name, root))
  node << value if value
  node
end

#each(element = root) ⇒ Object



19
20
21
22
23
24
# File 'lib/recurly/xml/nokogiri.rb', line 19

def each element = root
  element.elements.each do |el|
    yield el
    each el, &Proc.new
  end
end

#each_element(xpath = nil) ⇒ Object



14
15
16
17
# File 'lib/recurly/xml/nokogiri.rb', line 14

def each_element xpath = nil
  elements = xpath.nil? ? root.children : root.xpath(xpath)
  elements.each { |el| yield el }
end

#initialize(xml) ⇒ Object



4
5
6
# File 'lib/recurly/xml/nokogiri.rb', line 4

def initialize xml
  @root = Nokogiri(xml).root
end

#nameObject



26
27
28
# File 'lib/recurly/xml/nokogiri.rb', line 26

def name
  root.name
end

#text(xpath = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/recurly/xml/nokogiri.rb', line 34

def text xpath = nil
  if node = (xpath ? root.at_xpath(xpath) : root)
    if node.text?
      node.text
    else
      node.children.each { |e| return e.text if e.text? }
    end
  end
end

#text=(text) ⇒ Object



44
45
46
# File 'lib/recurly/xml/nokogiri.rb', line 44

def text= text
  root.content = text
end

#to_sObject



48
49
50
# File 'lib/recurly/xml/nokogiri.rb', line 48

def to_s
  root.to_xml(:indent => 0).gsub(/$\n/, '')
end