libgeo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +13 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/libgeo.rb +24 -0
- data/lib/libgeo/coordinate.rb +187 -0
- data/lib/libgeo/coordinate/class_methods.rb +243 -0
- data/lib/libgeo/coordinate/hemi_helpers.rb +80 -0
- data/lib/libgeo/coordinate/presenters.rb +67 -0
- data/lib/libgeo/formatter.rb +61 -0
- data/lib/libgeo/formatter/compiler.rb +128 -0
- data/lib/libgeo/formatter/evaluator.rb +53 -0
- data/lib/libgeo/latitude.rb +36 -0
- data/lib/libgeo/longitude.rb +48 -0
- data/lib/libgeo/version.rb +3 -0
- data/libgeo.gemspec +23 -0
- data/spec/shared_examples/coordinate_attrs.rb +74 -0
- data/spec/shared_examples/coordinate_inputs.rb +127 -0
- data/spec/shared_examples/coordinate_presenters.rb +16 -0
- data/spec/shared_examples/coordinate_validation.rb +89 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/unit/libgeo/coordinate_spec.rb +82 -0
- data/spec/unit/libgeo/formatter/compiler_spec.rb +76 -0
- data/spec/unit/libgeo/formatter/evaluator_spec.rb +69 -0
- data/spec/unit/libgeo/formatter_spec.rb +34 -0
- data/spec/unit/libgeo/latitude_spec.rb +378 -0
- data/spec/unit/libgeo/longitude_spec.rb +377 -0
- metadata +114 -0
data/libgeo.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'libgeo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'libgeo'
|
8
|
+
spec.version = Libgeo::VERSION
|
9
|
+
spec.authors = ['Andrey Savchenko']
|
10
|
+
spec.email = ['andrey@aejis.eu']
|
11
|
+
spec.summary = %q{Collection of geographical primitives}
|
12
|
+
spec.description = %q{Collection of geographical primitives}
|
13
|
+
spec.homepage = 'https://github.com/Ptico/libgeo'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
shared_examples_for 'Coordinate#degrees' do
|
2
|
+
subject { instance.degrees }
|
3
|
+
|
4
|
+
context 'when int' do
|
5
|
+
let(:degrees) { 48 }
|
6
|
+
|
7
|
+
it { expect(subject).to eql(48) }
|
8
|
+
it { expect(subject).to be_an(Integer) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when float' do
|
12
|
+
let(:degrees) { 48.0 }
|
13
|
+
|
14
|
+
it { expect(subject).to eql(48) }
|
15
|
+
it { expect(subject).to be_an(Integer) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when string' do
|
19
|
+
let(:degrees) { '48' }
|
20
|
+
|
21
|
+
it { expect(subject).to eql(48) }
|
22
|
+
it { expect(subject).to be_an(Integer) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
shared_examples_for 'Coordinate#minutes' do
|
27
|
+
subject { instance.minutes }
|
28
|
+
|
29
|
+
context 'when int' do
|
30
|
+
let(:minutes) { 4 }
|
31
|
+
|
32
|
+
it { expect(subject).to eql(4) }
|
33
|
+
it { expect(subject).to be_an(Integer) }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when float' do
|
37
|
+
let(:minutes) { 4.0 }
|
38
|
+
|
39
|
+
it { expect(subject).to eql(4) }
|
40
|
+
it { expect(subject).to be_an(Integer) }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when string' do
|
44
|
+
let(:minutes) { '4' }
|
45
|
+
|
46
|
+
it { expect(subject).to eql(4) }
|
47
|
+
it { expect(subject).to be_an(Integer) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
shared_examples_for 'Coordinate#seconds' do
|
52
|
+
subject { instance.seconds }
|
53
|
+
|
54
|
+
context 'when int' do
|
55
|
+
let(:seconds) { 15 }
|
56
|
+
|
57
|
+
it { expect(subject).to eql(15.0) }
|
58
|
+
it { expect(subject).to be_a(Float) }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when float' do
|
62
|
+
let(:seconds) { 15.7836 }
|
63
|
+
|
64
|
+
it { expect(subject).to eql(15.7836) }
|
65
|
+
it { expect(subject).to be_a(Float) }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when string' do
|
69
|
+
let(:seconds) { '15.7836' }
|
70
|
+
|
71
|
+
it { expect(subject).to eql(15.7836) }
|
72
|
+
it { expect(subject).to be_a(Float) }
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
shared_examples_for 'Coordinate#inputs' do
|
2
|
+
|
3
|
+
describe '.dms different notations' do
|
4
|
+
let(:instance) { described_class.dms(input) }
|
5
|
+
|
6
|
+
context 'only degrees' do
|
7
|
+
let(:input) { '58' }
|
8
|
+
|
9
|
+
it { expect(instance.degrees).to eql(58) }
|
10
|
+
it { expect(instance.minutes).to eql(0) }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'without seconds' do
|
14
|
+
let(:input) { '58 45'}
|
15
|
+
|
16
|
+
it { expect(instance.degrees).to eql(58) }
|
17
|
+
it { expect(instance.minutes).to eql(45) }
|
18
|
+
it { expect(instance.seconds).to eql(0.0) }
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'different formats' do
|
23
|
+
context '51 52 53' do
|
24
|
+
let(:input) { '51 52 53' }
|
25
|
+
|
26
|
+
it { expect(instance.degrees).to eql(51) }
|
27
|
+
it { expect(instance.minutes).to eql(52) }
|
28
|
+
it { expect(instance.seconds).to eql(53.0) }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "51'52'53" do
|
32
|
+
let(:input) { "51'52'53" }
|
33
|
+
|
34
|
+
it { expect(instance.degrees).to eql(51) }
|
35
|
+
it { expect(instance.minutes).to eql(52) }
|
36
|
+
it { expect(instance.seconds).to eql(53.0) }
|
37
|
+
end
|
38
|
+
|
39
|
+
context '51 52 53 HEMI' do
|
40
|
+
let(:input) { "51 52 53 #{hemisphere}" }
|
41
|
+
|
42
|
+
it { expect(instance.degrees).to eql(51) }
|
43
|
+
it { expect(instance.minutes).to eql(52) }
|
44
|
+
it { expect(instance.seconds).to eql(53.0) }
|
45
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
46
|
+
end
|
47
|
+
|
48
|
+
context '51 52 53, HEMI' do
|
49
|
+
let(:input) { "51 52 53, #{hemisphere}" }
|
50
|
+
|
51
|
+
it { expect(instance.degrees).to eql(51) }
|
52
|
+
it { expect(instance.minutes).to eql(52) }
|
53
|
+
it { expect(instance.seconds).to eql(53.0) }
|
54
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '.nmea different notations' do
|
61
|
+
let(:instance) { described_class.nmea(input) }
|
62
|
+
|
63
|
+
context 'only degrees and minutes' do
|
64
|
+
let(:input) { '03920' }
|
65
|
+
|
66
|
+
it { expect(instance.degrees).to eql(39) }
|
67
|
+
it { expect(instance.minutes).to eql(20) }
|
68
|
+
it { expect(instance.seconds).to eql(0.0) }
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'only degrees and hemisphere' do
|
72
|
+
let(:input) { "03920,#{hemisphere}" }
|
73
|
+
|
74
|
+
it { expect(instance.degrees).to eql(39) }
|
75
|
+
it { expect(instance.minutes).to eql(20) }
|
76
|
+
it { expect(instance.seconds).to eql(0.0) }
|
77
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'different formats' do
|
82
|
+
context '03920 HEMI' do
|
83
|
+
let(:input) { "03920 #{hemisphere}" }
|
84
|
+
|
85
|
+
it { expect(instance.degrees).to eql(39) }
|
86
|
+
it { expect(instance.minutes).to eql(20) }
|
87
|
+
it { expect(instance.seconds).to eql(0.0) }
|
88
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
89
|
+
end
|
90
|
+
|
91
|
+
context '03920,HEMI' do
|
92
|
+
let(:input) { "03920,#{hemisphere}" }
|
93
|
+
|
94
|
+
it { expect(instance.degrees).to eql(39) }
|
95
|
+
it { expect(instance.minutes).to eql(20) }
|
96
|
+
it { expect(instance.seconds).to eql(0.0) }
|
97
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
98
|
+
end
|
99
|
+
|
100
|
+
context '03920, HEMI' do
|
101
|
+
let(:input) { "03920, #{hemisphere}" }
|
102
|
+
|
103
|
+
it { expect(instance.degrees).to eql(39) }
|
104
|
+
it { expect(instance.minutes).to eql(20) }
|
105
|
+
it { expect(instance.seconds).to eql(0.0) }
|
106
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
107
|
+
end
|
108
|
+
|
109
|
+
context '03920.56074 HEMI' do
|
110
|
+
let(:input) { "03920.56074 #{hemisphere}" }
|
111
|
+
|
112
|
+
it { expect(instance.degrees).to eql(39) }
|
113
|
+
it { expect(instance.minutes).to eql(20) }
|
114
|
+
it { expect(instance.seconds).to eql(33.6444) }
|
115
|
+
it { expect(instance.hemisphere).to eql(hemisphere) }
|
116
|
+
end
|
117
|
+
|
118
|
+
context '03920' do
|
119
|
+
let(:input) { '03920' }
|
120
|
+
|
121
|
+
it { expect(instance.degrees).to eql(39) }
|
122
|
+
it { expect(instance.minutes).to eql(20) }
|
123
|
+
it { expect(instance.seconds).to eql(0.0) }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
shared_examples_for 'Coordinate#to_hash' do
|
2
|
+
subject { instance.to_hash }
|
3
|
+
let(:result) do
|
4
|
+
{
|
5
|
+
type: instance.type,
|
6
|
+
degrees: degrees,
|
7
|
+
minutes: minutes,
|
8
|
+
seconds: seconds,
|
9
|
+
hemisphere: hemi
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
it do
|
14
|
+
expect(subject).to eq(result)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
shared_examples_for 'Coordinate#validation' do
|
2
|
+
describe '.nmea' do
|
3
|
+
let(:instance) { described_class.nmea(input) }
|
4
|
+
|
5
|
+
context 'degrees out of range' do
|
6
|
+
let(:input) { "#{max_degrees + 1}.2343242" }
|
7
|
+
|
8
|
+
it 'should fail' do
|
9
|
+
expect { instance }.to raise_error
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'wrong hemisphere' do
|
14
|
+
let(:input) { "32.43 A" }
|
15
|
+
|
16
|
+
it 'should fail' do
|
17
|
+
expect { instance }.to raise_error
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.dms' do
|
23
|
+
let(:instance) { described_class.dms(input) }
|
24
|
+
|
25
|
+
context 'degrees out of range' do
|
26
|
+
let(:input) { "#{max_degrees + 1}'43'32" }
|
27
|
+
|
28
|
+
it 'should fail' do
|
29
|
+
expect { instance }.to raise_error
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'minutes out of range' do
|
34
|
+
let(:input) { "10'90'32" }
|
35
|
+
|
36
|
+
it 'should fail' do
|
37
|
+
expect { instance }.to raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'seconds out of range' do
|
42
|
+
let(:input) { "10'43'90" }
|
43
|
+
|
44
|
+
it 'should fail' do
|
45
|
+
expect { instance }.to raise_error
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'wrong hemisphere' do
|
50
|
+
let(:input) { "10'43'32, A" }
|
51
|
+
|
52
|
+
it 'should fail' do
|
53
|
+
expect { instance }.to raise_error
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '.decimal' do
|
59
|
+
let(:instance) { described_class.decimal(input) }
|
60
|
+
|
61
|
+
context 'degrees out of range' do
|
62
|
+
let(:input) { max_degrees + 1 }
|
63
|
+
|
64
|
+
it 'should fail' do
|
65
|
+
expect { instance }.to raise_error
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '.degrees_minutes' do
|
71
|
+
let(:instance) { described_class.degrees_minutes(*input) }
|
72
|
+
|
73
|
+
context 'degrees out of range' do
|
74
|
+
let(:input) { [max_degrees + 1, 59] }
|
75
|
+
|
76
|
+
it 'should fail' do
|
77
|
+
expect { instance }.to raise_error
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'minutes out of range' do
|
82
|
+
let(:input) { [max_degrees - 1, 61] }
|
83
|
+
|
84
|
+
it 'should fail' do
|
85
|
+
expect { instance }.to raise_error
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter '/spec/'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'libgeo'
|
9
|
+
|
10
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
11
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
12
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
13
|
+
# loaded once.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
|
17
|
+
Dir[File.join(Dir.pwd, 'spec/shared_examples/**/*.rb')].each { |f| require f }
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# Limit the spec run to only specs with the focus metadata. If no specs have
|
21
|
+
# the filtering metadata and `run_all_when_everything_filtered = true` then
|
22
|
+
# all specs will run.
|
23
|
+
#config.filter_run :focus
|
24
|
+
|
25
|
+
# Run all specs when none match the provided filter. This works well in
|
26
|
+
# conjunction with `config.filter_run :focus`, as it will run the entire
|
27
|
+
# suite when no specs have `:filter` metadata.
|
28
|
+
#config.run_all_when_everything_filtered = true
|
29
|
+
|
30
|
+
# Run specs in random order to surface order dependencies. If you find an
|
31
|
+
# order dependency and want to debug it, you can fix the order by providing
|
32
|
+
# the seed, which is printed after each run.
|
33
|
+
# --seed 1234
|
34
|
+
config.mock_with :rspec do |mocks|
|
35
|
+
mocks.verify_doubled_constant_names = true
|
36
|
+
end
|
37
|
+
|
38
|
+
config.order = 'random'
|
39
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Libgeo::Coordinate do
|
6
|
+
let(:instance) do
|
7
|
+
described_class.new(
|
8
|
+
params[:hemi],
|
9
|
+
params[:degrees],
|
10
|
+
params[:minutes],
|
11
|
+
params[:seconds]
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:second_instance) do
|
16
|
+
described_class.new(
|
17
|
+
second_params[:hemi],
|
18
|
+
second_params[:degrees],
|
19
|
+
second_params[:minutes],
|
20
|
+
second_params[:seconds]
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:params) do
|
25
|
+
{
|
26
|
+
hemi: :N,
|
27
|
+
degrees: 10,
|
28
|
+
minutes: 10,
|
29
|
+
seconds: 5.6
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'equality' do
|
34
|
+
context 'when attributes same' do
|
35
|
+
let(:second_params) { params }
|
36
|
+
|
37
|
+
it 'coordinates should be same' do
|
38
|
+
expect(instance.eql?(second_instance)).to eql(true)
|
39
|
+
expect(instance == second_instance).to eql(true)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when different' do
|
44
|
+
context 'degrees' do
|
45
|
+
let(:second_params) { params.merge(degrees: 20) }
|
46
|
+
|
47
|
+
it 'should not be equal' do
|
48
|
+
expect(instance.eql?(second_instance)).to eql(false)
|
49
|
+
expect(instance == second_instance).to eql(false)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'minutes' do
|
54
|
+
let(:second_params) { params.merge(minutes: 30) }
|
55
|
+
|
56
|
+
it 'should not be equal' do
|
57
|
+
expect(instance.eql?(second_instance)).to eql(false)
|
58
|
+
expect(instance == second_instance).to eql(false)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'seconds' do
|
63
|
+
let(:second_params) { params.merge(seconds: 6.6) }
|
64
|
+
|
65
|
+
it 'should not be equal' do
|
66
|
+
expect(instance.eql?(second_instance)).to eql(false)
|
67
|
+
expect(instance == second_instance).to eql(false)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'hemisphere' do
|
72
|
+
let(:second_params) { params.merge(hemi: :S) }
|
73
|
+
|
74
|
+
it 'should not be equal' do
|
75
|
+
expect(instance.eql?(second_instance)).to eql(false)
|
76
|
+
expect(instance == second_instance).to eql(false)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|