Class: T2Airtime::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(user, password, url, name, params) ⇒ Request

Returns a new instance of Request.



3
4
5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#add_param(key, value) ⇒ Object



28
29
30
# File 'lib/t2_airtime/request.rb', line 28

def add_param(key, value)
  @params[key.to_sym] = value
end

#authenticateObject



21
22
23
24
25
26
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/t2_airtime/request.rb', line 36

def get?
  @params[:method] == :get
end

#keyObject



32
33
34
# File 'lib/t2_airtime/request.rb', line 32

def key
  @params[:key]
end

#post?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/t2_airtime/request.rb', line 40

def post?
  @params[:method] == :post
end

#resetObject



16
17
18
19
# File 'lib/t2_airtime/request.rb', line 16

def reset
  @params = {}
  authenticate
end

#run(method = :get) ⇒ Object

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).



57
58
59
60
61
62
# 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