geo_position 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/geo_position/conversion/dms.rb +32 -6
- data/lib/geo_position/error/invalid_degrees_error.rb +6 -0
- data/lib/geo_position/error/invalid_direction_error.rb +6 -0
- data/lib/geo_position/error/invalid_float_error.rb +6 -0
- data/lib/geo_position/error.rb +7 -0
- data/lib/geo_position/version.rb +1 -1
- data/lib/geo_position.rb +3 -0
- data/spec/conversion/dms_spec.rb +32 -6
- metadata +7 -3
@@ -13,13 +13,11 @@ module GeoPosition
|
|
13
13
|
# => -12.061783333333333
|
14
14
|
#
|
15
15
|
class Dms
|
16
|
-
# Error that is raised if invalid directions are provided
|
17
|
-
InvalidDirectionError = Class.new(StandardError)
|
18
|
-
|
19
16
|
ALLOWED_DIRECTIONS = %w( N n E e S s W w )
|
20
17
|
MINUTES_CONVERSION = 60
|
21
18
|
SECONDS_CONVERSION = 3600
|
22
19
|
|
20
|
+
|
23
21
|
# Creates a new instance of the DMS conversion object
|
24
22
|
#
|
25
23
|
# @param degrees [String,Integer]
|
@@ -29,35 +27,55 @@ module GeoPosition
|
|
29
27
|
#
|
30
28
|
# @return [void]
|
31
29
|
def initialize(degrees, minutes, seconds, direction)
|
32
|
-
raise InvalidDirectionError.new("Please provided a direction of N, S, E, or W") unless valid_direction?(direction)
|
30
|
+
raise GeoPosition::Error::InvalidDirectionError.new("Please provided a direction of N, S, E, or W") unless valid_direction?(direction)
|
31
|
+
raise GeoPosition::Error::InvalidFloatError.new("Arguments could not be coerced to a float") unless valid_floats?([degrees, minutes, seconds])
|
32
|
+
raise GeoPosition::Error::InvalidDegreesError.new("Degrees must be between 0 and 360. %s was provided" % [degrees]) unless valid_degrees?(degrees)
|
33
33
|
|
34
|
-
@degrees = degrees
|
34
|
+
@degrees = degrees # Can only be between 0 and 360
|
35
35
|
@minutes = minutes
|
36
36
|
@seconds = seconds
|
37
37
|
|
38
38
|
@direction = direction
|
39
39
|
end
|
40
40
|
|
41
|
+
# Returns the coerced degrees
|
42
|
+
#
|
43
|
+
# @return [Float] Positive float of the provided degrees
|
41
44
|
def degrees
|
42
|
-
@degrees.to_f
|
45
|
+
@degrees.to_f.abs
|
43
46
|
end
|
44
47
|
|
48
|
+
# Returns the coerced minutes
|
49
|
+
#
|
50
|
+
# @return [Float]
|
45
51
|
def minutes
|
46
52
|
@minutes.to_f
|
47
53
|
end
|
48
54
|
|
55
|
+
# Returns the coerced seconds
|
56
|
+
#
|
57
|
+
# @return [Float]
|
49
58
|
def seconds
|
50
59
|
@seconds.to_f
|
51
60
|
end
|
52
61
|
|
62
|
+
# Returns the uppercased direction
|
63
|
+
#
|
64
|
+
# @return [String] Uppercase direction
|
53
65
|
def direction
|
54
66
|
@direction.to_s.upcase
|
55
67
|
end
|
56
68
|
|
69
|
+
# Returns the formatted string
|
70
|
+
#
|
71
|
+
# @return [String]
|
57
72
|
def to_s
|
58
73
|
"%s deg %s' %s\" %s" % [self.degrees.to_i, self.minutes.to_i, self.seconds, self.direction]
|
59
74
|
end
|
60
75
|
|
76
|
+
# Returns the converted DMS to a float for use with latitude/longitude
|
77
|
+
#
|
78
|
+
# @return [Float]
|
61
79
|
def to_f
|
62
80
|
convert!
|
63
81
|
end
|
@@ -67,6 +85,14 @@ module GeoPosition
|
|
67
85
|
ALLOWED_DIRECTIONS.include?(dir[0,1])
|
68
86
|
end
|
69
87
|
|
88
|
+
def valid_floats?(collection)
|
89
|
+
collection.all? { |arg| arg.respond_to?(:to_f) }
|
90
|
+
end
|
91
|
+
|
92
|
+
def valid_degrees?(deg)
|
93
|
+
deg.to_f.abs.between?(0.0, 360.0)
|
94
|
+
end
|
95
|
+
|
70
96
|
def convert!
|
71
97
|
result = (self.degrees + ((self.minutes/MINUTES_CONVERSION) + (self.seconds/SECONDS_CONVERSION)))
|
72
98
|
if negative? then -(result) else result end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
module GeoPosition
|
2
|
+
module Error
|
3
|
+
autoload :InvalidDirectionError, File.join(File.dirname(__FILE__), 'error/invalid_direction_error')
|
4
|
+
autoload :InvalidFloatError, File.join(File.dirname(__FILE__), 'error/invalid_float_error')
|
5
|
+
autoload :InvalidDegreesError, File.join(File.dirname(__FILE__), 'error/invalid_degrees_error')
|
6
|
+
end
|
7
|
+
end
|
data/lib/geo_position/version.rb
CHANGED
data/lib/geo_position.rb
CHANGED
@@ -3,6 +3,9 @@ root = File.expand_path(File.dirname(__FILE__))
|
|
3
3
|
# Version
|
4
4
|
require File.join(root, 'geo_position', 'version')
|
5
5
|
|
6
|
+
# Errors
|
7
|
+
require File.join(root, 'geo_position', 'error')
|
8
|
+
|
6
9
|
# Conversions
|
7
10
|
require File.join(root, 'geo_position', 'conversion')
|
8
11
|
require File.join(root, 'geo_position', 'conversion', 'dms')
|
data/spec/conversion/dms_spec.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
# With negative numbers
|
2
|
-
# With nil
|
3
|
-
# With empty strings
|
4
|
-
# With strings
|
5
1
|
require 'spec_helper'
|
6
2
|
|
7
3
|
describe GeoPosition::Conversion::Dms do
|
@@ -15,8 +11,18 @@ describe GeoPosition::Conversion::Dms do
|
|
15
11
|
subject.should respond_to(:to_s)
|
16
12
|
end
|
17
13
|
|
18
|
-
|
19
|
-
|
14
|
+
context('Error Handling') do
|
15
|
+
it "raises an exception if invalid direction is given" do
|
16
|
+
lambda { described_class.new(12,12,23,'r') }.should raise_error(GeoPosition::Error::InvalidDirectionError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "raises an InvalidFloatError if any arguments can't be coerced to a float" do
|
20
|
+
lambda { described_class.new([12], [3], [42.42], 'w') }.should raise_error(GeoPosition::Error::InvalidFloatError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises an exception if degrees are greater than 360" do
|
24
|
+
lambda{ described_class.new(361, 12, 123, 'n') }.should raise_error(GeoPosition::Error::InvalidDegreesError)
|
25
|
+
end
|
20
26
|
end
|
21
27
|
|
22
28
|
it "coerces the degrees to a float" do
|
@@ -44,6 +50,26 @@ describe GeoPosition::Conversion::Dms do
|
|
44
50
|
subject.to_f.should eql(-12.061783333333333)
|
45
51
|
end
|
46
52
|
|
53
|
+
it "returns 0's if nil is provided" do
|
54
|
+
gp = described_class.new(nil, nil, nil, 'N')
|
55
|
+
gp.to_f.should eql(0.0)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns 0's if empty strings are provided" do
|
59
|
+
gp = described_class.new('', '', '', 'N')
|
60
|
+
gp.to_f.should eql(0.0)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "ensures the degrees are always positive" do
|
64
|
+
gp = described_class.new(-12, 3, 42.42, 'w')
|
65
|
+
gp.to_f.should eql(-12.061783333333333)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "coerces string arguments to floats" do
|
69
|
+
gp = described_class.new('12', '3', '42.42', 'w')
|
70
|
+
gp.to_f.should eql(-12.061783333333333)
|
71
|
+
end
|
72
|
+
|
47
73
|
context("North") do
|
48
74
|
subject { described_class.new(12,3,42.42,'n') }
|
49
75
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_position
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -93,6 +93,10 @@ files:
|
|
93
93
|
- lib/geo_position.rb
|
94
94
|
- lib/geo_position/conversion.rb
|
95
95
|
- lib/geo_position/conversion/dms.rb
|
96
|
+
- lib/geo_position/error.rb
|
97
|
+
- lib/geo_position/error/invalid_degrees_error.rb
|
98
|
+
- lib/geo_position/error/invalid_direction_error.rb
|
99
|
+
- lib/geo_position/error/invalid_float_error.rb
|
96
100
|
- lib/geo_position/parser.rb
|
97
101
|
- lib/geo_position/version.rb
|
98
102
|
- reload_yard
|
@@ -112,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
116
|
version: '0'
|
113
117
|
segments:
|
114
118
|
- 0
|
115
|
-
hash:
|
119
|
+
hash: 438636080150112414
|
116
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
121
|
none: false
|
118
122
|
requirements:
|
@@ -121,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
125
|
version: '0'
|
122
126
|
segments:
|
123
127
|
- 0
|
124
|
-
hash:
|
128
|
+
hash: 438636080150112414
|
125
129
|
requirements: []
|
126
130
|
rubyforge_project:
|
127
131
|
rubygems_version: 1.8.24
|