Class: TimeTraveler::Utils Private

Inherits:
Object
  • Object
show all
Defined in:
lib/time_traveler/utils.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Utilities for working with data files.

Class Method Summary collapse

Class Method Details

.download_geonames_data(cities_file, target_directory) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • cities_file (String)
  • target_directory (String)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/time_traveler/utils.rb', line 22

def download_geonames_data(cities_file, target_directory)
  require 'net/http'
  resp = nil
  Net::HTTP.start('download.geonames.org') do |http|
    resp = http.get("/export/dump/#{cities_file}.zip")
    open("#{target_directory}/#{cities_file}.zip", 'wb') do |file|
      file.write(resp.body)
    end
  end
  !resp.nil? && resp.code.to_i < 400
end

.load_timezone_data(working_directory = TimeTraveler::DATA_DIR) ⇒ Quadtree::Quadtree

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load timezone database from file.

Parameters:

  • working_directory (Pathname) (defaults to: TimeTraveler::DATA_DIR)

Returns:

  • (Quadtree::Quadtree)

    the database.



15
16
17
18
# File 'lib/time_traveler/utils.rb', line 15

def load_timezone_data(working_directory = TimeTraveler::DATA_DIR)
  require 'json'
  Quadtree::Quadtree.from_json(JSON.load(unpack_timezone_data(working_directory)))
end

.pack_timezone_data(working_directory = TimeTraveler::DATA_DIR) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • working_directory (Pathname) (defaults to: TimeTraveler::DATA_DIR)


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/time_traveler/utils.rb', line 81

def pack_timezone_data(working_directory = TimeTraveler::DATA_DIR)
  require 'zlib'
  working_directory = Pathname.new(working_directory) if working_directory.is_a? String

  Zlib::GzipWriter.open(working_directory.join('cities.data.gz')) do |gz|
    File.open(working_directory.join('cities.data')) do |fp|
      while (chunk = fp.read(16 * 1024))
        gz.write chunk
      end
    end
    gz.close
  end
end

.process_geonames_data(cities_file, working_directory, target_directory = './lib/data') ⇒ Quadtree::Quadtree

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the database.

Parameters:

  • cities_file (String)
  • working_directory (String)
  • target_directory (String) (defaults to: './lib/data')

Returns:

  • (Quadtree::Quadtree)

    the database.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/time_traveler/utils.rb', line 52

def process_geonames_data(cities_file, working_directory, target_directory = './lib/data')
  require 'csv'

  aabb = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(0, 0), 180)
  qt = Quadtree::Quadtree.new(aabb)

  parsed_file = CSV.read("#{working_directory}/#{cities_file}.txt", :col_sep => "\t")
  parsed_file.each do |entry|
    next unless entry.size > 18

    latitude = entry[4]
    longitude = entry[5]
    timezone = entry[17]
    unless latitude.nil? || longitude.nil? || timezone.nil?
      point = Quadtree::Point.new(longitude.to_f, latitude.to_f, timezone)
      qt.insert! point
    end
  end

  File.open("#{target_directory}/cities.data", 'w') do |f|
    f.write qt.to_json
  end

  pack_timezone_data(target_directory)

  qt
end

.unpack_timezone_data(working_directory = TimeTraveler::DATA_DIR) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • working_directory (Pathname) (defaults to: TimeTraveler::DATA_DIR)

Returns:

  • (String)


97
98
99
100
101
102
# File 'lib/time_traveler/utils.rb', line 97

def unpack_timezone_data(working_directory = TimeTraveler::DATA_DIR)
  require 'zlib'
  working_directory = Pathname.new(working_directory) if working_directory.is_a? String
  gz = Zlib::GzipReader.open(working_directory.join('cities.data.gz'))
  gz.read
end

.unzip_geonames_data(cities_file, working_directory) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • cities_file (String)
  • working_directory (String)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/time_traveler/utils.rb', line 36

def unzip_geonames_data(cities_file, working_directory)
  require 'zip'
  Zip.on_exists_proc = true

  extracted = Zip::File.open("#{working_directory}/#{cities_file}.zip") do |zip_file|
    zip_file.each do |entry|
      entry.extract("#{working_directory}/#{entry.name}")
    end
  end
  !extracted.nil? && extracted.size.positive?
end