Extract common code for parsing floats

This commit is contained in:
Tom Hughes 2013-06-24 22:44:17 +01:00
parent 9d2fed811f
commit f03c8637f7
3 changed files with 11 additions and 20 deletions

View file

@ -59,16 +59,8 @@ class NotesController < ApplicationController
raise OSM::APIBadUserInput.new("No text was given") if params[:text].blank?
# Extract the arguments
begin
lon = Float(params[:lon])
rescue
raise OSM::APIBadUserInput.new("lon was not a number")
end
begin
lat = Float(params[:lat])
rescue
raise OSM::APIBadUserInput.new("lat was not a number")
end
lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
comment = params[:text]
# Include in a transaction to ensure that there is always a note_comment for every note

View file

@ -83,16 +83,8 @@ class Node < ActiveRecord::Base
raise OSM::APIBadXMLError.new("node", pt, "lat missing") if pt['lat'].nil?
raise OSM::APIBadXMLError.new("node", pt, "lon missing") if pt['lon'].nil?
begin
node.lat = Float(pt['lat'])
rescue
raise OSM::APIBadXMLError.new("node", pt, "lat not a number")
end
begin
node.lon = Float(pt['lon'])
rescue
raise OSM::APIBadXMLError.new("node", pt, "lon not a number")
end
node.lat = OSM.parse_float(pt['lat'], OSM::APIBadXMLError, "node", pt, "lat not a number")
node.lon = OSM.parse_float(pt['lon'], OSM::APIBadXMLError, "node", pt, "lon not a number")
raise OSM::APIBadXMLError.new("node", pt, "Changeset id is missing") if pt['changeset'].nil?
node.changeset_id = pt['changeset'].to_i

View file

@ -550,6 +550,13 @@ module OSM
return nil
end
# Parse a float, raising a specified exception on failure
def self.parse_float(str, klass, *args)
Float(str)
rescue
raise klass.new(*args)
end
# Construct a random token of a given length
def self.make_token(length = 30)
chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'