diff --git a/lib/geocoder/results/opencagedata.rb b/lib/geocoder/results/opencagedata.rb index 94407dbc135128df84c96d9cacf634bcf6c79cce..2c8fbb7c233a86f4d11b39c580bcd52728722838 100644 --- a/lib/geocoder/results/opencagedata.rb +++ b/lib/geocoder/results/opencagedata.rb @@ -75,6 +75,15 @@ module Geocoder::Result [south, west, north, east] end + def time_zone + # The OpenCage API documentation states that `annotations` is available + # "when possible" https://geocoder.opencagedata.com/api#annotations + @data + .fetch('annotations', {}) + .fetch('timezone', {}) + .fetch('name', nil) + end + def self.response_attributes %w[boundingbox license formatted stadium] diff --git a/test/unit/lookups/opencagedata_test.rb b/test/unit/lookups/opencagedata_test.rb index 7ed0eb321aca2694bb4dda2473224f51a77f407e..b8ad20de249a4f8bf7ca01d4258d7196bde7cdf4 100644 --- a/test/unit/lookups/opencagedata_test.rb +++ b/test/unit/lookups/opencagedata_test.rb @@ -60,7 +60,10 @@ class OpencagedataTest < GeocoderTestCase assert_match(/\bq=45.423733%2C-75.676333\b/, query.url) end - + def test_opencagedata_time_zone + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "America/New_York", result.time_zone + end def test_raises_exception_when_invalid_request Geocoder.configure(always_raise: [Geocoder::InvalidRequest])