Class: AfricasTalking::Base
- Inherits:
-
Object
- Object
- AfricasTalking::Base
- Defined in:
- lib/africas_talking/base.rb
Direct Known Subclasses
Constant Summary collapse
- BASE_URI =
'https://api.africastalking.com'
Instance Method Summary collapse
- #api_error_messages(response) ⇒ Object
- #build_messages_array(response) ⇒ Object
- #get(url) ⇒ Object
- #headers ⇒ Object
- #parse_api_errors(response) ⇒ Object
- #parse_api_response(response) ⇒ Object
- #post(url, json_body = nil) ⇒ Object
- #prepare_recipients(recipients) ⇒ Object
- #process_api_response(response) ⇒ Object
Instance Method Details
#api_error_messages(response) ⇒ Object
36 37 38 |
# File 'lib/africas_talking/base.rb', line 36 def (response) AfricasTalking::AfricasTalkingGatewayError#, parse_api_response(response)["SMSMessageData"]["Message"] end |
#build_messages_array(response) ⇒ Object
27 28 29 30 |
# File 'lib/africas_talking/base.rb', line 27 def (response) parsed_response = parse_api_response(response)["SMSMessageData"]["Messages"] parsed_response.collect { |msg| AfricasTalking::SMSMessage.new(msg["id"], msg["text"], msg["from"] , msg["to"], msg["linkId"], msg["date"])} end |
#get(url) ⇒ Object
16 17 18 19 |
# File 'lib/africas_talking/base.rb', line 16 def get(url) response = Typhoeus.post("#{BASE_URI}#{url}", headers: headers) response.[:response_code] == 200 ? (response) : (response) end |
#headers ⇒ Object
40 41 42 |
# File 'lib/africas_talking/base.rb', line 40 def headers {'Accept' => "application/json", 'apiKey'=> 'c6ec656ed9bceb689289ccaa6e38d7f6c1b0718a1237b4c13391d1efc1108bfb'} end |
#parse_api_errors(response) ⇒ Object
21 22 23 24 25 |
# File 'lib/africas_talking/base.rb', line 21 def parse_api_errors(response) reports = parse_api_response(response)["SMSMessageData"]["Recipients"] return reports.collect {|entry| AfricasTalking::StatusReport.new(entry["number"], entry["status"], entry["cost"])} # return parsed_api_errors end |
#parse_api_response(response) ⇒ Object
32 33 34 |
# File 'lib/africas_talking/base.rb', line 32 def parse_api_response(response) JSON.parse(response.body) end |
#post(url, json_body = nil) ⇒ Object
11 12 13 14 |
# File 'lib/africas_talking/base.rb', line 11 def post(url, json_body=nil) response = Typhoeus.post("#{BASE_URI}#{url}", body: json_body, headers: headers) process_api_response(response) end |
#prepare_recipients(recipients) ⇒ Object
5 6 7 8 9 |
# File 'lib/africas_talking/base.rb', line 5 def prepare_recipients(recipients) recipients_array = [] recipients.split(',').each { |recip| recipients_array << recip.strip.gsub(/^0/,'254') } recipients_array.join(', ') end |
#process_api_response(response) ⇒ Object
44 45 46 47 48 |
# File 'lib/africas_talking/base.rb', line 44 def process_api_response(response) return parse_api_response(response) if response.[:response_code] == 200 return parse_api_errors(response) if response.[:response_code] == 201 raise (response) end |