Class: Recurly::XML

Inherits:
Object
  • Object
show all
Includes:
NokogiriAdapter, REXMLAdapter
Defined in:
lib/recurly/xml.rb,
lib/recurly/xml/rexml.rb,
lib/recurly/xml/nokogiri.rb

Defined Under Namespace

Modules: NokogiriAdapter, REXMLAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NokogiriAdapter

#[], #each, #text, #text=

Methods included from REXMLAdapter

#[], #each, #text, #text=

Constructor Details

#initialize(xml) ⇒ XML

Returns a new instance of XML.



53
54
55
# File 'lib/recurly/xml.rb', line 53

def initialize xml
  @root = xml.is_a?(String) ? super : xml
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



51
52
53
# File 'lib/recurly/xml.rb', line 51

def root
  @root
end

Class Method Details

.cast(el) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/recurly/xml.rb', line 4

def cast el
  return if el.attribute 'nil'

  if el.attribute 'type'
    type = el.attribute('type').value
  end

  case type
    when 'array'    then el.elements.map { |e| XML.cast e }
    when 'boolean'  then el.text == 'true'
    when 'date'     then Date.parse el.text
    when 'datetime' then DateTime.parse el.text
    when 'float'    then el.text.to_f
    when 'integer'  then el.text.to_i
  else
    # FIXME: Move some of this logic to Resource.from_xml?
    if type and resource_name = Helper.classify(type)
      if Recurly.const_defined? resource_name
        resource_class = Recurly.const_get(Helper.classify(type))
        return resource_class.new resource_class.from_xml(el)
      end
    end
    if el.elements.empty?
      el.text
    else
      Hash[el.elements.map { |e| [e.name, XML.cast(e)] }]
    end
  end
end

.filter(text) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/recurly/xml.rb', line 34

def filter text
  xml = XML.new text
  xml.each do |el|
    el = XML.new el
    case el.name
    when "number"
      text = el.text
      last = text[-4, 4]
      el.text = "#{text[0, text.length - 4].to_s.gsub(/\d/, '*')}#{last}"
    when "verification_value"
      el.text = el.text.gsub(/\d/, '*')
    end
  end
  xml.to_s
end

Instance Method Details

#add_element(name, value = nil) ⇒ Object

Adds an element to the root.



58
59
60
61
# File 'lib/recurly/xml.rb', line 58

def add_element name, value = nil
  value = value.respond_to?(:xmlschema) ? value.xmlschema : value.to_s
  XML.new super(name, value)
end

#each_element(xpath = nil) ⇒ Object

Iterates over the root’s elements.



64
65
66
67
# File 'lib/recurly/xml.rb', line 64

def each_element xpath = nil
  return enum_for :each_element unless block_given?
  super
end

#nameObject

Returns the root’s name.



70
71
72
# File 'lib/recurly/xml.rb', line 70

def name
  super
end

#to_sObject

Returns an XML string.



75
76
77
# File 'lib/recurly/xml.rb', line 75

def to_s
  super
end