Class: OrangeSms::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/orange_sms/client.rb

Overview

Orange Api Client used to send http request to the Orange Backend

Constant Summary collapse

SUPPORTED_COUNTRIES =
{
  sen: { country: 'Senegal', code: 'SEN', prefix: 'tel:+221' },
  mli: { country: 'Mali', code: 'MLI', prefix: 'tel:+223' },
  bwa: { country: 'Botswana', code: 'BWA', prefix: 'tel:+267' },
  bfa: { country: 'Burkina Faso', code: 'BFA', prefix: 'tel:+226' },
  cod: { country: 'DR Congo', code: 'COD', prefix: 'tel:+243' },
  civ: { country: "Côte d'Ivoire / Ivory Coast", code: 'CIV', prefix: 'tel:+225' },
  egy: { country: 'Egypt', code: 'EGY', prefix: 'tel:+200' },
  jor: { country: 'Jordan', code: 'JOR', prefix: 'tel:+962' },
  gin: { country: 'Guinea Conakry', code: 'GIN', prefix: 'tel:+224' },
  ner: { country: 'Niger', code: 'NER', prefix: 'tel:+227' },
  tun: { country: 'Tunisia', code: 'TUN', prefix: 'tel:+216' },
  cmr: { country: 'Cameroon', code: 'CMR', prefix: 'tel:+237' }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_code = nil) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
# File 'lib/orange_sms/client.rb', line 23

def initialize(country_code = nil)
  @country_code = country_code.nil? ? OrangeSms.default_receiver_country_code : country_code
  @country = SUPPORTED_COUNTRIES[@country_code]
  @sender_country = SUPPORTED_COUNTRIES[OrangeSms.sender_country_code]
  @sender_phone = @sender_country[:prefix] + OrangeSms.sender_phone
end

Instance Attribute Details

#countryObject (readonly)

Returns the value of attribute country.



6
7
8
# File 'lib/orange_sms/client.rb', line 6

def country
  @country
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



6
7
8
# File 'lib/orange_sms/client.rb', line 6

def country_code
  @country_code
end

#sender_phoneObject (readonly)

Returns the value of attribute sender_phone.



6
7
8
# File 'lib/orange_sms/client.rb', line 6

def sender_phone
  @sender_phone
end

Instance Method Details

#fetch_access_tokenObject

Fetch the access token directly from your code



31
32
33
34
35
36
37
38
39
# File 'lib/orange_sms/client.rb', line 31

def fetch_access_token
  response = send_request('/oauth/v3/token',
                          'grant_type=client_credentials',
                          OrangeSms.authorization,
                          'application/x-www-form-urlencoded')
  raise OrangeSms::Error::AuthenticationError.new('Unable to fetch access token', response) if response.status != 200

  JSON.parse(response.body).fetch('access_token', nil)
end

#send_sms(receiver_phone, message) ⇒ Object

Ask Orange backend to send Sms to some number



47
48
49
50
51
52
# File 'lib/orange_sms/client.rb', line 47

def send_sms(receiver_phone, message)
  response = send_request("/smsmessaging/v1/outbound/#{sender_phone}/requests",
                          build_sms_payload(receiver_phone, message),
                          "Bearer #{fetch_access_token}", 'application/json')
  raise OrangeSms::Error::ApiError.new('Unable to Send message', response) if response.status != 201
end

#send_test_smsObject

Ask Orange backend to send test message to the sender_phone defined in the /config/initializer/orange_sms.rb



42
43
44
# File 'lib/orange_sms/client.rb', line 42

def send_test_sms
  send_sms(OrangeSms.sender_phone, "Yes ! it's working")
end