]> git.openstreetmap.org Git - rails.git/blobdiff - lib/osm.rb
Potlatch 1.0
[rails.git] / lib / osm.rb
index f6646503d0e37e0d68e04efbaf01755be4c6195e..885d1e35821961d998e85bf78e75eff081a9ff57 100644 (file)
@@ -13,6 +13,10 @@ module OSM
     def render_opts
       { :text => "Generic API Error", :status => :internal_server_error, :content_type => "text/plain" }
     end
+
+    def to_s
+      "Generic API Error"
+    end
   end
 
   # Raised when an API object is not found.
@@ -31,12 +35,22 @@ module OSM
     def render_opts
       { :text => "Precondition failed: #{@message}", :status => :precondition_failed, :content_type => "text/plain" }
     end
+
+    def to_s
+      "Precondition failed: #{@message}"
+    end
   end
 
   # Raised when to delete an already-deleted object.
   class APIAlreadyDeletedError < APIError
+    def initialize(object = "object", object_id = "")
+      @object, @object_id = object, object_id
+    end
+    
+    attr_reader :object, :object_id
+    
     def render_opts
-      { :text => "The object has already been deleted", :status => :gone, :content_type => "text/plain" }
+      { :text => "The #{object} with the id #{object_id} has already been deleted", :status => :gone, :content_type => "text/plain" }
     end
   end
 
@@ -65,6 +79,10 @@ module OSM
     def render_opts
       { :text => "You need to supply a changeset to be able to make a change", :status => :conflict, :content_type => "text/plain" }
     end
+    
+    def to_s
+       "You need to supply a changeset to be able to make a change"
+    end
   end
 
   # Raised when a diff is uploaded containing many changeset IDs which don't match
@@ -115,10 +133,13 @@ module OSM
     attr_reader :provided, :latest, :id, :type
 
     def render_opts
-      { :text => "Version mismatch: Provided " + provided.to_s +
-        ", server had: " + latest.to_s + " of " + type + " " + id.to_s, 
+      { :text => "Version mismatch: Provided #{provided}, server had: #{latest} of #{type} #{id}",
         :status => :conflict, :content_type => "text/plain" }
     end
+    
+    def to_s
+       "Version mismatch: Provided #{provided}, server had: #{latest} of #{type} #{id}"
+    end
   end
 
   # raised when a two tags have a duplicate key string in an element.
@@ -163,6 +184,30 @@ module OSM
     end
   end
 
+  ##
+  # raised when an API call is made using a method not supported on that URI
+  class APIBadMethodError < APIError
+    def initialize(supported_method)
+      @supported_method = supported_method
+    end
+
+    def render_opts
+      { :text => "Only method #{@supported_method} is supported on this URI.", :status => :method_not_allowed }
+    end
+  end
+
+  ##
+  # raised when an API call takes too long
+  class APITimeoutError < APIError
+    def render_opts
+      { :text => "Request timed out", :status => :request_timeout }
+    end
+
+    def to_s
+      "Request timed out"
+    end
+  end
+
   # Helper methods for going to/from mercator and lat/lng.
   class Mercator
     include Math
@@ -240,7 +285,7 @@ module OSM
   class GeoRSS
     def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
       @doc = XML::Document.new
-      @doc.encoding = 'UTF-8' 
+      @doc.encoding = XML::Encoding::UTF_8
 
       rss = XML::Node.new 'rss'
       @doc.root = rss
@@ -326,7 +371,7 @@ module OSM
   class API
     def get_xml_doc
       doc = XML::Document.new
-      doc.encoding = 'UTF-8' 
+      doc.encoding = XML::Encoding::UTF_8
       root = XML::Node.new 'osm'
       root['version'] = API_VERSION
       root['generator'] = GENERATOR
@@ -340,16 +385,8 @@ module OSM
       Net::HTTP.start('api.hostip.info') do |http|
         country = http.get("/country.php?ip=#{ip_address}").body
         country = "GB" if country == "UK"
-        Net::HTTP.start('ws.geonames.org') do |http|
-          xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
-          xml.elements.each("geonames/country") do |ele|
-            minlon = ele.get_text("bBoxWest").to_s
-            minlat = ele.get_text("bBoxSouth").to_s
-            maxlon = ele.get_text("bBoxEast").to_s
-            maxlat = ele.get_text("bBoxNorth").to_s
-            return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
-          end
-        end
+        country = Country.find_by_code(country)
+        return { :minlon => country.min_lon, :minlat => country.min_lat, :maxlon => country.max_lon, :maxlat => country.max_lat }
       end
     end