module Geocoder
Constants
- VERSION
Public Class Methods
address(query, options = {})
click to toggle source
Look up the address of the given coordinates ([lat,lon]) or IP address (string).
# File lib/geocoder.rb, line 38 def self.address(query, options = {}) if (results = search(query, options)).size > 0 results.first.address end end
config()
click to toggle source
Read-only access to the singleton's config data.
# File lib/geocoder/configuration.rb, line 25 def self.config Configuration.instance.data end
config_for_lookup(lookup_name)
click to toggle source
Read-only access to lookup-specific config data.
# File lib/geocoder/configuration.rb, line 32 def self.config_for_lookup(lookup_name) data = config.clone data.reject!{ |key,value| !Configuration::OPTIONS.include?(key) } if config.has_key?(lookup_name) data.merge!(config[lookup_name]) end data end
configure(options = nil, &block)
click to toggle source
Configuration
options should be set by passing a hash:
Geocoder.configure( :timeout => 5, :lookup => :yandex, :api_key => "2a9fsa983jaslfj982fjasd", :units => :km )
# File lib/geocoder/configuration.rb, line 16 def self.configure(options = nil, &block) if !options.nil? Configuration.instance.configure(options) end end
coordinates(address, options = {})
click to toggle source
Look up the coordinates of the given street or IP address.
# File lib/geocoder.rb, line 28 def self.coordinates(address, options = {}) if (results = search(address, options)).size > 0 results.first.coordinates end end
log(level, message)
click to toggle source
# File lib/geocoder/logger.rb, line 5 def self.log(level, message) Logger.instance.log(level, message) end
merge_into_lookup_config(lookup_name, options)
click to toggle source
Merge the given hash into a lookup's existing configuration.
# File lib/geocoder/configuration.rb, line 44 def self.merge_into_lookup_config(lookup_name, options) base = Geocoder.config[lookup_name] Geocoder.configure(lookup_name => base.merge(options)) end
search(query, options = {})
click to toggle source
Search for information about an address or a set of coordinates.
# File lib/geocoder.rb, line 20 def self.search(query, options = {}) query = Geocoder::Query.new(query, options) unless query.is_a?(Geocoder::Query) query.blank? ? [] : query.execute end
Public Instance Methods
archive_url(package)
click to toggle source
# File lib/maxmind_database.rb, line 93 def archive_url(package) base_url + archive_url_path(package) end
archive_url_path(package)
click to toggle source
# File lib/maxmind_database.rb, line 97 def archive_url_path(package) { geolite_country_csv: "GeoIPCountryCSV.zip", geolite_city_csv: "GeoLiteCity_CSV/GeoLiteCity-latest.zip", geolite_asn_csv: "asnum/GeoIPASNum2.zip" }[package] end
base_url()
click to toggle source
# File lib/maxmind_database.rb, line 105 def base_url "http://geolite.maxmind.com/download/geoip/database/" end
classify_name(filename)
click to toggle source
Convert an “underscore” version of a name into a “class” version.
# File lib/geocoder/lookup.rb, line 114 def classify_name(filename) filename.to_s.split("_").map{ |i| i[0...1].upcase + i[1..-1] }.join end
data_files(package, dir = "tmp")
click to toggle source
# File lib/maxmind_database.rb, line 82 def data_files(package, dir = "tmp") case package when :geolite_city_csv # use the last two in case multiple versions exist files = Dir.glob(File.join(dir, "GeoLiteCity_*/*.csv"))[-2..-1].sort Hash[*files.zip(["maxmind_geolite_city_blocks", "maxmind_geolite_city_location"]).flatten] when :geolite_country_csv {File.join(dir, "GeoIPCountryWhois.csv") => "maxmind_geolite_country"} end end
expire_single_url(url)
click to toggle source
# File lib/geocoder/cache.rb, line 89 def expire_single_url(url) key = key_for(url) store.respond_to?(:del) ? store.del(key) : store.delete(key) end
insert_into_table(table, filepath)
click to toggle source
# File lib/maxmind_database.rb, line 48 def insert_into_table(table, filepath) start_time = Time.now print "Loading data for table #{table}" rows = [] columns = table_columns(table) CSV.foreach(filepath, encoding: "ISO-8859-1") do |line| # Some files have header rows. # skip if starts with "Copyright" or "locId" or "startIpNum" next if line.first.match(/[A-z]/) rows << line.to_a if rows.size == 10000 insert_rows(table, columns, rows) rows = [] print "." end end insert_rows(table, columns, rows) if rows.size > 0 puts "done (#{Time.now - start_time} seconds)" end
insert_rows(table, headers, rows)
click to toggle source
# File lib/maxmind_database.rb, line 68 def insert_rows(table, headers, rows) value_strings = rows.map do |row| "(" + row.map{ |col| sql_escaped_value(col) }.join(',') + ")" end q = "INSERT INTO #{table} (#{headers.join(',')}) " + "VALUES #{value_strings.join(',')}" ActiveRecord::Base.connection.execute(q) end
interpret(value)
click to toggle source
Clean up value before returning. Namely, convert empty string to nil. (Some key/value stores return empty string instead of nil.)
# File lib/geocoder/cache.rb, line 85 def interpret(value) value == "" ? nil : value end
key_for(url)
click to toggle source
Cache
key for a given URL.
# File lib/geocoder/cache.rb, line 62 def key_for(url) [prefix, url].join end
keys()
click to toggle source
Array of keys with the currently configured prefix that have non-nil values.
# File lib/geocoder/cache.rb, line 70 def keys store.keys.select{ |k| k.match(/^#{prefix}/) and self[k] } end
sql_escaped_value(value)
click to toggle source
# File lib/maxmind_database.rb, line 77 def sql_escaped_value(value) value.to_i.to_s == value ? value : ActiveRecord::Base.connection.quote(value) end
store()
click to toggle source
# File lib/geocoder/cache.rb, line 57 def store; @store; end
urls()
click to toggle source
Array of cached URLs.
# File lib/geocoder/cache.rb, line 77 def urls keys.map{ |k| k[/^#{prefix}(.*)/, 1] } end
valid_level?(level)
click to toggle source
# File lib/geocoder/logger.rb, line 43 def valid_level?(level) SEVERITY.keys.include?(level) end