Class: Geonames::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/geonames_local/cli.rb

Overview

Command Line Interface for Geonames Local

Class Method Summary collapse

Class Method Details

.load_adapter(name) ⇒ Object



163
164
165
166
167
168
# File 'lib/geonames_local/cli.rb', line 163

def load_adapter(name)
  require_relative "models/#{name}"
rescue LoadError => e
  info "Can't find adapter for #{name} #{e}"
  stop!
end

.load_configObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/geonames_local/cli.rb', line 58

def load_config
  info 'Loading config file...'
  if Opt[:config]
    Opt.merge! YAML.load(File.read(Opt[:config]))
  else
    # Load config/geonames.yml if there's one
    if File.exist?(cfg = File.join('config', 'geonames.yml'))
      Opt.merge! YAML.load(File.read(cfg))
    else
      fail
    end
  end
rescue
  info "Can't find config file"
  exit
end

.stop!Object



181
182
183
184
# File 'lib/geonames_local/cli.rb', line 181

def stop!
  info 'Closing Geonames...'
  exit
end

.trap_signalsObject



75
76
77
78
79
# File 'lib/geonames_local/cli.rb', line 75

def trap_signals
  puts 'Geopolitical Local Start!'
  trap(:INT) { stop! }
  trap(:TERM) { stop! }
end

.unify!(dump, zip) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/geonames_local/cli.rb', line 170

def unify!(dump, zip)
  start = Time.now
  dump.map! do |spot|
    next spot unless (other = zip.find { |z| z.code == spot.code })
    spot.zip = other.zip
    spot
  end
  info "Done. #{(Time.now - start).to_i}s"
  dump
end

.work(argv) ⇒ Object

Ugly but works?



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/geonames_local/cli.rb', line 108

def work(argv)
  start = Time.now
  trap_signals
  Opt.merge! parse_options(argv)
  if Opt[:locales].nil? || Opt[:locales].empty?
    Opt[:locales] = ['en']
  end

  if shp = Opt[:shp]
    SHP.import(shp)
    exit
  end

  #
  # Return Codes and Exit
  #
  if argv[0] =~ /list|codes/
    Codes.each do |key, val|
      str = [val.values, key.to_s].join(' ').downcase
      if s = argv[1]
        next unless str =~ /#{s.downcase}/
      end
      puts "#{val[:en_us]}: #{key}"
    end
    exit
  end

  #
  # If arguments scaffold, config, write down yml.
  #
  if argv[0] =~ /scaff|conf|init/
    fname = (argv[1] || 'geonames') + '.yml'
    if File.exist?(fname)
      puts "File exists: #{fname}"
    else
      puts "Writing to #{fname}"
      `cp #{File.join(File.dirname(__FILE__), 'config', 'geonames.yml')} #{fname}`
    end
    exit
  end

  # Load config if we got til here
  load_config

  # Export Data as CSV or JSON
  return Geonames::Export.new(Nation.all).to_csv if argv[0] =~ /csv|json/

  # Do the magic! Import Geonames Data
  load_adapter(Opt[:store])
  info "Using adapter #{Opt[:store]}.."
  wrapper.clean if Opt[:clean]
  puts Benchmark.measure { work_nations } unless wrapper.nations_populated?
  puts Benchmark.measure { work_spots }
end

.work_nationsObject



85
86
87
88
89
90
91
92
# File 'lib/geonames_local/cli.rb', line 85

def work_nations
  info "\nPopulating 'nations' database..."
  dump = Geonames::Dump.new(:all, :dump)
  info "\n---\nTotal #{dump.data.length} parsed."

  info 'Writing to nations DB'
  wrapper.nations dump.data
end

.work_spotsObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/geonames_local/cli.rb', line 94

def work_spots
  info "\nPopulating 'regions' and 'cities' database..."
  zip = Geonames::Dump.new(Opt[:nations], :zip).data
  dump = Geonames::Dump.new(Opt[:nations], :dump).data
  info "\n---\nTotal #{dump.size} parsed. #{zip.size} postal codes."

  info 'Join dump << zip'
  dump = unify!(dump, zip).group_by(&:kind)

  info 'Writing to DB...'
  wrapper.batch dump
end

.wrapperObject



81
82
83
# File 'lib/geonames_local/cli.rb', line 81

def wrapper
  Geonames::Models::MongoWrapper
end