Module: GeoLocation

Defined in:
lib/geo_location/setup.rb,
lib/geo_location/version.rb,
lib/geo_location/countries.rb,
lib/geo_location/timezones.rb,
lib/geo_location/geo_location.rb

Constant Summary collapse

VERSION =
"0.4.2"
@@use =
:hostip
@@key =
nil
@@dev =
nil
@@dev_ip =
nil
@@timezones =
{}
@@countries =
{}

Class Method Summary collapse

Class Method Details

.build_countriesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/geo_location/countries.rb', line 10

def build_countries
  if GeoLocation::countries.empty?
    data = {}
    
    file = File.join(File.dirname(__FILE__), 'countries.txt')
    File.open(file, "r") do |infile|
      while (line = infile.gets)
        countries = line.split("\n")
        countries.each do |c|
          c = c.split("  ")
          code = c[0].to_sym
          country = c[1]
          data[code] = country
        end # end countries.each
      end # end while
    end # end file.open
    
    GeoLocation::countries = data
  end # end if
end

.build_timezonesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geo_location/timezones.rb', line 10

def build_timezones
  if GeoLocation::timezones.empty?
    data = {}
    
    file = File.join(File.dirname(__FILE__), 'timezones.txt')
    File.open(file, "r") do |infile|
      while (line = infile.gets)
        zones = line.split("\n")
        zones.each do |z|
          zone = z.split("  ")
          country = zone[0].to_sym
          region = zone[1].empty? ? '' : zone[1].to_sym
          value = zone[2]
          
          data[country] = {} if data[country].nil?
          if region.to_s.empty?
            data[country] = value
          else
            data[country][region] = value
          end
          
        end # end zones.each
      end # end while
    end # end file.open
    
    GeoLocation::timezones = data
  end # end if
end

.country(country_code) ⇒ Object



5
6
7
8
# File 'lib/geo_location/countries.rb', line 5

def country(country_code)
  return nil if GeoLocation::countries.empty?
  GeoLocation::countries[country_code.to_sym]
end

.find(ip = nil) ⇒ Object



8
9
10
11
12
# File 'lib/geo_location/geo_location.rb', line 8

def find(ip=nil)
  ip = GeoLocation::dev_ip unless GeoLocation::dev_ip.nil?
  return nil unless valid_ip(ip)
  return (GeoLocation::use == :maxmind) ? maxmind(ip) : hostip(ip)
end

.timezone(country, region = nil) ⇒ Object



5
6
7
8
# File 'lib/geo_location/timezones.rb', line 5

def timezone(country, region=nil)
  return nil if GeoLocation::timezones.empty?
  (region.nil? || region.empty?) ? GeoLocation::timezones[country.to_sym] : GeoLocation::timezones[country.to_sym][region.to_sym]
end