cert_open_data_visualizer 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -39,10 +39,13 @@ $ cert_open_data_visualizer #=> prints help
39
39
  #=> clean - cleanes cache
40
40
  #=> fetch - fetches new data if neccessary
41
41
  #=> app - launces web server for visualizations in http://127.0.0.1:4567
42
+ #=> city - `city hel` - list details for each city containing `hel`
42
43
  #=>
43
- #=> `first` and `second` may download and parse data when neccesary.
44
+ #=> Commands may download and parse data when neccesary.
45
+ #=> Data is cached so running commands gest faster when repeating those.
44
46
  #=>
45
- #=> This includes a file cache, for faster results.
47
+
48
+ #=> A file cache is utilised for faster results.
46
49
  #=> ie. information is calculated only when necessary, and cached for
47
50
  #=> further use. Cache may be cleared using `clean`
48
51
 
@@ -51,6 +54,10 @@ $ cert_open_data_visualizer first #=> Prints output specified by exercise descri
51
54
  $ cert_open_data_visualizer second #=> Prints output specified by exercise description
52
55
 
53
56
  $ cert_open_data_visualizer clean #=> Cleans cache
57
+
58
+ $ cert_open_data_visualizer city hel # Lists cities containing word given as parameter
59
+ # If multiple parameters are given, results are
60
+ # listed separately for each
54
61
  ```
55
62
 
56
63
  #### Example using this as a library
@@ -26,6 +26,12 @@ class CertDataVisualizer
26
26
  puts second_format
27
27
  end
28
28
 
29
+ def filter_by_city(*citys)
30
+ citys.each do |city|
31
+ @formatter.filter_by_city(city)
32
+ end
33
+ end
34
+
29
35
  def clean_cache!
30
36
  puts "Cleaning cache!"
31
37
  CertOpenDataVisualizer::DummyCacher.new.clean!
@@ -14,17 +14,18 @@ module CertOpenDataVisualizer
14
14
  first: "print_first_format",
15
15
  second: "print_second_format",
16
16
  app: "start-app",
17
+ city: "filter_by_city",
17
18
  help: "print_help"}
18
19
 
19
20
  return help if @argv[0].nil?
20
- command = @argv[0].to_sym
21
+ command = @argv.shift.to_sym
21
22
 
22
23
  exec = commands[command]
23
24
 
24
25
  return run_app if exec == "start-app"
25
26
 
26
27
  return help if exec == "print_help"
27
- return visualizer.send(exec) if exec
28
+ return visualizer.send(exec, *argv) if exec
28
29
  puts "Invalid command #{command}"
29
30
  end
30
31
 
@@ -39,10 +40,11 @@ Commands are:
39
40
  clean - cleanes cache
40
41
  fetch - fetches new data if neccessary
41
42
  app - launces web server for visualizations in http://127.0.0.1:4567
43
+ city - `city hel` - list details for each city containing `hel`
42
44
 
43
- `first` and `second` may download and parse data when neccesary.
45
+ Commands may download and parse data when neccesary.
44
46
 
45
- This includes a file cache, for faster results.
47
+ A file cache is utilised for faster results.
46
48
  ie. information is calculated only when necessary, and cached for
47
49
  further use. Cache may be cleared using `clean`
48
50
  EOF
@@ -46,6 +46,55 @@ module CertOpenDataVisualizer
46
46
  incidents_str
47
47
  end
48
48
 
49
+ def filter_by_city(name)
50
+ # get everything matching
51
+ load_data! if @data.nil?
52
+ filterd = @data.select do |line|
53
+ line[8].downcase.include? name
54
+ end
55
+
56
+ # create hash for formatting
57
+ results = generate_city_hash(filterd)
58
+ # print in nice format
59
+ print_city_results(results)
60
+ end
61
+
62
+ def generate_city_hash(list)
63
+ results = {}
64
+ list.each do |l|
65
+ country, city, main, sub = l[7], l[8], l[5], l[6]
66
+ if results[country].nil?
67
+ results[country] = { city => { main => { sub => 1 } }}
68
+ elsif results[country][city].nil?
69
+ results[country][city] = {main => { sub => 1 } }
70
+ elsif results[country][city][main].nil?
71
+ results[country][city][main] = { sub => 1 }
72
+ elsif results[country][city][main][sub].nil?
73
+ results[country][city][main][sub] = 1
74
+ else
75
+ results[country][city][main][sub] += 1
76
+ end
77
+ end
78
+ results
79
+ end
80
+
81
+ def print_city_results(results)
82
+ results.keys.each do |country|
83
+ puts "#{country}:"
84
+ next if results[country].nil?
85
+ results[country].keys.each do |city|
86
+ puts " #{city}:"
87
+ next if results[country][city].nil?
88
+ results[country][city].keys.each do |main|
89
+ puts " #{main}:"
90
+ next if results[country][city][main].nil?
91
+ results[country][city][main].keys.each do |sub|
92
+ puts " #{sub}: #{results[country][city][main][sub] }"
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
49
98
 
50
99
  private
51
100
  def count_main_incidents
@@ -1,3 +1,3 @@
1
1
  module CertOpenDataVisualizer
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -3,6 +3,7 @@ module CertOpenDataVisualizer
3
3
  require 'httparty'
4
4
  require 'tmpdir'
5
5
  require 'csv'
6
+ require 'json'
6
7
 
7
8
  class Visualize
8
9
  attr_accessor :all_data, :cacher
@@ -14,15 +15,14 @@ module CertOpenDataVisualizer
14
15
 
15
16
  def parse
16
17
  maybe_download
17
- files = extract_and_list_files
18
- read_and_merge_files(files)
18
+ files = maybe_extract_and_list_files
19
+ maybe_read_and_merge_files(files)
19
20
  self
20
21
  end
21
22
 
22
23
  def maybe_download
23
24
  if @cacher.file_exists?("cert.zip")
24
- puts "Loading data from cache"
25
- @cacher.get_from_cache("cert.zip")
25
+ puts "File found in cache, not downloading"
26
26
  else
27
27
  download
28
28
  end
@@ -32,20 +32,28 @@ module CertOpenDataVisualizer
32
32
  puts "Downloading, may take a while depending on your connection"
33
33
  data = HTTParty.get(CSV_DATA_URL).body
34
34
  puts "Done"
35
- write_tmp_file(data)
35
+ write_tmp_file("cert.zip", data)
36
36
  end
37
37
 
38
- def write_tmp_file(contents)
39
- @cacher.write_file_to_cache "cert.zip", contents
38
+ def write_tmp_file(filename, contents)
39
+ @cacher.write_file_to_cache filename, contents
40
40
  end
41
41
 
42
- def extract_and_list_files
43
- @cacher.unzip_file("cert.zip")
42
+ def maybe_extract_and_list_files
43
+ if @cacher.file_exists? "cert.zip" and not @cacher.file_exists? "certfi_autoreporter_opendata_2006.csv"
44
+ @cacher.unzip_file("cert.zip")
45
+ end
44
46
  @cacher.find_files_matching("*.csv")
45
47
  end
46
48
 
47
- def read_and_merge_files(files)
48
- @all_data = get_csvs(files)
49
+ def maybe_read_and_merge_files(files)
50
+ if @cacher.file_exists? "all_data.json"
51
+ @all_data = JSON.parse(File.read(@cacher.get_from_cache("all_data.json")))
52
+ else
53
+ @all_data = get_csvs(files)
54
+ write_tmp_file("all_data.json", @all_data.to_json)
55
+ end
56
+ @all_data
49
57
  end
50
58
 
51
59
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cert_open_data_visualizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: