Sha256: 7d7123aa347874a81dbcefc31d42ec287007f2e3fe777729196817973b79ab59

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'securerandom'
require 'faraday/multipart'

module Crefo
  class Service
    class Request
      attr_reader :request_id, :options

      def initialize(request_id: nil, **options)
        @request_id = request_id || Crefo.config.transactionreference || generate_request_id
        @options = options
      end

      def envelope
        @envelope ||= XML::Request::Envelope.new(request: self).build
      end

      def send(url)
        connection.post url do |reqest|
          reqest.headers[:content_type] = 'application/xop+xml'
          reqest.body = envelope
        end
      end

      def connection
        @connection ||= begin
          options = Crefo.config.connection_options
          Faraday.new(options) do |connection|
            connection.headers[:user_agent] = user_agent
            connection.request :multipart
            connection.adapter :net_http
          end
        end
      end

      def transmissiontimestamp
        self.class.transmissiontimestamp || Time.now
      end

      private

      def user_agent
        "#{Crefo.config.clientapplicationname} v#{Crefo.config.clientapplicationversion}"
      end

      def generate_request_id
        SecureRandom.hex[0, 25]
      end

      class << self
        attr_accessor :response_class
        attr_accessor :request_name

        @@transmissiontimestamp = nil

        def transmissiontimestamp
          @@transmissiontimestamp
        end

        def mock_transmissiontimestamp(time = Time.now, &block)
          @@transmissiontimestamp = time
          block.call
          @@transmissiontimestamp = nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crefo-0.3.0 lib/crefo/service/request.rb