Sha256: b0650fbf5c56463048aab4611cca55c8c3e83e70003784b85d6d500f1150e24b

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Securetrading
  class XmlDoc
    def initialize(request_type, account_type)
      @account_type = account_type
      @request_type = request_type
    end

    def doc
      return @doc if @doc.present?
      @doc = Ox::Document.new(version: '1.0')
      root = new_element('requestblock')
      root[:version] = '3.67'
      root << alias_el << request_el
      @doc << root
    end

    # rubocop:disable Metrics/MethodLength
    def self.elements(hash)
      return '' unless hash.present?
      hash.flat_map do |k, v|
        return v.flat_map { |e| elements(k => e) } if v.is_a?(Array)
        el = new_element(k.to_s)
        if v.is_a?(Hash)
          elements(v).each { |e| el << e }
        else
          el << v.to_s
        end
        [el]
      end
    end
    # rubocop:enable Metrics/MethodLength

    def self.new_element(name)
      Ox::Element.new(name)
    end

    private

    def new_element(name)
      self.class.new_element(name)
    end

    def alias_el
      self.class.elements(alias: Securetrading.config.user).first
    end

    def request_el
      el = new_element('request')
      el[:type] = @request_type
      el
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
securetrading-0.3.0 lib/securetrading/xml_doc.rb
securetrading-0.2.0 lib/securetrading/xml_doc.rb