Class: Soofa

Inherits:
Object
  • Object
show all
Defined in:
lib/soofapay.rb

Constant Summary collapse

SUCCESSFUL =
200
LOGGER =
Logger.new(STDOUT)

Instance Method Summary collapse

Constructor Details

#initialize(till_no, client_secret) ⇒ Soofa

Returns a new instance of Soofa.



13
14
15
16
17
18
# File 'lib/soofapay.rb', line 13

def initialize(till_no, client_secret)
  @till_no = till_no
  @client_secret = client_secret
  @transaction = nil
  @status = nil
end

Instance Method Details

#find(tid) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/soofapay.rb', line 20

def find(tid)
  @url = "https://api.soofapay.com/v1/transactions/#{tid}/"
  headers = {"Authorization": @client_secret,
             "X-TILL": @till_no}
  begin
    response = RestClient.get(@url, headers = headers)
    data = JSON.parse(response)
    _status = data["status"]
    if response.code == SUCCESSFUL
      @transaction = Transaction.new(data)
      return true
    end
  rescue RestClient::NotFound
    LOGGER.warn("The transaction %s does not exist" % tid)
    return false
  rescue RestClient::Forbidden
    raise SoofaPermissionError.new("Your are not allowed to perform this action. Please ensure you use your correct till number and client_secret")
  end
end

#get_balanceObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/soofapay.rb', line 48

def get_balance
  @url = "http://api.soofapay.com/v1/balance/"
  headers = {"Authorization": @client_secret,
             "X-TILL": @till_no}
  begin
    response = RestClient.get(@url, headers = headers)
    data = JSON.parse(response).to_json
    return data
  rescue RestClient::Forbidden
    raise SoofaPermissionError.new("Your are not allowed to perform this action. Please ensure you use your correct till number and client_secret")
  end
end

#get_transactionObject



40
41
42
43
44
45
46
# File 'lib/soofapay.rb', line 40

def get_transaction
  if @transaction == nil
    raise Exception("A transaction is not available yet. Please ensure you call find method and verify that one exists before proceeding")
  else
    return @transaction
  end
end