Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Validating that minutes must be between 0 and 60 range

  • Loading branch information...
commit 1adac684cc7ea01dd15301ac4a011a66f3b96dc8 1 parent 4c737e1
@nateklaiber nateklaiber authored
View
7 lib/geo_position/conversion/dms.rb
@@ -13,12 +13,12 @@ module Conversion
# => -12.061783333333333
#
class Dms
+ ALLOWED_MINUTES = (0.0..60.0)
ALLOWED_DEGREES = (0.0..360.0)
ALLOWED_DIRECTIONS = %w( N n E e S s W w )
MINUTES_CONVERSION = 60
SECONDS_CONVERSION = 3600
-
# Creates a new instance of the DMS conversion object
#
# @param degrees [String,Integer]
@@ -31,6 +31,7 @@ def initialize(degrees, minutes, seconds, direction)
raise GeoPosition::Error::InvalidDirectionError.new("Please provided a direction of N, S, E, or W") unless valid_direction?(direction)
raise GeoPosition::Error::InvalidFloatError.new("Arguments could not be coerced to a float") unless valid_floats?([degrees, minutes, seconds])
raise GeoPosition::Error::InvalidDegreesError.new("Degrees must be between 0 and 360. %s was provided" % [degrees]) unless valid_degrees?(degrees)
+ raise GeoPosition::Error::InvalidMinutesError.new("Minutes must be between 0 and 60. %s was provided" % [minutes]) unless valid_minutes?(minutes)
@degrees = degrees # Can only be between 0 and 360
@minutes = minutes
@@ -94,6 +95,10 @@ def valid_degrees?(deg)
ALLOWED_DEGREES.include?(deg.to_f.abs)
end
+ def valid_minutes?(min)
+ ALLOWED_MINUTES.include?(min.to_f.abs)
+ end
+
def convert!
result = (self.degrees + ((self.minutes/MINUTES_CONVERSION) + (self.seconds/SECONDS_CONVERSION)))
if negative? then -(result) else result end
View
1  lib/geo_position/error.rb
@@ -3,5 +3,6 @@ module Error
autoload :InvalidDirectionError, File.join(File.dirname(__FILE__), 'error/invalid_direction_error')
autoload :InvalidFloatError, File.join(File.dirname(__FILE__), 'error/invalid_float_error')
autoload :InvalidDegreesError, File.join(File.dirname(__FILE__), 'error/invalid_degrees_error')
+ autoload :InvalidMinutesError, File.join(File.dirname(__FILE__), 'error/invalid_minutes_error')
end
end
View
6 lib/geo_position/error/invalid_minutes_error.rb
@@ -0,0 +1,6 @@
+module GeoPosition
+ module Error
+ class InvalidMinutesError < StandardError
+ end
+ end
+end
View
4 spec/conversion/dms_spec.rb
@@ -23,6 +23,10 @@
it "raises an exception if degrees are greater than 360" do
lambda{ described_class.new(361, 12, 123, 'n') }.should raise_error(GeoPosition::Error::InvalidDegreesError)
end
+
+ it "raises an exception if minutes are greater than 60" do
+ lambda { described_class.new(12, 61, 12, 'n') }.should raise_error(GeoPosition::Error::InvalidMinutesError)
+ end
end
it "coerces the degrees to a float" do

0 comments on commit 1adac68

Please sign in to comment.
Something went wrong with that request. Please try again.