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.



56
57
58
# File 'lib/recurly/xml.rb', line 56

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

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
33
34
35
# 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'
      el.elements.map { |e|
        e.elements.empty? ? XML.cast(e) : { e.name => 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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/recurly/xml.rb', line 37

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].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.



61
62
63
64
# File 'lib/recurly/xml.rb', line 61

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.



67
68
69
70
# File 'lib/recurly/xml.rb', line 67

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

#nameObject

Returns the root’s name.



73
74
75
# File 'lib/recurly/xml.rb', line 73

def name
  super
end

#to_sObject

Returns an XML string.



78
79
80
# File 'lib/recurly/xml.rb', line 78

def to_s
  super
end