Class: CertOpenDataVisualizer::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cert_open_data_visualizer/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



5
6
7
8
# File 'lib/cert_open_data_visualizer/formatter.rb', line 5

def initialize
  @visualizer = Visualize.new
  @cacher = @visualizer.cacher
end

Instance Attribute Details

#cacherObject

Returns the value of attribute cacher.



3
4
5
# File 'lib/cert_open_data_visualizer/formatter.rb', line 3

def cacher
  @cacher
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/cert_open_data_visualizer/formatter.rb', line 3

def data
  @data
end

#visualizerObject

Returns the value of attribute visualizer.



3
4
5
# File 'lib/cert_open_data_visualizer/formatter.rb', line 3

def visualizer
  @visualizer
end

Instance Method Details

#filter_by_city(name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cert_open_data_visualizer/formatter.rb', line 49

def filter_by_city(name)
  # get everything matching
  load_data! if @data.nil?
  filterd = @data.select do |line|
    line[8].downcase.include? name
  end

  # create hash for formatting
  results = generate_city_hash(filterd)
  # print in nice format
  print_city_results(results)
end

#first_formatObject

0: date from 1: date to 2: anon AS-number 3: anon IP-address 4: IP version 5: maincategory 6: subcategory 7: cc 8: city



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cert_open_data_visualizer/formatter.rb', line 19

def first_format
  if @cacher.file_exists?("first_challenge")
    return File.read(@cacher.get_from_cache("first_challenge"))
  else
    load_data! if @data.nil?
  end
  incidents = count_main_incidents
  incidents_str = ""
  incidents.each do |incident|
    incidents_str <<  "#{incident[0]} #{incident[1]}\n"
  end
  @cacher.write_file_to_cache("first_challenge", incidents_str)
  incidents_str
end

#generate_city_hash(list) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cert_open_data_visualizer/formatter.rb', line 62

def generate_city_hash(list)
  results = {}
  list.each do |l|
    country, city, main, sub = l[7], l[8], l[5], l[6]
    if results[country].nil?
      results[country] = { city => { main => { sub => 1 } }}
    elsif results[country][city].nil?
      results[country][city] = {main => { sub => 1 } }
    elsif results[country][city][main].nil?
      results[country][city][main] = { sub => 1 }
    elsif results[country][city][main][sub].nil?
      results[country][city][main][sub] = 1
    else
      results[country][city][main][sub] += 1
    end
  end
  results
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cert_open_data_visualizer/formatter.rb', line 81

def print_city_results(results)
  results.keys.each do |country|
    puts "#{country}:"
    next if results[country].nil?
    results[country].keys.each do |city|
      puts "  #{city}:"
      next if results[country][city].nil?
      results[country][city].keys.each do |main|
        puts "    #{main}:"
        next if results[country][city][main].nil?
        results[country][city][main].keys.each do |sub|
          puts "      #{sub}: #{results[country][city][main][sub] }"
        end
      end
    end
  end
end

#second_formatObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cert_open_data_visualizer/formatter.rb', line 34

def second_format
  if @cacher.file_exists?("second_challenge")
    return File.read(@cacher.get_from_cache("second_challenge"))
  else
    load_data! if @data.nil?
  end
  incidents = count_incidents_based_on_location
  incidents_str = ""
  incidents.each do |incident|
    incidents_str <<  "#{incident[0]} #{incident[1]}\n"
  end
  @cacher.write_file_to_cache("second_challenge", incidents_str)
  incidents_str
end