Class: Infobeep::Client

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

Constant Summary collapse

API_BASE_URL =
'https://api.infobip.com/'

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/infobeep/client.rb', line 10

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#authentication_headersObject



31
32
33
34
# File 'lib/infobeep/client.rb', line 31

def authentication_headers
  auth_string = ::Base64.encode64("#{@username}:#{@password}").strip
  {'Authorization': "Basic #{auth_string}"}
end

#client_headersObject



36
37
38
39
40
41
42
43
44
# File 'lib/infobeep/client.rb', line 36

def client_headers
  hsh = {
      'User-Agent': "Infobeep-#{Infobeep::VERSION}",
      'Content-Type': 'application/json',
      'accept': 'application/json'
  }
  hsh.merge!(authentication_headers)
  hsh
end

#send_request(sms_request) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/infobeep/client.rb', line 15

def send_request(sms_request)
  url = API_BASE_URL + sms_request.route
  method = sms_request.http_method
  headers = client_headers.merge(sms_request.headers)
  payload = sms_request.payload

  begin
    response = RestClient::Request.execute(method: method, url: url, payload: payload, headers: headers)
  rescue RestClient::ExceptionWithResponse => err
    raise err
  end

  response_hash = JSON.parse(response.body)
  sms_request.response_class.new(response_hash)
end