Sha256: f8143120d38230059826a9a041b10727ce59a496432dc5d3c9136ef3e2d48c6e

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Securetrading
  class Connection
    include HTTParty
    base_uri 'https://webservices.securetrading.net/xml'

    # headers after: http://www.securetrading.com/files/documentation/STPP-Web-Services-User-Guide.pdf
    headers(
      'Content-Type' => 'text/xml;charset=utf-8',
      'Accept-Encoding' => 'gzip',
      'Accept' => 'text/xml',
      'User-Agent' => 'Securetrading Ruby gem; '\
                      "version: #{Securetrading::VERSION}",
      'Connection' => 'close'
    )

    def to_xml
      Ox.dump(ox_xml)
    end

    private

    def doc
      @doc ||= XmlDoc.new(request_type, @account_type).doc
    end

    def request_type
      fail NotImplementedError, 'Implement :request_type method in subclas!'
    end

    def ox_xml
      fail NotImplementedError, 'Implement :ox_xml method in subclas!'
    end

    def perform_with(method, xml, options = {})
      self.class.public_send(method,
                             '/',
                             options.merge(body: xml,
                                           headers: dynamic_headers))
    end

    def dynamic_headers
      { 'Authorization' => "Basic #{Securetrading.config.auth}" }
    end

    def prepare_doc
      return doc if xml_prepared?
      yield
      @xml_prepared = true
      doc
    end

    def xml_prepared?
      @xml_prepared
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
securetrading-0.2.0 lib/securetrading/connection.rb