Class: Arafa::AfricasTalking

Inherits:
Providers::Base show all
Defined in:
lib/arafa/providers/africas_talking.rb

Overview

Africa's Talking Bulk SMS adapter (ยง1.1). A single HTTP 200 can still carry a mix of successful and failed recipients, so success/failure is read out of each entry's statusCode in SMSMessageData.Recipients, never assumed from the HTTP status alone.

Constant Summary collapse

CONTRACT =
Contracts::AfricasTalkingContract
SUCCESS_STATUS_CODES =

100 Processed, 101 Sent, 102 Queued.

[100, 101, 102].freeze

Constants inherited from Providers::Base

Providers::Base::TO_TYPE

Instance Method Summary collapse

Methods inherited from Providers::Base

connection, #connection, new, send, #send

Instance Method Details

#build_payloadObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/arafa/providers/africas_talking.rb', line 36

def build_payload
  payload = {
    username: username,
    phoneNumbers: to,
    message: text,
    senderId: from
  }
  payload[:enqueue] = enqueue unless enqueue.nil?
  payload
end

#endpointObject



20
21
22
23
24
25
26
# File 'lib/arafa/providers/africas_talking.rb', line 20

def endpoint
  if Arafa.config.africas_talking.sandbox
    "https://api.sandbox.africastalking.com/version1/messaging"
  else
    "https://api.africastalking.com/version1/messaging/bulk"
  end
end

#headersObject



28
29
30
31
32
33
34
# File 'lib/arafa/providers/africas_talking.rb', line 28

def headers
  {
    "apiKey" => Arafa.config.africas_talking.api_key,
    "Content-Type" => "application/json",
    "Accept" => "application/json"
  }
end

#parse_response(response) ⇒ Object



47
48
49
50
51
# File 'lib/arafa/providers/africas_talking.rb', line 47

def parse_response(response)
  return build_result(response.body) if response.status.between?(200, 299)

  Failure(error_class_for(response.status).new(error_message(response)))
end