Class: Watobo::HTTP::Xml

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

Defined Under Namespace

Modules: Mixin

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Xml

Returns a new instance of Xml.



81
82
83
84
# File 'lib/watobo/http/xml/xml.rb', line 81

def initialize(root)
  @root = root

end

Instance Method Details

#has_parm?(parm_name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/watobo/http/xml/xml.rb', line 49

def has_parm?(parm_name)
  false
end

#parameters(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/watobo/http/xml/xml.rb', line 53

def parameters(&block)
  params = []

  return params unless @root.is_xml?
  leaf_nodes do |n|
    p = { :name => n.name }
    val = n.children.size == 0 ? "" : n.children.first.to_s

      p[:value] = val
      parent_name = ""
      unless n.parent.namespace.nil?
        parent_name << n.parent.namespace.prefix
        parent_name << ":"  
      end
      parent_name << n.parent.name
      p[:parent] = "#{parent_name}"

      unless n.namespace.nil?
        p[:namespace] = n.namespace.prefix
      end
    param = XmlParameter.new(p)
    yield param if block_given?
    params << param
  end

  return params
end

#set(parm) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/watobo/http/xml/xml.rb', line 16

def set(parm)
  return false unless parm.location == :xml
 # puts "= set "
 # puts parm.to_yaml
  
  doc = Nokogiri::XML(@root.body.strip)
  namespaces = doc.collect_namespaces
  parent = doc.xpath("//#{parm.parent}", namespaces).first
  if parent.nil?
    puts "* could not find parent node #{parm.parent}"
    return false
  end
  
  parm_name = parm.namespace.nil? ? "" : parm.namespace
  parm_name << parm.name
  # find node
  node = parent.xpath("//#{parm_name}", namespaces).first
  if node.nil?
    puts "* node does not exist #{parm_name}"
  end
  
  child = node.children.first
  if child.nil?
    child = Nokogiri::XML::Text.new(parm.value, node)
    node.add_child child
  else        
    child.content = parm.value
  end
  
  @root.set_body doc.to_s

end

#to_sObject



12
13
14
# File 'lib/watobo/http/xml/xml.rb', line 12

def to_s
  s = @root.body.to_s
end