]> git.openstreetmap.org Git - rails.git/blobdiff - lib/osm.rb
Use Faraday in place of Net::HTTP so we can mock responses
[rails.git] / lib / osm.rb
index 1d08964171f3782d095da64ae65ab5c035f40539..6c54caadf935e0085d709aa8fbc7b3a342b5b841 100644 (file)
@@ -1,14 +1,14 @@
 # The OSM module provides support functions for OSM.
 module OSM
-  require 'time'
-  require 'rexml/parsers/sax2parser'
-  require 'rexml/text'
-  require 'xml/libxml'
+  require "time"
+  require "rexml/parsers/sax2parser"
+  require "rexml/text"
+  require "xml/libxml"
 
   if defined?(SystemTimer)
     Timer = SystemTimer
   else
-    require 'timeout'
+    require "timeout"
     Timer = Timeout
   end
 
@@ -482,40 +482,38 @@ module OSM
     def get_xml_doc
       doc = XML::Document.new
       doc.encoding = XML::Encoding::UTF_8
-      root = XML::Node.new 'osm'
-      root['version'] = API_VERSION.to_s
-      root['generator'] = GENERATOR
-      root['copyright'] = COPYRIGHT_OWNER
-      root['attribution'] = ATTRIBUTION_URL
-      root['license'] =  LICENSE_URL
+      root = XML::Node.new "osm"
+      root["version"] = API_VERSION.to_s
+      root["generator"] = GENERATOR
+      root["copyright"] = COPYRIGHT_OWNER
+      root["attribution"] = ATTRIBUTION_URL
+      root["license"] =  LICENSE_URL
       doc.root = root
       doc
     end
   end
 
-  def self.IPToCountry(ip_address)
+  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
     end
 
     return nil
-  rescue Exception
+  rescue StandardError
     return nil
   end
 
-  def self.IPLocation(ip_address)
-    code = OSM.IPToCountry(ip_address)
+  def self.ip_location(ip_address)
+    code = OSM.ip_to_country(ip_address)
 
     if code && country = Country.find_by_code(code)
       return { :minlon => country.min_lon, :minlat => country.min_lat, :maxlon => country.max_lon, :maxlat => country.max_lat }
@@ -533,8 +531,8 @@ module OSM
 
   # Construct a random token of a given length
   def self.make_token(length = 30)
-    chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
-    token = ''
+    chars = "abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+    token = ""
 
     length.times do
       token += chars[(rand * chars.length).to_i].chr
@@ -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