plus_codes 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7430998fc47b7df06e1254f74ca980b1c33fbd12
4
- data.tar.gz: f503a83dd1b483d57d2dc917b3abe5d95dc22ee0
3
+ metadata.gz: 1da7f815765ecb2206cb0649c9aa38dd3fa74f02
4
+ data.tar.gz: 31e1d4218ecd47d5430e73b47760ae8acda9d163
5
5
  SHA512:
6
- metadata.gz: 114f48473a81ead5f78179d0293892ff4c7f43b891b5c4cb9f4d4524806a80bacaf8242e042ee1f866c665f025f7381fc2a568b33b83bbe544b605e81a7519ab
7
- data.tar.gz: c289c5db96763ff688bc8d6ed77d26b9442e1f248a29b0bd92099ecb659192239f4ec825a9780e240cb08b6a8cf019a08a6c186dfb005a1217d0122079e7843d
6
+ metadata.gz: dde33abf4454355f14b1497c82266e97c2e8cfe5b7a6cf0a8567f133ed0d5d225c433cd8a87029a5c8aab87f10f524095254b08f00e035f97c4224c4f92bb192
7
+ data.tar.gz: 6e01e7ed5b9bfe233a819a6195122e7d5b0af0f7ef53ae3c5f5d962b9d9d8c67e4165c10e1e201a7fdd4cc48851dbc0aa1c057246d3671646d7e93934049f214
data/lib/plus_codes.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # Plus+Codes is a Ruby implementation of Google Open Location Code(Plus+Codes).
2
+ #
2
3
  # @author We-Ming Wu
3
4
  module PlusCodes
4
5
 
@@ -12,6 +12,7 @@ module PlusCodes
12
12
  # latitude_center: The latitude of the center in degrees.
13
13
  # longitude_center: The longitude of the center in degrees.
14
14
  # code_length: The number of significant characters that were in the code.
15
+ #
15
16
  # @author We-Ming Wu
16
17
  class CodeArea
17
18
  attr_accessor :latitude_lo, :longitude_lo, :latitude_hi, :longitude_hi,
@@ -4,6 +4,7 @@ require 'plus_codes/code_area'
4
4
  module PlusCodes
5
5
 
6
6
  # [OpenLocationCode] implements the Google Open Location Code(Plus+Codes) algorithm.
7
+ #
7
8
  # @author We-Ming Wu
8
9
  class OpenLocationCode
9
10
 
@@ -19,22 +20,22 @@ module PlusCodes
19
20
  return false if separator_index.nil? ||
20
21
  separator_index != code.rindex(SEPARATOR) ||
21
22
  separator_index > SEPARATOR_POSITION ||
22
- separator_index % 2 == 1
23
+ separator_index.odd?
23
24
 
24
25
  # We can have an even number of padding characters before the separator,
25
26
  # but then it must be the final character.
26
27
  if code.include?(PADDING)
27
28
  # Not allowed to start with them!
28
- return false if code.index(PADDING) == 0
29
+ return false if code.start_with?(PADDING)
29
30
 
30
31
  # There can only be one group and it must have even length.
31
32
  pad_match = /(#{PADDING}+)/.match(code).to_a
32
33
  return false if pad_match.length != 2
33
34
  match = pad_match[1]
34
- return false if match.length % 2 == 1 || match.length > SEPARATOR_POSITION - 2
35
+ return false if match.length.odd? || match.length > SEPARATOR_POSITION - 2
35
36
 
36
37
  # If the code is long enough to end with a separator, make sure it does.
37
- return false if code[code.length - 1] != SEPARATOR
38
+ return false if code[-1] != SEPARATOR
38
39
  end
39
40
 
40
41
  # If there are characters after the separator, make sure there isn't just
@@ -43,7 +44,7 @@ module PlusCodes
43
44
 
44
45
  # Check code contains only valid characters.
45
46
  code.chars.each do |ch|
46
- return false if ch.ord > DECODE.length || DECODE[ch.ord] < -1
47
+ return false if ch.ord >= DECODE.length || DECODE[ch.ord] < -1
47
48
  end
48
49
  true
49
50
  end
@@ -87,8 +88,7 @@ module PlusCodes
87
88
  # @param code_length [Integer] the number of characters in the code, this excludes the separator
88
89
  # @return [String] a plus+codes
89
90
  def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
90
- if code_length < 2 ||
91
- (code_length < SEPARATOR_POSITION && code_length % 2 == 1)
91
+ if code_length < 2 || (code_length < SEPARATOR_POSITION && code_length.odd?)
92
92
  raise ArgumentError, "Invalid Open Location Code length: #{code_length}"
93
93
  end
94
94
 
@@ -96,11 +96,12 @@ module PlusCodes
96
96
  longitude = normalize_longitude(longitude)
97
97
  if latitude == 90
98
98
  latitude = latitude - compute_latitude_precision(code_length).to_f
99
- p latitude
100
99
  end
101
100
  code = encode_pairs(latitude, longitude, [code_length, PAIR_CODE_LENGTH].min)
102
101
  # If the requested length indicates we want grid refined codes.
103
- code += encode_grid(latitude, longitude, code_length - PAIR_CODE_LENGTH) if code_length > PAIR_CODE_LENGTH
102
+ if code_length > PAIR_CODE_LENGTH
103
+ code += encode_grid(latitude, longitude, code_length - PAIR_CODE_LENGTH)
104
+ end
104
105
  code
105
106
  end
106
107
 
@@ -142,7 +143,7 @@ module PlusCodes
142
143
  if full?(short_code)
143
144
  return short_code
144
145
  else
145
- raise ArgumentError, 'ValueError: Passed short code is not valid: ' + short_code
146
+ raise ArgumentError, "ValueError: Passed short code is not valid: #{short_code}"
146
147
  end
147
148
  end
148
149
 
@@ -1,3 +1,3 @@
1
1
  module PlusCodes
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
@@ -64,6 +64,16 @@ class PlusCodesTest < Test::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
+ def test_exceptions
68
+ assert_raise ArgumentError do
69
+ @olc.encode(20, 30, 1)
70
+ end
71
+ assert_raise ArgumentError do
72
+ @olc.recover_nearest('9C3W9QCJ-2VX', 51.3708675, -1.217765625)
73
+ end
74
+ @olc.recover_nearest('9C3W9QCJ+2VX', 51.3708675, -1.217765625)
75
+ end
76
+
67
77
  def read_csv_lines(csv_file)
68
78
  f = File.open(File.join(@test_data_folder_path, csv_file), 'r')
69
79
  f.each_line.lazy.select { |line| line !~ /^\s*#/ }.map { |line| line.chop }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plus_codes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wei-Ming Wu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-15 00:00:00.000000000 Z
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler