-
Notifications
You must be signed in to change notification settings - Fork 53
/
geonames.rb
51 lines (44 loc) · 1.29 KB
/
geonames.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 && 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