Sha256: 40b4ad33684ca486395db4258282baae6a0499e0008ea8ebe734fa7277a81c75

Contents?: true

Size: 1.85 KB

Versions: 1

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(pp(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

1 entries across 1 versions & 1 rubygems

Version Path
payture-cheques-0.1.0 lib/payture/cheques/methods/base.rb