Sha256: 8c9441f9f5ebed5ab328b728200c4dc64721f6c2f84f25eacc863674ca0d7ad1

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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

    def self.elements(hash)
      return unless hash.present?
      hash.map do |k, v|
        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

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

    private

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

    def operation
      self.class.elements(
        operation: {
          sitereference: Securetrading.config.site_reference,
          accounttypedescription: @account_type
        }
      ).first
    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 << operation
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
securetrading-0.1.0 lib/securetrading/xml_doc.rb