class T2Airtime::Request

Public Class Methods

new(user, password, url, name, params) click to toggle source
# File lib/t2_airtime/request.rb, line 3
def initialize(user, password, url, name, params)
  @user   = user || ''
  @pass   = password || ''
  @conn = Faraday.new(url: url) do |faraday|
    faraday.request  :url_encoded
    faraday.adapter  :net_http
  end
  reset
  @name = name
  add_param :action, name
  @params.merge!(params)
end

Public Instance Methods

add_param(key, value) click to toggle source
# File lib/t2_airtime/request.rb, line 28
def add_param(key, value)
  @params[key.to_sym] = value
end
authenticate() click to toggle source
# File lib/t2_airtime/request.rb, line 21
def authenticate
  time = Time.now.to_i.to_s
  add_param :key,   time
  add_param :md5,   md5_hash(@user + @pass + time)
  add_param :login, @user
end
get?() click to toggle source
# File lib/t2_airtime/request.rb, line 36
def get?
  @params[:method] == :get
end
key() click to toggle source
# File lib/t2_airtime/request.rb, line 32
def key
  @params[:key]
end
post?() click to toggle source
# File lib/t2_airtime/request.rb, line 40
def post?
  @params[:method] == :post
end
reset() click to toggle source
# File lib/t2_airtime/request.rb, line 16
def reset
  @params = {}
  authenticate
end
run(method = :get) click to toggle source

More than 99.5% of the transactions are currently processed within a few seconds. However, it may happen in some rare cases that a transaction takes longer to be processed by the receiving operator due to congested system on their end for instance. TransferTo guarantees that transactions not processed within 600 seconds will not be charged, whatever the final status of the transaction (successful or not). In addition, TransferTo is operating a real time system; therefore, the status returned in the Top-up response is final and will not change. To ensure that your system captures successfully the status of all transactions especially the longest ones, it is advised to either set up a high timeout value of 600seconds to be on the safe side (TCP connection could potentially be opened for a long time in this case) or to set up a mechanism to check on the status (to do so, you can call the trans_info method to retrieve the final status of a transaction). In case of timeout, both methods reserve_id and get_id_from_key of the API could be useful to identify the ID of the transaction (we recommend to either send a reserve_id request and to use the ID returned in the response in the subsequent top-up request so that you know in advance the ID of the transaction).

# File lib/t2_airtime/request.rb, line 57
def run(method = :get)
  add_param :method, method
  @conn.send(method, "/#{T2Airtime::ENDPOINT}", @params) do |req|
    req.options = { timeout: 600, open_timeout: 600 }
  end
end

Private Instance Methods

md5_hash(str) click to toggle source
# File lib/t2_airtime/request.rb, line 66
def md5_hash(str)
  (Digest::MD5.new << str).to_s
end