Small gem to get timezone info by known coordinates using google timezone api.
Add this line to your application's Gemfile:
gem 'google_timezone'
And then execute:
$ bundle
Or install it yourself as:
$ gem install google_timezone
You can pass latitude and longitude to gem in several ways: with array or separated values.
GoogleTimezone.fetch([latitude, longitude])
GoogleTimezone.fetch(latitude, longitude)
Any other options which are described in google's documentation you can pass via options hash
GoogleTimezone.fetch(latitude, longitude, language: 'en', signature: 'key')
It uses latitude and longitude to retrieve timezone info.
result = GoogleTimezone.fetch(50.1196004, 8.679918299999999)
result.time_zone_name => 'Europe/Berlin'
It will get GoogleTimezone::Result
object which maps major google api responce items named in snake case.
More information here
Also there is GoogleTimezone::Result#success?
method. It returns true if responce was successful.
The bang version fetch!
raises an GoogleTimezone::Error
exception with error message if response from Google wasn't success.
During tests, you probably don't want to make remote calls. To achieve this, you should set a default stub in your test/spec helper file, e.g.:
# spec/support/google_timezone.rb
GoogleTimezone.set_default_stub(
'dstOffset' => 3600,
'rawOffset' => -10800,
'status' => 'OK',
'timeZoneId' => 'America/Sao_Paulo',
'timeZoneName' => 'Brasilia Summer Time'
)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request