Class: Arafa::Airtime::AfricasTalking

Inherits:
Airtime::Base
  • Object
show all
Defined in:
lib/arafa/airtime/africas_talking.rb

Overview

Africa's Talking airtime request adapter (§1.10). A single HTTP 200 can still carry a mix of Sent/Failed recipients (read from responses), or reject the whole request via a top-level errorMessage with no responses at all (e.g. an unconfigured/invalid username) — both cases are handled, unlike the AT SMS adapter which only needs the former.

Constant Summary collapse

CONTRACT =
Contracts::AfricasTalkingAirtimeContract

Instance Method Summary collapse

Instance Method Details

#build_payloadObject



35
36
37
38
39
40
41
42
43
# File 'lib/arafa/airtime/africas_talking.rb', line 35

def build_payload
  payload = {
    username: username,
    recipients: phone_number.map { |number| { phoneNumber: number, amount: "#{currency_code} #{amount}" } }
  }
  payload[:maxNumRetry] = max_num_retry unless max_num_retry.nil?
  payload[:requestMetadata] =  unless .nil?
  payload
end

#endpointObject



21
22
23
# File 'lib/arafa/airtime/africas_talking.rb', line 21

def endpoint
  "https://api.africastalking.com/version1/airtime/send"
end

#headersObject



25
26
27
28
29
30
31
32
33
# File 'lib/arafa/airtime/africas_talking.rb', line 25

def headers
  headers = {
    "apiKey" => Arafa.config.africas_talking.api_key,
    "Content-Type" => "application/json",
    "Accept" => "application/json"
  }
  headers["Idempotency-Key"] = idempotency_key unless idempotency_key.nil?
  headers
end

#parse_response(response) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/arafa/airtime/africas_talking.rb', line 45

def parse_response(response)
  return build_failure(response) unless response.status.between?(200, 299)

  body = response.body
  entries = Array(body["responses"])
  return Failure(Arafa::InvalidRequestError.new(body["errorMessage"] || body.to_s)) if entries.empty?

  build_success(body, entries)
end