From a8cdbcf4cfd1f2aed0c53abc68bdfb5eae077b7d Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs <matthewrudyjacobs@gmail.com> Date: Mon, 25 May 2015 16:03:46 +0800 Subject: [PATCH] introduce `supported_protocols` as a place for lookups to override avoid overriding `use_ssl?` or `protocol` directly --- lib/geocoder/lookups/base.rb | 12 ++++++++++++ lib/geocoder/lookups/google_places_details.rb | 4 ++-- lib/geocoder/lookups/maxmind_geoip2.rb | 8 ++++---- lib/geocoder/lookups/smarty_streets.rb | 9 +++++---- lib/geocoder/lookups/telize.rb | 10 +++++----- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 5ac00379..11ea78b4 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -86,6 +86,15 @@ module Geocoder @cache end + ## + # Array containing the protocols supported by the api. + # Should be set to [:http] if only HTTP is supported + # or [:https] if only HTTPS is supported. + # + def supported_protocols + [:http, :https] + end + private # ------------------------------------------------------------- ## @@ -279,6 +288,9 @@ module Geocoder end def use_ssl? + return true if supported_protocols == [:https] + return false if supported_protocols == [:http] + configuration.use_https end diff --git a/lib/geocoder/lookups/google_places_details.rb b/lib/geocoder/lookups/google_places_details.rb index 38b3bb84..c4825f81 100644 --- a/lib/geocoder/lookups/google_places_details.rb +++ b/lib/geocoder/lookups/google_places_details.rb @@ -12,8 +12,8 @@ module Geocoder ["key"] end - def use_ssl? - true + def supported_protocols + [:https] end def query_url(query) diff --git a/lib/geocoder/lookups/maxmind_geoip2.rb b/lib/geocoder/lookups/maxmind_geoip2.rb index 761be9b6..8ada99d3 100644 --- a/lib/geocoder/lookups/maxmind_geoip2.rb +++ b/lib/geocoder/lookups/maxmind_geoip2.rb @@ -8,10 +8,10 @@ module Geocoder::Lookup "MaxMind GeoIP2" end - def use_ssl? - # Maxmind's GeoIP2 Precision Services only supports HTTPS, - # otherwise a `404 Not Found` HTTP response will be returned - true + # Maxmind's GeoIP2 Precision Services only supports HTTPS, + # otherwise a `404 Not Found` HTTP response will be returned + def supported_protocols + [:https] end def query_url(query) diff --git a/lib/geocoder/lookups/smarty_streets.rb b/lib/geocoder/lookups/smarty_streets.rb index 2aebf07a..92065d61 100644 --- a/lib/geocoder/lookups/smarty_streets.rb +++ b/lib/geocoder/lookups/smarty_streets.rb @@ -16,12 +16,13 @@ module Geocoder::Lookup "#{protocol}://api.smartystreets.com/#{path}?#{url_query_string(query)}" end - private # --------------------------------------------------------------- - - def protocol - "https" # required by API as of 26 March 2015 + # required by API as of 26 March 2015 + def supported_protocols + [:https] end + private # --------------------------------------------------------------- + def zipcode_only?(query) !query.text.is_a?(Array) and query.to_s.strip =~ /\A\d{5}(-\d{4})?\Z/ end diff --git a/lib/geocoder/lookups/telize.rb b/lib/geocoder/lookups/telize.rb index 43b1de40..85fb668d 100644 --- a/lib/geocoder/lookups/telize.rb +++ b/lib/geocoder/lookups/telize.rb @@ -9,16 +9,16 @@ module Geocoder::Lookup end def query_url(query) - #currently doesn't support HTTPS "http://www.telize.com/geoip/#{query.sanitized_text}" end - private # --------------------------------------------------------------- - - def use_ssl? - false + # currently doesn't support HTTPS + def supported_protocols + [:http] end + private # --------------------------------------------------------------- + def results(query) # don't look up a loopback address, just return the stored result return [reserved_result(query.text)] if query.loopback_ip_address? -- GitLab