X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/9b4271ed8c0577d23c6d77c4be0ed8a8082dc86c..d4954bc7b1bc06431bea4e97af401e3ea6acd0e6:/lib/osm.rb diff --git a/lib/osm.rb b/lib/osm.rb index eb5afd562..885d1e358 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -43,8 +43,14 @@ module OSM # 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 @@ -127,13 +133,12 @@ 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.to_s + ", server had: " + latest.to_s + " of " + type + " " + id.to_s + "Version mismatch: Provided #{provided}, server had: #{latest} of #{type} #{id}" end end @@ -179,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 @@ -356,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