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.



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

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

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
# 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?
    resource_name = Helper.classify el.name
    if Recurly.const_defined? resource_name, false
      return Recurly.const_get(resource_name, false).from_xml el
    end
    if el.elements.empty?
      el.text
    else
      Hash[el.elements.map { |e| [e.name, XML.cast(e)] }]
    end
  end
end

.filter(text) ⇒ Object



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

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.



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

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.



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

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

#nameObject

Returns the root’s name.



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

def name
  super
end

#to_sObject

Returns an XML string.



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

def to_s
  super
end