Module: CS
- Defined in:
- lib/city-state.rb,
lib/city-state/version.rb
Constant Summary collapse
- MAXMIND_ZIPPED_CSV =
CS constants
"http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip"- CSV_FN =
"GeoLite2-City-Locations-en.csv"- FILES_FOLDER =
File.('../db', __FILE__)
- CSV_FN_FULL =
File.join(FILES_FOLDER, CSV_FN)
- ID =
constants: CVS position
0- COUNTRY =
4- STATE =
6- STATE_LONG =
7- CITY =
10- VERSION =
"0.0.8"
Class Method Summary collapse
- .cities(state, country = nil) ⇒ Object
- .current_country ⇒ Object
- .current_country=(country) ⇒ Object
- .install(country) ⇒ Object
- .states(country) ⇒ Object
- .update ⇒ Object
- .update_maxmind ⇒ Object
Class Method Details
.cities(state, country = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/city-state.rb', line 119 def self.cities(state, country = nil) self.current_country = country if country.present? # set as current_country country = self.current_country # load the country file if @cities[country].blank? cities_fn = File.join(FILES_FOLDER, "cities.#{country.to_s.downcase}") self.install(country) if ! File.exists? cities_fn @cities[country] = YAML::load_file(cities_fn).symbolize_keys end @cities[country][state.to_s.upcase.to_sym] || [] end |
.current_country ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/city-state.rb', line 95 def self.current_country return @current_country if @current_country.present? # we don't have used this module yet: discover by the file extension fn = Dir[File.join(FILES_FOLDER, "cities.*")].last @current_country = fn.blank? ? nil : fn.split(".").last # there's no files: we'll install and use :US if @current_country.blank? @current_country = :US self.install(@current_country) # we find a file: normalize the extension to something like :US else @current_country = @current_country.to_s.upcase.to_sym end @current_country end |
.current_country=(country) ⇒ Object
115 116 117 |
# File 'lib/city-state.rb', line 115 def self.current_country=(country) @current_country = country.to_s.upcase.to_sym end |
.install(country) ⇒ Object
51 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/city-state.rb', line 51 def self.install(country) # get CSV if doesn't exists update_maxmind unless File.exists? CSV_FN_FULL # normalize "country" country = country.to_s.upcase # read CSV line by line cities = {} states = {} File.foreach(CSV_FN_FULL) do |line| rec = line.split(",") next if rec[COUNTRY] != country next if rec[STATE].blank? || rec[CITY].blank? # normalize rec[STATE] = rec[STATE].to_sym rec[CITY].gsub!(/\"/, "") # sometimes names come with a "\" char rec[STATE_LONG].gsub!(/\"/, "") # sometimes names come with a "\" char # cities list: {TX: ["Texas City", "Another", "Another 2"]} cities.merge!({rec[STATE] => []}) if ! states.has_key?(rec[STATE]) cities[rec[STATE]] << rec[CITY] # states list: {TX: "Texas", CA: "California"} if ! states.has_key?(rec[STATE]) state = {rec[STATE] => rec[STATE_LONG]} states.merge!(state) end end # sort cities = Hash[cities.sort] states = Hash[states.sort] cities.each { |k, v| cities[k].sort! } # save to states.us and cities.us states_fn = File.join(FILES_FOLDER, "states.#{country.downcase}") cities_fn = File.join(FILES_FOLDER, "cities.#{country.downcase}") File.open(states_fn, "w") { |f| f.write states.to_yaml } File.open(cities_fn, "w") { |f| f.write cities.to_yaml } true end |
.states(country) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/city-state.rb', line 133 def self.states(country) self.current_country = country # set as current_country country = self.current_country # normalized # load the country file if @states[country].blank? states_fn = File.join(FILES_FOLDER, "states.#{country.to_s.downcase}") self.install(country) if ! File.exists? states_fn @states[country] = YAML::load_file(states_fn).symbolize_keys end @states[country] || [] end |
.update ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/city-state.rb', line 35 def self.update self.update_maxmind # update via internet Dir[File.join(FILES_FOLDER, "states.*")].each do |state_fn| self.install(state_fn.split(".").last.upcase.to_sym) # reinstall country end @cities = @states = {} # invalidades cache true end |
.update_maxmind ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/city-state.rb', line 14 def self.update_maxmind require "open-uri" require "zip" # get zipped file f_zipped = open(MAXMIND_ZIPPED_CSV) # unzip file: # recursively searches for "GeoLite2-City-Locations-en" Zip::File.open(f_zipped) do |zip_file| zip_file.each do |entry| if entry.name["GeoLite2-City-Locations-en"].present? fn = entry.name.split("/").last entry.extract(File.join(FILES_FOLDER, fn)) { true } # { true } is to overwrite break end end end true end |