Skip to content
Snippets Groups Projects
Commit 02ce8c1f authored by Alex Reisner's avatar Alex Reisner
Browse files

Fall back to ActiveSupport::JSON if no JSON.

Split result parsing into separate method.
parent 9b0c2100
No related branches found
No related tags found
No related merge requests found
require 'net/http' require 'net/http'
require 'json' unless defined? ActiveSupport::JSON
begin
require 'json'
rescue LoadError
raise LoadError, "Please install the json gem to parse geocoder results."
end
end
module Geocoder module Geocoder
module Lookup module Lookup
...@@ -49,14 +55,27 @@ module Geocoder ...@@ -49,14 +55,27 @@ module Geocoder
# #
def fetch_data(query, reverse = false) def fetch_data(query, reverse = false)
begin begin
JSON.parse(fetch_raw_data(query, reverse)) parse_raw_data fetch_raw_data(query, reverse)
rescue SocketError rescue SocketError
warn "Geocoding API connection cannot be established." warn "Geocoding API connection cannot be established."
rescue TimeoutError rescue TimeoutError
warn "Geocoding API not responding fast enough " + warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)." "(see Geocoder::Configuration.timeout to set limit)."
rescue JSON::ParseError end
warn "Geocoding API's response was not valid JSON." end
##
# Parses a raw search result (returns hash or array).
#
def parse_raw_data(raw_data)
if defined?(JSON)
begin
JSON.parse(raw_data)
rescue JSON::ParseError
warn "Geocoding API's response was not valid JSON."
end
elsif defined?(ActiveSupport::JSON)
ActiveSupport::JSON.decode(raw_data)
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment