module Wifimap::ParserFactory

Instantiate the correct parser class according to the dump format.

Public Class Methods

create(file_content) click to toggle source

Instantiate the parser factory or raise an error if the dump is not supported.

# File lib/wifimap/parser_factory.rb, line 9
def create(file_content)
  case dump_format(file_content)
  when :airodump
    Wifimap::Parser::Airodump.new(file_content)
  when :sniff_probes
    Wifimap::Parser::SniffProbes.new(file_content)
  else
    raise Error, 'Unsupported dump format'
  end
end
dump_format(file_content) click to toggle source

Check the file content and identify the correct dump format.

@param [String] file_content @return [Symbol|Nil]

# File lib/wifimap/parser_factory.rb, line 24
def dump_format(file_content)
  return :airodump if airodump_format?(file_content)
  return :sniff_probes if sniff_probes_format?(file_content)
end

Private Class Methods

airodump_format?(file_content) click to toggle source

@param [String] file_content @return [Boolean]

# File lib/wifimap/parser_factory.rb, line 31
        def airodump_format?(file_content)
  airodump_header = 'BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, Authentication, Power'
  file_content.include?(airodump_header)
end
sniff_probes_format?(file_content) click to toggle source

@param [String] file_content @return [Boolean]

# File lib/wifimap/parser_factory.rb, line 38
        def sniff_probes_format?(file_content)
  file_content.split("\n").all? do |row|
    fields = row.split
    fields[1].include?('dBm') && Wifimap::Mac.valid?(fields[2])
  end
end