Class: FreeZipcodeData::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/free_zipcode_data/runner.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



21
22
23
# File 'lib/free_zipcode_data/runner.rb', line 21

def initialize
  @logger = Logger.instance
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/free_zipcode_data/runner.rb', line 14

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/free_zipcode_data/runner.rb', line 14

def options
  @options
end

Class Method Details

.instanceObject

Make a singleton but allow the class to be instantiated for easier testing



17
18
19
# File 'lib/free_zipcode_data/runner.rb', line 17

def self.instance
  @instance || new
end

Instance Method Details

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/free_zipcode_data/runner.rb', line 25

def start
  start_time = Time.now
  opt = FreeZipcodeData::Options.instance
  opt.initialize_hash(collect_args)
  @options = opt.hash

  logger.info("Starting FreeZipcodeData v#{VERSION}...".green)

  datasource = DataSource.new(options.country)
  datasource.download

  db_file = File.join(options.work_dir, 'free_zipcode_data.sqlite3')
  database = SqliteRam.new(db_file)

  line_count = datasource_line_count(datasource.datafile)
  configure_meta(database.conn, line_count)

  %i[country state county zipcode].each { |t| initialize_table(t, database) }

  extract_transform_load(datasource, database)

  logger.info("Saving database to disk '#{db_file}'...")
  database.save_to_disk

  if options.generate_files
    logger.info('Generating .csv files...')
    database.dump_tables(options.work_dir)
  end

  elapsed = Time.at(Time.now - start_time).utc.strftime('%H:%M:%S')
  logger.info("Processed #{line_count} zipcodes in [#{elapsed}].".yellow)
end