Sha256: afa1f0b17ccb2c2c04ae0e6f85f35d94e2848aeec5122003e0d68e756b13531e

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Graticule #:nodoc:
  module Geocoder #:nodoc:
    class Geonames < Base
      
      def initialize
        @url = URI.parse 'http://ws.geonames.org/timezone'
      end
      
      def time_zone(location)
        get :formatted => 'true', :style => 'full', :lat => location.latitude, :lng => location.longitude        
      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.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

2 entries across 2 versions & 1 rubygems

Version Path
graticule-1.0.0.pre2 lib/graticule/geocoder/geonames.rb
graticule-1.0.0.pre lib/graticule/geocoder/geonames.rb