class Vodacom::Client

Constants

BALANCE_PATH
LOGIN_PATH

include HTTParty_with_cookies

Attributes

base_path[R]
expiry[R]
login_response[R]
password[R]
port[R]
username[R]

Public Class Methods

api_version() click to toggle source

This is the version of the API docs this client was built off-of

# File lib/vodacom/client.rb, line 28
def self.api_version
  'v3 2021-03-13'
end
compatible_api_version() click to toggle source
# File lib/vodacom/client.rb, line 23
def self.compatible_api_version
  'v3'
end
new(username:, password:, base_path: 'https://www.vodacom.co.za/cloud', port: 80) click to toggle source
# File lib/vodacom/client.rb, line 16
def initialize(username:, password:, base_path: 'https://www.vodacom.co.za/cloud', port: 80)
  @username = username
  @password = password
  @base_path = base_path
  @port = port
end

Public Instance Methods

balance(phone_number) click to toggle source
# File lib/vodacom/client.rb, line 32
def balance(phone_number)
  start_time = get_micro_second_time

  login unless (raw_cookie && (expiry.to_i > Time.now.to_i))
  response = HTTParty.send(
    :get,
    "#{BALANCE_PATH}#{phone_number}",
    headers: {
      'Content-Type': 'application/json',
      'Accept': '*/*',
      'Cookie': process_cookies
    },
    port: port
  )

  end_time = get_micro_second_time
  construct_response_object(response, BALANCE_PATH, start_time, end_time)
end

Private Instance Methods

body_is_missing?(response) click to toggle source
# File lib/vodacom/client.rb, line 118
def body_is_missing?(response)
  response.body.nil? || response.body.empty?
end
body_is_present?(response) click to toggle source
# File lib/vodacom/client.rb, line 114
def body_is_present?(response)
  !body_is_missing?(response)
end
construct_base_path(path, params) click to toggle source
# File lib/vodacom/client.rb, line 164
def construct_base_path(path, params)
  constructed_path = "#{base_path}/#{path}"

  if params == {}
    constructed_path
  else
    "#{constructed_path}?#{process_params(params)}"
  end
end
construct_metadata(response, start_time, end_time) click to toggle source
# File lib/vodacom/client.rb, line 104
def construct_metadata(response, start_time, end_time)
  total_time = end_time - start_time

  {
    'start_time' => start_time,
    'end_time' => end_time,
    'total_time' => total_time
  }
end
construct_response_object(response, path, start_time, end_time) click to toggle source
end_time = get_micro_second_time
construct_response_object(response, path, start_time, end_time)

end

# File lib/vodacom/client.rb, line 96
def construct_response_object(response, path, start_time, end_time)
  {
    'body' => parse_body(response, path),
    'headers' => response.headers,
    'metadata' => construct_metadata(response, start_time, end_time)
  }
end
get_micro_second_time() click to toggle source
# File lib/vodacom/client.rb, line 160
def get_micro_second_time
  (Time.now.to_f * 1000000).to_i
end
login() click to toggle source
# File lib/vodacom/client.rb, line 53
def login
  start_time = get_micro_second_time

  response = HTTParty.send(
    :post,
    LOGIN_PATH,
    body: { username: username, password: password }.to_json,
    headers: {
      'Content-Type': 'application/json',
      'Authority': 'www.vodacom.co.za',
      'x-application-context': 'vod-ms-zuul-gateway-crd:prodcluster:8091',
      'Accept': '*/*'
    },
    port: port
  )

  end_time = get_micro_second_time

  @login_response = construct_response_object(response, LOGIN_PATH, start_time, end_time)

  @raw_cookie = @login_response.dig("headers").dig("set-cookie")
  _, @expiry = parse_cookie(raw_cookie, 'OAuth2AppsTokenDB')
end
parse_body(response, path) click to toggle source
# File lib/vodacom/client.rb, line 122
def parse_body(response, path)
  parsed_response = JSON.parse(response.body) # Purposely not using HTTParty

  if parsed_response.dig(path.to_s)
    parsed_response.dig(path.to_s)
  else
    parsed_response
  end
rescue JSON::ParserError => _e
  response.body
end
process_cookies() click to toggle source
# File lib/vodacom/client.rb, line 155
def process_cookies
  # Cookies are always a single string separated by spaces
  raw_cookie.map { |item| item.split('; ').first }.join('; ')
end
process_params(params) click to toggle source
# File lib/vodacom/client.rb, line 174
def process_params(params)
  params.keys.map { |key| "#{key}=#{params[key]}" }.join('&')
end