Sha256: e867676e2f3c09b0d07562be04a601440df3ca464d95e58e967f82e14eede324

Contents?: true

Size: 801 Bytes

Versions: 3

Compression:

Stored size: 801 Bytes

Contents

require 'timezone/lookup/basic'
require 'timezone/error'
require 'json'
require 'uri'

module Timezone
  module Lookup
    class Geonames < ::Timezone::Lookup::Basic
      def lookup(lat, lng)
        response = client.get(url(lat, lng))

        return unless response.code =~ /^2\d\d$/

        data = JSON.parse(response.body)

        if data['status'] && data['status']['value'] == 18
          raise(Timezone::Error::GeoNames, 'api limit reached')
        end

        data['timezoneId']
      rescue => e
        raise(Timezone::Error::GeoNames, e.message)
      end

      private

      def url(lat, lng)
        query = URI.encode_www_form(
          'lat' => lat,
          'lng' => lng,
          'username' => config.username)
        "/timezoneJSON?#{query}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timezone-0.3.9 lib/timezone/lookup/geonames.rb
timezone-0.3.8 lib/timezone/lookup/geonames.rb
timezone-0.3.7 lib/timezone/lookup/geonames.rb