Sha256: 3ffd8a66a0046002e4406f9ff7f2f555b5a3b38f824fdc99b001d3875545ac1d

Contents?: true

Size: 1.16 KB

Versions: 23

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module CardanoWallet
  ##
  # General Utils not connected to API
  module Utils
    def self.verify_param_is_hash!(param)
      raise ArgumentError, 'argument should be Hash' unless param.is_a?(Hash)
    end

    def self.verify_param_is_array!(param)
      raise ArgumentError, 'argument should be Array' unless param.is_a?(Array)
    end

    ##
    # @param payments [Array of Hashes] - [{addr1: 1}, {addr2: 2}]
    # @return [Array of Hashes] - [{:address=>"addr1", :amount=>{:quantity=>1, :unit=>"lovelace"}}, {...}}]
    def self.format_payments(payments)
      verify_param_is_array!(payments)
      unless payments.all? { |p| p.size == 1 && p.is_a?(Hash) }
        raise ArgumentError, 'argument should be Array of single Hashes'
      end

      payments.map do |p|
        a = p.collect do |addr, amt|
          { address: addr.to_s,
            amount: { quantity: amt.to_i,
                      unit: 'lovelace' } }
        end.flatten
        Hash[*a]
      end
    end

    def self.to_query(query)
      verify_param_is_hash!(query)
      q = query.collect do |k, v|
        "#{k}=#{v}"
      end.join '&'
      "?#{q}"
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
cardano_wallet-0.3.26 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.25 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.24 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.23 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.22 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.21 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.20 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.19 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.18 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.17 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.16 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.15 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.14 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.12 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.11 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.10 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.9 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.8 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.7 lib/cardano_wallet/utils.rb
cardano_wallet-0.3.6 lib/cardano_wallet/utils.rb