class Geonames::CLI
Command Line Interface for Geonames
Local
Public Class Methods
load_adapter(name)
click to toggle source
# 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_config()
click to toggle source
# 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!()
click to toggle source
# File lib/geonames_local/cli.rb, line 181 def stop! info 'Closing Geonames...' exit end
trap_signals()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Ugly but works?
# 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_nations()
click to toggle source
# 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_spots()
click to toggle source
# 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
wrapper()
click to toggle source
# File lib/geonames_local/cli.rb, line 81 def wrapper Geonames::Models::MongoWrapper end
Private Class Methods
parse_options(argv)
click to toggle source
# File lib/geonames_local/cli.rb, line 17 def self.parse_options(argv) options = {} argv.options do |opts| opts.banner = 'Geonames Command Line Usage\n\n geonames <nation code(s)> <opts>\n\n\n' opts.on('-l', '--level LEVEL', String, 'The level of logging to report') { |level| options[:level] = level } opts.on('-d', '--dump', 'Dump DB before all') { options[:clean] = true } opts.separator '' opts.separator 'Config file:' opts.on('-c', '--config CONFIG', String, 'Geonames Config file path') { |file| options[:config] = file } opts.on('-i', '--import CONFIG', String, 'Geonames Import SHP/DBF/GPX') { |file| options[:shp] = file } opts.separator '' opts.separator 'SHP Options:' opts.on('--map TYPE', Array, 'Use zone/road to import') { |s| options[:map] = s.map(&:to_sym) } opts.on('--type TYPE', String, 'Use zone/road to import') { |s| options[:type] = s } opts.on('--city CITY', String, 'Use city gid to import') { |s| options[:city] = s } opts.on('--nation NATION', String, 'Use nation gid to import') { |s| options[:nation] = s } opts.separator '' opts.separator 'Common Options:' opts.on('-h', '--help', 'Show this message') { puts opts; exit } opts.on('-v', '--verbose', 'Turn on logging to STDOUT') { |bool| options[:verbose] = bool } opts.on('-V', '--version', 'Show version') { puts Geonames::VERSION; exit } opts.separator '' begin opts.parse! if argv.empty? && !options[:config] puts opts exit end rescue puts opts exit end end options end