Sha256: 2f16ddc943ff6ae4b68e0cb40f99c1f2c8b2d50cda6bf65852f40ea04b23f6fc

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# encoding: UTF-8
module Graticule #:nodoc:
  module Geocoder #:nodoc:
    class Geonames < Base

      def initialize(username)
        @url = URI.parse('http://ws.geonames.org/timezone')
        @username = URI.encode(username)
      end

      def time_zone(location)
        get :formatted => 'true', 
            :style => 'full',
            :lat => location.latitude,
            :lng => location.longitude,
            :username => @username
      end

    private
      class Status
        include HappyMapper
        tag 'status'
        attribute :message, String
        attribute :value, String
      end

      class Response
        include HappyMapper
        tag 'geonames'
        element :timezoneId, String, :deep => true
        has_one :status, Status
      end

      def prepare_response(xml)
        Response.parse(xml, :single => true)
      end

      def parse_response(response) #:nodoc:
        response.timezoneId
      end

      # Extracts and raises an error from +xml+, if any.
      def check_error(response) #:nodoc:
        if response && response.status
          case response.status.value
          when 14 then
            raise Error, reponse.status.message
          when 12 then
            raise AddressError, reponse.status.message
          else
            raise Error, "unknown error #{response.status.message}"
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
graticule-2.7.2 lib/graticule/geocoder/geonames.rb
graticule-2.7.1 lib/graticule/geocoder/geonames.rb
graticule-2.7.0 lib/graticule/geocoder/geonames.rb
graticule-2.6.0 lib/graticule/geocoder/geonames.rb