Sha256: 419390581f7362c1bec922f2e4f08be4068606a9417c6d9a7a7c93fa59b89c31

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'faraday'

module Cryptomus
  class Connection
    MIME_TYPE = 'application/json'

    # @param path [String]
    # @param body [Hash, nil]
    # @raise [Cryptomus::Errors::ApiError]
    def post(path, body: nil, query: nil)
      response = connection.post do |req|
        req.url(path, query)
        raw_body = body&.to_json
        req.body = raw_body
        req[:sign] = Signature.generate(raw_body)
      end
      handle_response(response)
      response.body
    end

    private

    def connection
      Faraday.new(connection_options) do |builder|
        builder.adapter Faraday.default_adapter
        builder.request :json
        builder.response :json, parser_options: { symbolize_names: true }
        builder.response :logger, Cryptomus.config.logger, bodies: true if Cryptomus.config.logger
        Cryptomus.config.connection_config&.call(builder)
      end
    end

    def handle_response(response)
      return if response.success?
      return if Cryptomus.config.handle_response&.call(response)

      raise Cryptomus::Errors::ApiError, response
    end

    def connection_options
      {
        url: Cryptomus::CONST::URL,
        headers: {
          'Content-Type' => MIME_TYPE,
          merchant: Cryptomus.config.merchant_id,
          user_agent: Cryptomus.config.user_agent
        }
      }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cryptomus-0.2.1 lib/cryptomus/connection.rb
cryptomus-0.2.0 lib/cryptomus/connection.rb
cryptomus-0.1.1 lib/cryptomus/connection.rb
cryptomus-0.1.0 lib/cryptomus/connection.rb