Class: TZDetect::GeonameGeocode
- Inherits:
-
Object
- Object
- TZDetect::GeonameGeocode
- Includes:
- Geocoder
- Defined in:
- lib/tzdetect/geocoders/geoname.rb
Constant Summary collapse
- GEONAME_GEOCODE_API =
"http://api.geonames.org/searchJSON"
Instance Attribute Summary
Attributes included from Geocoder
#city, #country, #latitude, #longitude, #region, #timezone
Instance Method Summary collapse
-
#position! ⇒ Object
Returns position.
Methods included from Geocoder
#fetched_data, included, #initialize, #postion
Instance Method Details
#position! ⇒ Object
Returns position
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 |
# File 'lib/tzdetect/geocoders/geoname.rb', line 11 def position! begin url =URI(GEONAME_GEOCODE_API) url.query = URI.encode_www_form(geoname_params) Net::HTTP.start(url.host, url.port, :use_ssl => false) do |http| request = Net::HTTP::Get.new(url.request_uri) response = http.request request # Net::HTTPResponse object json_result =JSON.parse(response.read_body) if !json_result["geonames"].nil? and !json_result["geonames"].empty? geoname = json_result["geonames"][0] @latitude = geoname["lat"] @longitude = geoname["lng"] @timezone = geoname["timezone"]["timeZoneId"] return fetched_data else raise "no results" end end rescue Exception => e raise TZDetect::Error::GeoCoder, e. end end |