From 33790824d6b32d103e0dc69bf6877373266744fe Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 1 Mar 2015 19:22:46 +0000 Subject: [PATCH] Use Faraday in place of Net::HTTP so we can mock responses --- Gemfile | 3 +++ Gemfile.lock | 5 +++-- app/controllers/geocoder_controller.rb | 3 +-- lib/osm.rb | 21 +++++++++++++++------ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 25de38098..ae9c96b1c 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,9 @@ gem "htmlentities" # Load SystemTimer for implementing request timeouts gem "SystemTimer", ">= 1.1.3", :require => "system_timer", :platforms => :ruby_18 +# Load faraday for mockable HTTP client +gem "faraday" + # Load httpclient and soap4r for SOAP support for Quova GeoIP queries gem "httpclient" gem "soap4r-ruby1.9" diff --git a/Gemfile.lock b/Gemfile.lock index 91d4b662c..7de1743dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -221,7 +221,7 @@ GEM crass (~> 1.0.1) nokogiri (>= 1.4.4) nokogumbo (= 1.2.0) - sass (3.4.12) + sass (3.4.13) sass-rails (5.0.1) railties (>= 4.0.0, < 5.0) sass (~> 3.1) @@ -255,7 +255,7 @@ GEM tins (1.3.4) tzinfo (1.2.2) thread_safe (~> 0.1) - uglifier (2.7.0) + uglifier (2.7.1) execjs (>= 0.3.0) json (>= 1.8.0) validates_email_format_of (1.6.2) @@ -281,6 +281,7 @@ DEPENDENCIES dalli deadlock_retry (>= 1.2.0) dynamic_form + faraday htmlentities http_accept_language (~> 2.0.0) httpclient diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index be4f0570c..5a59c9eeb 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -3,7 +3,6 @@ class GeocoderController < ApplicationController require "cgi" require "uri" - require "net/http" require "rexml/document" before_action :authorize_web @@ -287,7 +286,7 @@ class GeocoderController < ApplicationController private def fetch_text(url) - Net::HTTP.get(URI.parse(url)) + OSM.http_client.get(URI.parse(url)).body end def fetch_xml(url) diff --git a/lib/osm.rb b/lib/osm.rb index 3a0beccdf..6c54caadf 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -495,15 +495,13 @@ module OSM def self.ip_to_country(ip_address) Timer.timeout(4) do - ipinfo = Quova::IpInfo.new(ip_address) + ipinfo = Quova::IpInfo.new(ip_address) if defined?(QUOVA_USERNAME) - if ipinfo.status == Quova::SUCCESS + if ipinfo && ipinfo.status == Quova::SUCCESS country = ipinfo.country_code else - Net::HTTP.start("api.hostip.info") do |http| - country = http.get("/country.php?ip=#{ip_address}").body - country = "GB" if country == "UK" - end + country = http_client.get("http://api.hostip.info/country.php?ip=#{ip_address}").body + country = "GB" if country == "UK" end return country.upcase @@ -552,9 +550,20 @@ module OSM "AND #{prefix}longitude BETWEEN #{bbox.min_lon} AND #{bbox.max_lon}" end + # Return the terms and conditions text for a given country def self.legal_text_for_country(country_code) file_name = File.join(Rails.root, "config", "legales", country_code.to_s + ".yml") file_name = File.join(Rails.root, "config", "legales", DEFAULT_LEGALE + ".yml") unless File.exist? file_name YAML.load_file(file_name) end + + # Return the HTTP client to use + def self.http_client + @http_client ||= Faraday.new + end + + # Set the HTTP client to use + def self.http_client=(client) + @http_client = client + end end -- 2.43.2