Module: GoogleMapsApi::TimeZone::ClassMethods

Defined in:
app/models/google_maps_api/time_zone.rb

Instance Method Summary collapse

Instance Method Details

#time_zone_id(latitude, longitude, timestamp = nil) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/google_maps_api/time_zone.rb', line 8

def time_zone_id(latitude, longitude, timestamp = nil)
  raise ArgumentError.new("missing latitude and/or longitude: #{latitude}, #{longitude}") if latitude.blank? || longitude.blank?

  timestamp ||= Time.now.to_i
  location = "#{latitude},#{longitude}"

  begin
    return nil unless result = send_request("#{BASE_URI}?location=#{location}&timestamp=#{timestamp}")
  rescue SocketError => e
    if Rails.env.development? || Rails.env.test?
      Rails.logger.warn 'not connected to the Internet... faking time zone API call'
      return 'America/Los_Angeles'
    end

    raise e
  end

  result['timeZoneId']
end