Sha256: bbed27e02b412a79031c56a9d41d6f967a2e73ba62ac3fd059a3d50da49d7783

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Payture::Cheques
  module Methods
    class Base
      attr_reader :config

      def initialize(config)
        @config = config
      end

      def call(**args)
        http_response = make_request(url, params(**args))
        build_response(http_response)
      end

      private

      def url
        raise NotImplementedError
      end

      def params
        raise NotImplementedError
      end

      def response_class
        raise NotImplementedError
      end

      def make_request(url, params)
        conn = Faraday.new do |builder|
          builder.adapter Faraday.default_adapter
          builder.request :url_encoded
          builder.response(:logger, config.logger) if config.logger
        end
        conn.post(url) do |req|
          req.headers['Content-Type'] = 'application/json'
          req.body = compact_hash(params).to_json
          config.logger.debug(req.body) if config.logger
          req.options.timeout = config.timeout if config.timeout
          req.options.open_timeout = config.open_timeout if config.open_timeout
        end
      rescue Faraday::Error => e
        raise Error, "#{e.class}: #{e.message}"
      end

      def build_response(http_response)
        unless http_response.success?
          raise Error, "Invalid http status: #{http_response.status}"
        end

        body =
          begin
            JSON.parse(http_response.body)
          rescue JSON::ParserError => e
            raise Error, "#{e.class}: #{e.message}"
          end

        config.logger.debug(body) if config.logger
        response_class.new(body)
      end

      def encoded_data(**params)
        params_str = compact_hash(params).map { |k, v| "#{k}=#{v}" }.join(';')
        CGI.escape(params_str)
      end

      def compact_hash(hash)
        hash.select { |k, v| k && v }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
payture-cheques-0.1.4 lib/payture/cheques/methods/base.rb
payture-cheques-0.1.3 lib/payture/cheques/methods/base.rb
payture-cheques-0.1.2 lib/payture/cheques/methods/base.rb