Skip to content
Snippets Groups Projects
Commit ca5d7813 authored by Marcin Olichwirowicz's avatar Marcin Olichwirowicz
Browse files

Extracts json parsing method

parent 1c0dd3e4
No related branches found
No related tags found
No related merge requests found
...@@ -171,22 +171,26 @@ module Geocoder ...@@ -171,22 +171,26 @@ module Geocoder
"(use Geocoder.configure(:timeout => ...) to set limit)." "(use Geocoder.configure(:timeout => ...) to set limit)."
end end
def parse_json(data)
if defined?(ActiveSupport::JSON)
ActiveSupport::JSON.decode(data)
else
JSON.parse(data)
end
end
## ##
# Parses a raw search result (returns hash or array). # Parses a raw search result (returns hash or array).
# #
def parse_raw_data(raw_data) def parse_raw_data(raw_data)
if defined?(ActiveSupport::JSON) parse_json(raw_data)
ActiveSupport::JSON.decode(raw_data)
else
JSON.parse(raw_data)
end
rescue rescue
warn "Geocoding API's response was not valid JSON." warn "Geocoding API's response was not valid JSON."
end end
## ##
# Protocol to use for communication with geocoding services. # Protocol to use for communication with geocoding services.
# Set in configuration but not available for every service. # Set in configuration but no available for every service.
# #
def protocol def protocol
"http" + (configuration.use_https ? "s" : "") "http" + (configuration.use_https ? "s" : "")
......
...@@ -17,7 +17,7 @@ module Geocoder::Lookup ...@@ -17,7 +17,7 @@ module Geocoder::Lookup
end end
def valid_response(response) def valid_response(response)
super(response) && JSON.parse(response.body)["status"] == "OK" super(response) && parse_json(response.body)["status"] == "OK"
end end
private # --------------------------------------------------------------- private # ---------------------------------------------------------------
......
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