geocoder-simplified 1.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.
- data/lib/geocoder-simplified.rb +1 -0
- data/lib/geocoder-simplified/base.rb +149 -0
- data/lib/geocoder-simplified/util.rb +45 -0
- data/lib/geocoder-simplified/version.rb +10 -0
- metadata +72 -0
@@ -0,0 +1 @@
|
|
1
|
+
require 'gem_with_extension_example/base'
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# holy grail of redis info:
|
4
|
+
# http://dev.mensfeld.pl/2011/11/using-redis-as-a-temporary-cache-for-data-shared-by-multiple-independent-processes/
|
5
|
+
|
6
|
+
# monkeypatch ruby from the rails underscore addition to String
|
7
|
+
|
8
|
+
class GeocoderSimplified
|
9
|
+
$:.push File.expand_path("..", __FILE__)
|
10
|
+
|
11
|
+
require 'geocoder'
|
12
|
+
require 'redis'
|
13
|
+
require 'util'
|
14
|
+
require 'redis-expiring_counter'
|
15
|
+
|
16
|
+
POSTFIX = "Geocode"
|
17
|
+
UNLIMITED_CALLS = Util::MAX_INT32
|
18
|
+
|
19
|
+
class Configuration # Future Development Stub
|
20
|
+
GOOGLE_API_KEY = ""
|
21
|
+
YAHOO_API_KEY = ""
|
22
|
+
end
|
23
|
+
class GeocoderApi# < RedisTable
|
24
|
+
attr_reader :name, :key, :max_calls_per_day, :symbol, :counter
|
25
|
+
def initialize(name, key, max_calls_per_day)
|
26
|
+
@name = name + POSTFIX
|
27
|
+
@key = key
|
28
|
+
@max_calls_per_day = max_calls_per_day
|
29
|
+
@symbol = name.underscore.to_sym
|
30
|
+
@counter = RedisExpiringCounter.new(@name, Util::SECONDS_PER_DAY, @max_calls_per_day)
|
31
|
+
end
|
32
|
+
def count
|
33
|
+
@counter.count
|
34
|
+
end
|
35
|
+
def dump
|
36
|
+
@counter.dump
|
37
|
+
end
|
38
|
+
def locate(place_name)
|
39
|
+
location = []
|
40
|
+
latitude = 0.0
|
41
|
+
longitude = 0.0
|
42
|
+
|
43
|
+
Geocoder::Configuration.lookup = @symbol # set the geocoder to use this api
|
44
|
+
|
45
|
+
begin
|
46
|
+
warning_text = Util.capture_stderr do
|
47
|
+
location = Geocoder.search(place_name)
|
48
|
+
increment_count
|
49
|
+
end
|
50
|
+
# Write sucess to log here
|
51
|
+
#Rails.logger.warn "Geocoder: " + warning_text if warning_text != ""
|
52
|
+
rescue StandardError => e
|
53
|
+
# Write fail to log here
|
54
|
+
#Rails.logger.error "Geocoder: " + @name + ": "+ e.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
if not (location.nil? or location.count == 0)
|
58
|
+
latitude = location.first.latitude
|
59
|
+
longitude = location.first.longitude
|
60
|
+
end
|
61
|
+
|
62
|
+
return latitude, longitude
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class << self
|
67
|
+
# Attach the default goecoding apis, _in order_. The first added will be the first tried.
|
68
|
+
@@api_list = []
|
69
|
+
@@api_list << GeocoderApi.new("GeocoderCa", "", UNLIMITED_CALLS)
|
70
|
+
@@api_list << GeocoderApi.new("Google", Configuration::GOOGLE_API_KEY, 2500)
|
71
|
+
@@api_list << GeocoderApi.new("Yahoo", Configuration::YAHOO_API_KEY, 50000)
|
72
|
+
#@@api_list.each{|api| puts "#{api.count}:#{api.symbol}"}
|
73
|
+
|
74
|
+
def get_counts
|
75
|
+
@@api_list.each.inject({}){|h,api| h.update(api.symbol => api.count)}
|
76
|
+
end
|
77
|
+
|
78
|
+
def locate(place_name)
|
79
|
+
latitude = 0.0
|
80
|
+
longitude = 0.0
|
81
|
+
api_name = nil
|
82
|
+
|
83
|
+
@@api_list.each do |api|
|
84
|
+
if api.counter.increment
|
85
|
+
latitude, longitude = api.locate(place_name)
|
86
|
+
if not (latitude == 0.0 and longitude == 0.0)
|
87
|
+
api_name = api.symbol
|
88
|
+
end
|
89
|
+
end
|
90
|
+
break if not (latitude == 0.0 and longitude == 0.0)
|
91
|
+
end
|
92
|
+
|
93
|
+
return latitude, longitude, api_name
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
if __FILE__ == $0
|
100
|
+
|
101
|
+
require "version"
|
102
|
+
|
103
|
+
puts "GeocoderSimplified v#{GeocoderSimplified::VERSION} test:\n\n"
|
104
|
+
|
105
|
+
[
|
106
|
+
"50 W Liberty, Reno",
|
107
|
+
"New York Metro, NY",
|
108
|
+
"San Francisco, CA",
|
109
|
+
"Reno, NV, USA",
|
110
|
+
"Peoria",
|
111
|
+
"Madrid, Spain",
|
112
|
+
"London, England"
|
113
|
+
].each do |place_name|
|
114
|
+
latitude, longitude, api_name = GeocoderSimplified.locate(place_name)
|
115
|
+
puts "% 35s: (%0.8f, %0.8f) via '%s'" % [place_name, latitude, longitude, api_name]
|
116
|
+
end
|
117
|
+
|
118
|
+
puts
|
119
|
+
p GeocoderSimplified.get_counts
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class String
|
2
|
+
def underscore
|
3
|
+
self.gsub(/::/, '/').
|
4
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
5
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
6
|
+
tr("-", "_").
|
7
|
+
downcase
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Util
|
12
|
+
require 'stringio'
|
13
|
+
|
14
|
+
MAX_INT32 = 0x7fffffff
|
15
|
+
SECONDS_PER_DAY = 4 * 60 * 60
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def dump_database(db)
|
19
|
+
puts "-" * 76
|
20
|
+
puts "database dump:"
|
21
|
+
keys = db.keys
|
22
|
+
if keys.size > 0
|
23
|
+
values = db.mget(*keys)
|
24
|
+
list = Hash[keys.zip(values)]
|
25
|
+
list.each{|k, v| puts "%s = %s" % [k, v]}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
def float_with_commas(n, decimal_places)
|
29
|
+
whole = Integer(n)
|
30
|
+
fraction = n - whole
|
31
|
+
decimal_format = "%%.%df" % decimal_places
|
32
|
+
whole.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse << (decimal_format % fraction)[1..-1]
|
33
|
+
end
|
34
|
+
def get_16bit_hash_id(s)
|
35
|
+
"%04x" % s.bytes.each_with_index.map{|c,i| c << ((i & 1) * 8)}.inject(0xFFFF){|hash, n| hash ^ n}
|
36
|
+
end
|
37
|
+
def capture_stderr
|
38
|
+
previous_stderr, $stderr = $stderr, StringIO.new
|
39
|
+
yield
|
40
|
+
$stderr.string
|
41
|
+
ensure
|
42
|
+
$stderr = previous_stderr
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class GeocoderSimplified
|
2
|
+
# Use semantic versioning: http://semver.org/
|
3
|
+
|
4
|
+
# NON_FUNCTIONAL 0.0.0.x level changes for changes to documentation and formatting or functionally equivalent sturctural changes
|
5
|
+
# PATCH 0.0.x level changes for implementation level detail changes, such as small bug fixes
|
6
|
+
# MINOR 0.x.0 level changes for any backwards compatible API changes, such as new functionality/features
|
7
|
+
# MAJOR x.0.0 level changes for backwards incompatible
|
8
|
+
|
9
|
+
VERSION = "1.0.0.1"
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geocoder-simplified
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean Vikoren
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-08 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: geocoder
|
16
|
+
requirement: &2173625120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2173625120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: redis-expiring_counter
|
27
|
+
requirement: &2173616720 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2173616720
|
36
|
+
description: The geocoder-simplified gem is intended to offer a no-frills wrapper
|
37
|
+
on the geocoder gem.
|
38
|
+
email:
|
39
|
+
- sean@vikoren.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- lib/geocoder-simplified/base.rb
|
45
|
+
- lib/geocoder-simplified/util.rb
|
46
|
+
- lib/geocoder-simplified/version.rb
|
47
|
+
- lib/geocoder-simplified.rb
|
48
|
+
homepage: https://github.com/seanvikoren/geocoder-simplified
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project: geocoder-simplified
|
68
|
+
rubygems_version: 1.8.11
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: geocoder-simplified will get lat and long for an address or place name.
|
72
|
+
test_files: []
|