Module: BulkgateSmsGatewayContract::Real

Defined in:
lib/bulkgate_sms_gateway_contract/real.rb

Class Method Summary collapse

Class Method Details

.send_sms(number:, country:, body:, sender_name: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bulkgate_sms_gateway_contract/real.rb', line 4

def self.send_sms(number:, country:, body:, sender_name: nil)
  body = {
    "application_id": Rails.application.credentials.dig(:bulkgate_sms, :application_id),
    "application_token": Rails.application.credentials.dig(:bulkgate_sms, :application_token),
    "number": number,
    "text": body,
    "country": country,
  }

  if sender_name # eg 'mycompany.com'
    body.merge!({
      'sender_id' => 'gText',
      'sender_id_value' => sender_name
    })
  end

  resp = HTTParty.post('https://portal.bulkgate.com/api/1.0/simple/transactional', body: body)
  case resp.code
  when 401
    raise BulkgateSmsGatewayContract::InvalidApplicationIdOrApplicationToken
  when 400
    resp_body_hash = JSON.parse(resp.body)
    error_type = resp_body_hash['type'] 
    case error_type
    when 'invalid_phone_number'
      raise BulkgateSmsGatewayContract::InvalidPhoneNumber, error_type
    when 'empty_message'
      raise BulkgateSmsGatewayContract::EmptyMessageBody, error_type
    when 'low_credit'
      raise BulkgateSmsGatewayContract::OutOfCredit, error_type
    else
      # for more deatils see https://help.bulkgate.com/docs/en/api-error-types.html#error-types
      raise BulkgateSmsGatewayContract::BulkgateError, error_type
    end
  when 200
    resp_body_hash
  end
end