From ca5d7813567d06a2a23ea99470431a35450d7f09 Mon Sep 17 00:00:00 2001 From: Marcin Olichwirowicz <olichwirowicz@gmail.com> Date: Fri, 30 Aug 2013 10:07:27 +0200 Subject: [PATCH] Extracts json parsing method --- lib/geocoder/lookups/base.rb | 16 ++++++++++------ lib/geocoder/lookups/google.rb | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 6589345a..c0ef4682 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -171,22 +171,26 @@ module Geocoder "(use Geocoder.configure(:timeout => ...) to set limit)." 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). # def parse_raw_data(raw_data) - if defined?(ActiveSupport::JSON) - ActiveSupport::JSON.decode(raw_data) - else - JSON.parse(raw_data) - end + parse_json(raw_data) rescue warn "Geocoding API's response was not valid JSON." end ## # 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 "http" + (configuration.use_https ? "s" : "") diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index 65308de2..4daeb977 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -17,7 +17,7 @@ module Geocoder::Lookup end def valid_response(response) - super(response) && JSON.parse(response.body)["status"] == "OK" + super(response) && parse_json(response.body)["status"] == "OK" end private # --------------------------------------------------------------- -- GitLab