talon 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/talon.rb +52 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 705bf74878a40145f09570db54773d729de73edd
4
+ data.tar.gz: 837daae298468cbbf49cc976bc42e6d2157a1d53
5
+ SHA512:
6
+ metadata.gz: 6bae5fd44aa2ecb11abef2572aa5ba8ded56654d70ede768d4e1132a833b81106b2852741cc84368c2c0faf81edd61d3e60f090f80b577cb4ece0374877e1153
7
+ data.tar.gz: 8fc7aa98a0794c667731b95aa46127b4c01e61b4197b7428098e1b195637bff37674a1edb65029bf3cb43533774608b6c90f7d4da151a93dc3c03a3c85561ab5
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Encode long co-ordinate pairs into a small utf-8 encoded string.
4
+ module Talon
5
+ def self.encode(lat, long)
6
+ # encode the latitude & longitude,
7
+ # split each into 2 hex strings, 4 characters long
8
+ # turn them into unicode characters, and join as string
9
+ [lat, long]
10
+ .map { |x| format('%08x', _encode(x)).scan(/.{4}/) }
11
+ .map { |x| x.map { |y| [y.to_i(16)].pack 'U' } }
12
+ .join
13
+ end
14
+
15
+ def self.decode(str)
16
+ # turn the given string's value into an 8-char hex string
17
+ # decode the integer representation of that
18
+ str.scan(/.{2}/)
19
+ .map { |x| format('%04x%04x', x[0].ord, x[1].ord) }
20
+ .map { |x| _decode(x.to_i(16)) }
21
+ end
22
+
23
+ private
24
+
25
+ def self._encode(loc)
26
+ location = loc.abs # so that the function remains non-destructive
27
+
28
+ encoding = loc >= 0 ? 2_000_000_000 : 0
29
+
30
+ if location > 90
31
+ encoding += 1_000_000_000
32
+ location -= 90
33
+ end
34
+
35
+ encoding + (location * 10_000_000).to_i
36
+ end
37
+
38
+ def self._decode(enc)
39
+ encoding = enc # so that the function remains non-destructive
40
+
41
+ positive = encoding >= 2_000_000_000
42
+ encoding -= 2_000_000_000 if positive
43
+
44
+ over_90 = encoding >= 1_000_000_000
45
+ encoding -= 1_000_000_000 if over_90
46
+
47
+ location = over_90 ? 90 : 0
48
+ location += encoding / 10_000_000.0
49
+
50
+ location * (positive ? 1 : -1)
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Prajjwal Bhandari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Talon encodes long co-ordinate pairs into a small UTF-8 string.
28
+ email: talon@pbhandari.ca
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/talon.rb
34
+ homepage: http://wmak.io/talon
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.2.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Concisely store latitude and longitude
58
+ test_files: []