module Billing

Constants

VERSION

Public Instance Methods

call_type(tpserv) click to toggle source

Método que define o tipo de serviço.

# File lib/billing.rb, line 27
def call_type tpserv
  type = nil
  case tpserv
  when "Chamadas Locais para Celulares TIM", "Chamadas Locais para Outros Celulares", "Chamadas Locais para Outros Telefones Fixos", "Chamadas Tarifa Zero"
     type = :local
   when "Chamadas Longa Distância: TIM LD 41", "Chamadas Longa Distância: Brasil Telecom", "Chamadas Longa Distância: Telemar", "Chamadas Longa Distância: Embratel", "Chamadas Longa Distância: Telefônica"
     type = :long_distance
   when "TIM Torpedo", "TIM VideoMensagem/FotoMensagem para Celular"
     type = :sms
   when "TIM Wap Fast", "TIM Connect Fast"
     type = :internet
  end
end
execute(arq = " click to toggle source

Método de execução do sistema

# File lib/billing.rb, line 89
def execute (arq = "#{ARGV[0]}", tel = "#{ARGV[1]}")
  # Executando método para validação do arquivo.
  validate arq
  if validate == true
    # Encontrando as words relacionadas ao numero
    rows_number = find_by_number arq, tel
    # Fazendo os calculos
    total = sum_bill rows_number
    # Imprimindo
    show_formated_result total
  else
    puts "Arquivo inválido - Só é aceito arquivos que tenham delimitadores."
  end
end
find_by_number(arq = " click to toggle source

Método que encontra os resultados no arquivo passado como parâmetro.

# File lib/billing.rb, line 16
def find_by_number (arq = "#{ARGV[0]}", number = "#{ARGV[1]}")
  results = []
  CSV.foreach(arq, {:col_sep => ';'}) do |row|
    if row[3] == number
      results << row
    end
  end
  results
end
show_formated_result(total) click to toggle source

Método para exibir o resultado formatado ao cliente.

# File lib/billing.rb, line 74
def show_formated_result total
  puts "============ Resultados de sua conta telefônica ========================"
  sleep 1
  puts "Total de minutos em chamadas locais: #{total[:local].round(2)} minutos"
  sleep 1
  puts "Total de minutos em chamadas de longa distância: #{total[:long_distance].round(2)} minutos"
  sleep 1
  puts "Total de SMS enviados: #{total[:sms]}"
  sleep 1
  puts "Total de bytes trafegados de Internet: #{total[:internet].round()} bytes"
  sleep 1
  puts "============ Obrigado por usar o Billing - By Pery Lemke ==============="
end
string_to_bytes(byte_string) click to toggle source

Método que converte string para float e realiza o cálculo de bytes.

# File lib/billing.rb, line 66
def string_to_bytes byte_string
  return 0 if byte_string.nil?
    byte = byte_string.split(" ")
    byte_float = byte[0].to_f
    byte_float * 1000
end
string_to_minutes(time_string) click to toggle source

Método que converte string para float e realiza o cálculo de minutos.

# File lib/billing.rb, line 58
def string_to_minutes time_string
  return 0 if time_string.nil?
    minutes = time_string.split("m").first.to_f
    seconds = time_string.split("m").last.split("s").last.to_f
    minutes + seconds / 60
end
sum_bill(calls) click to toggle source

Método que realiza as somas para exibição na tela do usuário.

# File lib/billing.rb, line 42
def sum_bill calls
  results = {local: 0, long_distance: 0, sms: 0, internet: 0}
  calls.each do |call|
    type = call_type call[6]
    if type == :local or type == :long_distance
      results[type] += string_to_minutes call[13]
    elsif type == :sms
      results[type] += 1
    elsif type == :internet
      results[type] += string_to_bytes call[13]
    end
  end
  results
end
validate(arq = " click to toggle source

Método para validação do arquivo.

# File lib/billing.rb, line 10
def validate (arq = "#{ARGV[0]}")
  validator = Csvlint::Validator.new( File.new("#{arq}" ))
  validator.valid?
end