Sha256: db2f07a5c3ea158daf218d8b1a09765ea1869357de74735e6540fcbd263fa560

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 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 sub-class!'
    end

    def ox_xml
      fail NotImplementedError, 'Implement :ox_xml method in sub-class!'
    end

    def perform_with(method, xml, options = {})
      party = self.class.public_send(
        method, '/', options.merge(body: xml, headers: dynamic_headers)
      )
      Securetrading::Response.new(party)
    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

3 entries across 3 versions & 1 rubygems

Version Path
securetrading-0.3.2 lib/securetrading/connection.rb
securetrading-0.3.1 lib/securetrading/connection.rb
securetrading-0.3.0 lib/securetrading/connection.rb