class PackerFiles::Utils::AutoZone

AutoZone handles the automatic detection of a time zone via web based GEO Lookups.

Public Class Methods

Cache() click to toggle source

Return back the Cached JSON

# File lib/PackerFiles/Utils/AutoZone.rb, line 27
def self.Cache
   @@cache
end

Public Instance Methods

JSON() click to toggle source

Return the JSON from the web services, to do what ever.

# File lib/PackerFiles/Utils/AutoZone.rb, line 32
def JSON
 
  # Return the cache if it is populated already
  return @@cache if !@@cache.nil?

  # Cache Miss! Let us get it from Internet.
  @@geo.each do |url|
     begin
       contents = open(url) {|f| f.read }
       @@cache  = JSON.parse(contents)
       break
     rescue
       next
     end
  end
  return @@cache
end
country_area_code() click to toggle source

Sugar coated country_area code

# File lib/PackerFiles/Utils/AutoZone.rb, line 56
def country_area_code
   json  = self.JSON
   return json['country_code'], json['area_code']
end
country_code() click to toggle source

Sugar coated country code

# File lib/PackerFiles/Utils/AutoZone.rb, line 51
def country_code
   return self.JSON['country_code']
end
time_zone() click to toggle source

Sugar coated perfect time zone look-up

# File lib/PackerFiles/Utils/AutoZone.rb, line 62
def time_zone
       
   # Get the RAW JSON
   json  = self.JSON

   # If it already contains a timezone, good enough and move on.
   return json['timezone'] if json.has_key?('timezone')

   # If not need more lookups with max-mind DB. Cache it for further
   # use.
   country = json['country_code']
   area    = json['area_code']
   open(@@url) {|f|
    f.each_line do |line|
      arr = line.split("\t")
      # Return the Time Zone if there is an exact match of country and area
      if (arr[0] == country && arr[1] == area)
         @@cache['timezone'] = arr[2]
         break
      # Return the Time Zone if there is an exact match of country and if
      # area cannot be determined reliably.
      elsif (arr[0] == country && (arr[1].empty? || area.empty?))
         @@cache['timezone'] = arr[2]
         break
      end
    end 
  }
  return @@cache['timezone']
end