class Bitpagos::Client

Constants

API_BASE
COMPLETED
PARTIALLY_PAID
PENDING
WAITING

Attributes

api_base[RW]
headers[RW]

Public Class Methods

new(api_key) click to toggle source
# File lib/bitpagos/client.rb, line 11
def initialize(api_key)
  if api_key.to_s.empty?
    raise Bitpagos::Errors::InvalidApiKey.new("No API key provided")
  end

  @api_key = "ApiKey #{api_key}".freeze
  @headers = { authorization: @api_key, content_type: :json, accept: :json }
  @api_base = API_BASE
end

Public Instance Methods

completed_transactions() click to toggle source
# File lib/bitpagos/client.rb, line 40
def completed_transactions
  retrieve_transactions(status: COMPLETED)
end
get_transaction(transaction_id) click to toggle source
# File lib/bitpagos/client.rb, line 21
def get_transaction(transaction_id)
  retrieve_transactions(nil, transaction_id)
end
partially_paid_transactions() click to toggle source
# File lib/bitpagos/client.rb, line 52
def partially_paid_transactions
  retrieve_transactions(status: PARTIALLY_PAID)
end
pending_transactions() click to toggle source
# File lib/bitpagos/client.rb, line 48
def pending_transactions
  retrieve_transactions(status: PENDING)
end
transaction_count() click to toggle source

Returns the total count of transactions in all states.

@return [Integer] Total transaction count

# File lib/bitpagos/client.rb, line 59
def transaction_count
  transactions["meta"]["total_count"]
end
transactions(params = {}) click to toggle source

Retrieve all transactions, or specify them with parameters, to retrieve allow retrieving paginated transactions.

@param [Hash] Allows to specify transaction type, limit or offset @option [Symbol] status (:pending, :waiting, :completed, :partially_paid) @option [Integer] offset @option [Integer] limit @return [Hash]

# File lib/bitpagos/client.rb, line 33
def transactions(params = {})
  if status = params[:status]
    params[:status] = get_transaction_type_from_symbol(status)
  end
  retrieve_transactions(params)
end
waiting_transactions() click to toggle source
# File lib/bitpagos/client.rb, line 44
def waiting_transactions
  retrieve_transactions(status: WAITING)
end

Private Instance Methods

get_transaction_type_from_symbol(transaction_type) click to toggle source

Takes a symbol and returns the proper transaction type.

@param [Symbol] Can be :pending, :waiting, :completed or :partially_paid @return [String,nil] Returns the corresponding “PE”, “WA”, “CO” or “PP”

# File lib/bitpagos/client.rb, line 69
def get_transaction_type_from_symbol(transaction_type)
  begin
    target_type = transaction_type.to_s.upcase
    return if target_type.empty?
    self.class.const_get(target_type)
  rescue NameError => error
    raise Bitpagos::Errors::InvalidTransactionType.new(error.message)
  end
end
retrieve_transactions(query = nil, transaction_id = nil) click to toggle source

Hits the Bitpagos transaction API, returns a hash with results

@param [String] State (Pending, Waiting, Completed, Partially Paid) @param [String] Transaction ID @return [Hash]

# File lib/bitpagos/client.rb, line 84
def retrieve_transactions(query = nil, transaction_id = nil)
  headers.merge!(params: query) if query
  url = "#{API_BASE}/transaction/#{transaction_id}"
  begin
    response = RestClient.get(url, headers)
    JSON.parse(response)
  rescue RestClient::Unauthorized => error
    raise Bitpagos::Errors::Unauthorized.new(error.message)
  end
end