]> git.openstreetmap.org Git - rails.git/commitdiff
Use Faraday in place of Net::HTTP so we can mock responses
authorTom Hughes <tom@compton.nu>
Sun, 1 Mar 2015 19:22:46 +0000 (19:22 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 1 Mar 2015 19:22:46 +0000 (19:22 +0000)
Gemfile
Gemfile.lock
app/controllers/geocoder_controller.rb
lib/osm.rb

diff --git a/Gemfile b/Gemfile
index 25de38098883ce6ee179ec6cf4099a23e8eb442c..ae9c96b1c4ee7fc21368bcb8ef928de64d88df04 100644 (file)
--- 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"
index 91d4b662c07b9f6da6fa8d772c0c2759086e6d30..7de1743dcef714eba8a6289d23d598c0ab799db7 100644 (file)
@@ -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
index be4f0570c828c298dedda67fd3446a0aed5686bd..5a59c9eeb159761486c93b14e5913530ac61acbf 100644 (file)
@@ -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)
index 3a0beccdf110d333608bbb5abbb3d727325bf5b1..6c54caadf935e0085d709aa8fbc7b3a342b5b841 100644 (file)
@@ -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