Module: Braintree::Xml::Generator
- Defined in:
- lib/braintree/xml/generator.rb
Constant Summary collapse
- XML_TYPE_NAMES =
{ "Fixnum" => "integer", "Bignum" => "integer", "Integer" => "integer", "TrueClass" => "boolean", "FalseClass" => "boolean", "Date" => "datetime", "DateTime" => "datetime", "Time" => "datetime", "ActiveSupport::TimeWithZone" => "datetime" }
- XML_FORMATTING_NAMES =
{ "BigDecimal" => "bigdecimal", "Symbol" => "symbol" }.merge(XML_TYPE_NAMES)
- XML_FORMATTING =
{ "symbol" => Proc.new { |symbol| symbol.to_s }, "datetime" => Proc.new do |date_or_time| date_or_time.respond_to?(:xmlschema) ? date_or_time.xmlschema : date_or_time.to_s end, "bigdecimal" => Proc.new do |bigdecimal| str = bigdecimal.to_s("F") if str =~ /\.\d$/ str += "0" end str end }
Class Method Summary collapse
- ._array_to_xml(array, options = {}) ⇒ Object
- ._convert_to_xml(hash_to_convert, options = {}) ⇒ Object
- ._xml_escape(key) ⇒ Object
- .hash_to_xml(hash) ⇒ Object
Class Method Details
._array_to_xml(array, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/braintree/xml/generator.rb', line 81 def self._array_to_xml(array, = {}) raise "expected options[:root]" unless [:root] raise "expected options[:builder]" unless [:builder] [:indent] ||= 2 root = .delete(:root).to_s.tr("_", "-") if array.empty? [:builder].tag!(root, :type => "array") else [:builder].tag!(root, :type => "array") do array.each do |e| if e.is_a?(Hash) _convert_to_xml(e, .merge(:root => "item", :skip_instruct => true)) else [:builder].tag!("item", e) end end end end end |
._convert_to_xml(hash_to_convert, options = {}) ⇒ Object
48 49 50 51 52 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/braintree/xml/generator.rb', line 48 def self._convert_to_xml(hash_to_convert, = {}) raise ArgumentError, "need root" unless [:root] [:indent] ||= 2 [:builder] ||= Builder::XmlMarkup.new(:indent => [:indent]) [:builder].instruct! unless .delete(:skip_instruct) root = _xml_escape([:root]) [:builder].__send__(:method_missing, root) do hash_to_convert.each do |key, value| case value when ::Hash _convert_to_xml(value, .merge(:root => key, :skip_instruct => true)) when ::Array _array_to_xml(value, .merge(:root => key, :skip_instruct => true)) else type_name = XML_TYPE_NAMES[value.class.name] attributes = ((value.nil? || type_name.nil?) ? {} : {:type => type_name}) if value.nil? attributes[:nil] = true end formatting_name = XML_FORMATTING_NAMES[value.class.name] [:builder].tag!(_xml_escape(key), XML_FORMATTING[formatting_name] ? XML_FORMATTING[formatting_name].call(value) : value, attributes, ) end end end end |
._xml_escape(key) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/braintree/xml/generator.rb', line 101 def self._xml_escape(key) dasherized_key = key.to_s.tr("_", "-") if Builder::XChar.respond_to?(:encode) Builder::XChar.encode(dasherized_key) else dasherized_key.to_xs end end |
.hash_to_xml(hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/braintree/xml/generator.rb', line 37 def self.hash_to_xml(hash) root, contents = hash.keys[0], hash.values[0] if contents.is_a?(String) builder = Builder::XmlMarkup.new builder.__send__(_xml_escape(root)) { |b| b.text! contents } else _convert_to_xml contents, :root => root end end |