geohashing 0.0.1

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.
@@ -0,0 +1,56 @@
1
+ require 'net/http'
2
+ require 'digest'
3
+
4
+ module Geohashing
5
+ # using the peeron API
6
+ # see: http://wiki.xkcd.com/geohashing/Dow_Jones_Industrial_Average
7
+ def last_dow_open
8
+ url = Time.new.strftime("http://carabiner.peeron.com/xkcd/map/data/%Y/%m/%d")
9
+ Net::HTTP.get_response(URI.parse(url)).body.to_f
10
+ end
11
+
12
+ def today
13
+ Time.now.strftime("%Y-%m-%d")
14
+ end
15
+
16
+ def code_today
17
+ "#{today}-#{last_dow_open.to_s}"
18
+ end
19
+
20
+ def md5sum str
21
+ Digest::MD5.hexdigest str
22
+ end
23
+
24
+ def md5split sum
25
+ [sum[0..15], sum[16..31]]
26
+ end
27
+
28
+ # this method slightly modified from ScottKuma's
29
+ # "Ruby Geohasher v1.2" implementation
30
+ # http://wiki.xkcd.com/geohashing/Implementations#Ruby_Geohasher_v1.2
31
+ def hex_to_dec h
32
+ fracLen = h.length
33
+ fracPortion = h[0..(fracLen-1)]
34
+ fracLen = fracPortion.length
35
+ len = (fracLen - 1)
36
+ sum = 0
37
+ (0..len).each do |i|
38
+ numSixteenths = fracPortion[i..i].to_i(16)
39
+ conversionFactor = (16.**(i + 1)).to_f
40
+ conversionFactor = 1./conversionFactor
41
+ sum = sum + ((numSixteenths) * conversionFactor)
42
+ end
43
+ return sum.to_s[2..8]
44
+ end
45
+
46
+ def graticule pos
47
+ pos < 0 ? pos.ceil : pos.floor
48
+ end
49
+
50
+ def geohash lat, long
51
+ lat, long = graticule(lat), graticule(long)
52
+ lat_dec, long_dec = md5split(md5sum(code_today)).map{ |s| hex_to_dec s }
53
+ ["#{lat}.#{lat_dec}".to_f, "#{long}.#{long_dec}".to_f]
54
+ end
55
+ end
56
+
@@ -0,0 +1,4 @@
1
+ module Geohashing
2
+ VERSION = '0.0.1'
3
+ end
4
+
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ include Geohashing
3
+
4
+ class Hasher
5
+ include Geohashing
6
+ end
7
+
8
+ describe Geohashing do
9
+ before(:each) do
10
+ @hasher = Hasher.new
11
+ @hasher.stub(:today) { "2005-05-26" }
12
+ @hasher.stub(:last_dow_open) { 10458.68 }
13
+ end
14
+
15
+ describe "code_today" do
16
+ it "should join the date and DJI" do
17
+ @hasher.code_today.should == "2005-05-26-10458.68"
18
+ end
19
+ end
20
+
21
+ describe "md5sum" do
22
+ it "should correctly encode the string" do
23
+ @hasher.md5sum(@hasher.code_today).should == "db9318c2259923d08b672cb305440f97"
24
+ end
25
+ end
26
+
27
+ describe "md5split" do
28
+ it "should split an md5sum into halves" do
29
+ hsh = "db9318c2259923d08b672cb305440f97"
30
+ md5split(hsh).should == ["db9318c2259923d0", "8b672cb305440f97"]
31
+ end
32
+ end
33
+
34
+ describe "hex_to_dec" do
35
+ it "should convert a hex string to a decimal string" do
36
+ hex = "db9318c2259923d0"
37
+ hex_to_dec(hex).should == "8577132"
38
+ end
39
+ end
40
+
41
+ describe "graticule" do
42
+ it "should round down a positive number" do
43
+ pos = 37.421542
44
+ graticule(pos).should == 37
45
+ end
46
+ it "should round down a negative number" do
47
+ pos = -122.085589
48
+ graticule(pos).should == -122
49
+ end
50
+ end
51
+
52
+ describe "geohash" do
53
+ it "should return the expected value" do
54
+ @hasher.geohash(37.421542, -122.085589).should == [37.8577132, -122.544543]
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'geohashing'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.formatter = 'documentation'
7
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geohashing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Harry Schwartz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70267357745000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70267357745000
25
+ description: Given your coordinates, yields today's geohashing meetup location
26
+ email:
27
+ - hrs@cs.wm.edu
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/geohashing/version.rb
33
+ - lib/geohashing.rb
34
+ - spec/geohashing_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: http://github.com/hrs/geohashing
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.6
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: XKCD geohashing algorithm
60
+ test_files:
61
+ - spec/geohashing_spec.rb
62
+ - spec/spec_helper.rb