class Libriciel::ApiXivo

 Class ApiXivo to use Xivo Api

Public Class Methods

add_association(line_id, device_id, token) click to toggle source

 Ajout d'une association entre un numéro et un téléphone

Attention ERROR 36 si deja associé à un device
# File lib/libriciel.rb, line 261
def self.add_association(line_id, device_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines/#{line_id}/devices/#{device_id}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Put.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'


  response = http.request(request)
  unless response.read_body.to_s.strip.empty?
    STDERR.puts("Line is already associated to a Device")
    exit(36)
  end
  sip_id_from_line = Libriciel::ApiXivo.get_sip_id_from_line(line_id, token)

  sip_del_options = Libriciel::ApiXivo.dell_webrtc_of_sip(sip_id_from_line, token)

end
add_new_user(csv_entry, token) click to toggle source

 Ajotu d'un nouvel utilisateurs avec création de son numéro (get_free_sip)  et passage du password dans le csv_entry

# File lib/libriciel.rb, line 323
def self.add_new_user(csv_entry, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/users/import")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(url)
  request["x-auth-token"] = "#{token}"
  request["content-type"] = 'text/csv; charset=utf-8'
  request["cache-control"] = 'no-cache'
  request.body = csv_entry

  response = http.request(request)
end
add_webrtc_of_sip(sip_id, token) click to toggle source
# File lib/libriciel.rb, line 410
def self.add_webrtc_of_sip(sip_id, token)
  options = Libriciel::ApiXivo.get_sip_options(sip_id, token)
  if !options.include?("webrtc")
    options_plus_webrtc = "{\n \"options\": #{options.gsub(/\]\n\]$/, "],\n            \[\n            \"webrtc\",\n            \"yes\"\n            ]\n]")}}"

  else
    STDERR.puts("webrtc already configured")
    exit 1
  end

  url = URI("https://xivo.libriciel.fr:9486/1.1/endpoints/sip/#{sip_id}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Put.new(url)
  request["x-auth-token"] = "#{token}"
  request["content-type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request.body = "#{options_plus_webrtc}"

  response = http.request(request)

end
del_association(line_id, device_id, token) click to toggle source

Suppression d'une associtaiton ligne / numéro Attention ERROR 36 si pas associé à un device

# File lib/libriciel.rb, line 235
def self.del_association(line_id, device_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines/#{line_id}/devices/#{device_id}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Delete.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'


  response = http.request(request)

  unless response.read_body.to_s.strip.empty?
    STDERR.puts("Line is not associated with Device")
    exit(36)
  end
  sip_id_from_line = Libriciel::ApiXivo.get_sip_id_from_line(line_id, token)

  sip_add_options = Libriciel::ApiXivo.add_webrtc_of_sip(sip_id_from_line, token)

end
dell_webrtc_of_sip(sip_id, token) click to toggle source
# File lib/libriciel.rb, line 386
def self.dell_webrtc_of_sip(sip_id, token)
  options = Libriciel::ApiXivo.get_sip_options(sip_id, token)
  if options.include?("webrtc")
    options_less_webrtc = "{\n \"options\": #{options.gsub(/\,\n            \[\n            \"webrtc\"\,\n            \"yes\"\n            \]/, '')}}"

  else
  end

  url = URI("https://xivo.libriciel.fr:9486/1.1/endpoints/sip/#{sip_id}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Put.new(url)
  request["x-auth-token"] = "#{token}"
  request["content-type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request.body = "#{options_less_webrtc}"

  response = http.request(request)

end
export_all_users_to_csv(xivo_csv_file, token) click to toggle source

 Export en csv de tous les utilisateurs xivo

# File lib/libriciel.rb, line 302
def self.export_all_users_to_csv(xivo_csv_file, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/users/export")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'


  response = http.request(request)

  File.open("#{xivo_csv_file}", "w") {|f| f.write(response.read_body)}
  return xivo_csv_file

end
get_device_id_from_line(line_id, token) click to toggle source

 Obtention de l'ID du téléphone avec l'ID de la ligne

# File lib/libriciel.rb, line 166
def self.get_device_id_from_line(line_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines/#{line_id}/devices")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  device_id = JSON.parse(response.read_body)

  return device_id["device_id"]
end
get_device_id_from_mac(mac, token) click to toggle source

 Obtention de l'ID d'un téléphone avec sa mac

# File lib/libriciel.rb, line 209
def self.get_device_id_from_mac(mac, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/devices")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)

  json["items"].each do |devices|
    if devices["mac"].to_s == mac.to_s
      return devices["id"]

    else
    end
  end

end
get_device_mac_from_id(device_id, token) click to toggle source

 Obtention de la mac du téléphone avec l'ID de ce dernier

# File lib/libriciel.rb, line 184
def self.get_device_mac_from_id(device_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/devices")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)

  json["items"].each do |devices|
    if devices["id"].to_s == device_id.to_s
      return devices["mac"]

    else
    end
  end

end
get_devices(token) click to toggle source

 Liste complète detous les devices avec leurs infos (format ouput: json)

# File lib/libriciel.rb, line 123
def self.get_devices(token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/devices")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)
  puts json["items"]
end
get_free_devices(token) click to toggle source

 Liste de tous les devices qui sont soit en autoprov (avec leur mac)  soit en not_configured

# File lib/libriciel.rb, line 65
def self.get_free_devices(token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/devices")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)

  json = JSON.parse(response.read_body)
  json["items"].each do |devices|
    if devices["status"] == "autoprov"
      puts "\nThe Device #{devices["id"]} is #{devices["status"]}"
      puts "The Devices #{devices["id"]} mac is #{devices["mac"]}\n"
    elsif devices["status"] == "not_configured"
      puts "\nThe Device #{devices["id"]} is #{devices["status"]}"
      puts "The Devices #{devices["id"]} mac is #{devices["mac"]}\n"
    end
  end

end
get_free_sip(min_sip, max_sip, token) click to toggle source

Fonction qui donne un numéro SIP disponible pour un nouveau user

# File lib/libriciel.rb, line 92
def self.get_free_sip(min_sip, max_sip, token)
  sip_number = min_sip
  array_numbers = []
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)
  json2 = json["items"].to_a
  json2.each do |number_check|
    array_numbers << number_check["caller_id_num"]
  end

  while sip_number < max_sip
    if array_numbers.include?(sip_number.to_s)
      sip_number += 1
    else
      return sip_number

    end
  end
end
get_line_id_from_number(number, token) click to toggle source

 Obtention de l'ID de la ligne avec le numéro de tel

# File lib/libriciel.rb, line 140
def self.get_line_id_from_number(number, token)
  line_id = 0
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)
  json2 = json["items"].to_a
  json2.each do |number_check|
    if number_check["caller_id_num"].to_i == number.to_i
      line_id = number_check["id"]
    else
    end

  end
  return line_id
end
get_sip_id_from_line(line_id, token) click to toggle source

 Recherche du sip_id en fonction du numéro en entrée

# File lib/libriciel.rb, line 341
def self.get_sip_id_from_line(line_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/lines/#{line_id}/endpoints/sip")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)

  return json["endpoint_id"]
end
get_sip_options(sip_id, token) click to toggle source

 Recherche des options du numéro SIP (à faire : traitement pour ajout et enlevement webrtc)

# File lib/libriciel.rb, line 359
def self.get_sip_options(sip_id, token)
  options_line = "[\n        "
  url = URI("https://xivo.libriciel.fr:9486/1.1/endpoints/sip/#{sip_id}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  json = JSON.parse(response.read_body)
  json["options"].each do |options|
    if options[0] == "callerid"
      options[1] = options[1].gsub(/\"/, "\\\"")

      options_line << "[\n            \"#{options[0]}\",\n            \"#{options[1]}\"\n            ],\n            "
    else
      options_line << "[\n            \"#{options[0]}\",\n            \"#{options[1]}\"\n            ],\n            "
    end
  end

  return options_line.gsub(/,\n            $/, "\n]\n")
end
synchro_device_id(device_id, token) click to toggle source

 Synchronisation du device avec son ID

# File lib/libriciel.rb, line 285
def self.synchro_device_id(device_id, token)
  url = URI("https://xivo.libriciel.fr:9486/1.1/devices/#{device_id}/synchronize")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["x-auth-token"] = "#{token}"
  request["cache-control"] = 'no-cache'

  response = http.request(request)
  puts response.read_body

end
token_generate(base64_enc) click to toggle source

 Génération du token permettant l'authent ADMIN (ATTENTION) pour l'api Xivo

# File lib/libriciel.rb, line 36
def self.token_generate(base64_enc)
  url = URI("https://xivo.libriciel.fr:9497/0.1/token")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(url)
  request["content-type"] = 'application/json'
  request["authorization"] = "Basic #{base64_enc}"
  request["cache-control"] = 'no-cache'

  request.body = "{\n  \"backend\": \"xivo_admin\",\n  \"expiration\": 100\n}"

  response = http.request(request)
  reponse_parsing = JSON.parse(response.read_body)

  if !reponse_parsing.include?("data")
    puts reponse_parsing["reason"]
    exit 223
  else
    token = reponse_parsing['data']['token']
  end

  return token
end
token_validation(token) click to toggle source
# File lib/libriciel.rb, line 13
def self.token_validation(token)
  url = URI("https://xivo.libriciel.fr:9497/0.1/token/#{token}")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Head.new(url)
  request["content-type"] = 'application/json'
  request["cache-control"] = 'no-cache'

  response = http.request(request)

  if response.message == "NO CONTENT"
    return true

  else
    return false

  end
end