geocoder_plus 0.1.1 → 0.2.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.
- data/README +0 -1
- data/README.rdoc +9 -3
- data/lib/geocoder.rb +55 -0
- data/lib/geocoder_plus.rb +24 -1
- data/lib/geocoders/google_v3_geocoder.rb +1 -1
- data/lib/geocoders/yahoo_place_finder_geocoder.rb +1 -1
- data/test/test_yahoo_place_finder_geocoder.rb +1 -0
- metadata +6 -5
data/README
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
= geocoder_plus
|
2
2
|
|
3
|
-
|
3
|
+
This gem has two additional geocoders for geokit gem, Yahoo! PlaceFinder and Google V3.
|
4
|
+
Google V3 code is taken from https://github.com/tello/google-v3-geocoder.
|
5
|
+
|
6
|
+
Example:
|
7
|
+
|
8
|
+
Geokit::Geocoders::YahooPlaceFinderGeocoder.geocode('701 First Avenue Sunnyvale, California 94089')
|
9
|
+
Geokit::Geocoders::GoogleV3Geocoder.geocode('701 First Avenue Sunnyvale, California 94089')
|
10
|
+
|
4
11
|
|
5
12
|
== Contributing to geocoder_plus
|
6
13
|
|
@@ -15,5 +22,4 @@ Description goes here.
|
|
15
22
|
== Copyright
|
16
23
|
|
17
24
|
Copyright (c) 2011 Fajar A B. See LICENSE.txt for
|
18
|
-
further details.
|
19
|
-
|
25
|
+
further details.
|
data/lib/geocoder.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Geokit
|
2
|
+
module Geocoders
|
3
|
+
|
4
|
+
@@cache = nil
|
5
|
+
@@hydra = nil
|
6
|
+
__define_accessors
|
7
|
+
|
8
|
+
# The Geocoder base class which defines the interface to be used by all
|
9
|
+
# other geocoders.
|
10
|
+
class Geocoder
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def self.cache_query
|
15
|
+
@_cache ||= initialize_cache
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set hydra and memcache if defined in configuration file
|
19
|
+
def self.initialize_cache
|
20
|
+
if (Geokit::Geocoders::cache && Geokit::Geocoders::hydra)
|
21
|
+
@cache = Geokit::Geocoders::cache
|
22
|
+
Geokit::Geocoders::hydra.cache_setter do |request|
|
23
|
+
@cache.set(request.cache_key, request.response, request.cache_timeout)
|
24
|
+
end
|
25
|
+
|
26
|
+
Geokit::Geocoders::hydra.cache_getter do |request|
|
27
|
+
@cache.get(request.cache_key) rescue nil
|
28
|
+
end
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Wraps the geocoder call around a proxy if necessary.
|
34
|
+
# Use typhoeus and memcache if defined
|
35
|
+
def self.do_get(url)
|
36
|
+
uri = URI.parse(url)
|
37
|
+
if (cache_query)
|
38
|
+
req = Typhoeus::Request.new(url, :cache_timeout => 604800) # cache response for 1 week
|
39
|
+
Geokit::Geocoders::hydra.queue(req)
|
40
|
+
Geokit::Geocoders::hydra.run
|
41
|
+
return req.response
|
42
|
+
else
|
43
|
+
req = Net::HTTP::Get.new(url)
|
44
|
+
req.basic_auth(uri.user, uri.password) if uri.userinfo
|
45
|
+
res = Net::HTTP::Proxy(GeoKit::Geocoders::proxy_addr,
|
46
|
+
GeoKit::Geocoders::proxy_port,
|
47
|
+
GeoKit::Geocoders::proxy_user,
|
48
|
+
GeoKit::Geocoders::proxy_pass).start(uri.host, uri.port) { |http| http.get(uri.path + "?" + uri.query) }
|
49
|
+
return res
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
data/lib/geocoder_plus.rb
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
require 'geokit'
|
2
2
|
require 'geokit/geocoders.rb'
|
3
|
+
require 'geocoder'
|
3
4
|
require 'yajl'
|
4
5
|
require 'geocoders/yahoo_place_finder_geocoder'
|
5
|
-
require 'geocoders/google_v3_geocoder'
|
6
|
+
require 'geocoders/google_v3_geocoder'
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
# finally, set up the http service Koala methods used to make requests
|
11
|
+
# you can use your own (for HTTParty, etc.) by calling Koala.http_service = YourModule
|
12
|
+
# def self.http_service=(service)
|
13
|
+
# self.send(:include, service)
|
14
|
+
# end
|
15
|
+
|
16
|
+
module GeocoderPlus
|
17
|
+
|
18
|
+
@use_typhoeus = true
|
19
|
+
|
20
|
+
# by default, try requiring Typhoeus -- if that works, use it
|
21
|
+
# if you have Typheous and don't want to use it (or want another service),
|
22
|
+
# you can run Koala.http_service = NetHTTPService (or MyHTTPService)
|
23
|
+
begin
|
24
|
+
require 'typhoeus'
|
25
|
+
rescue LoadError
|
26
|
+
puts "Typhoeus is not installed, using Net:HTTP. Install typhoeus to enable query caching."
|
27
|
+
end
|
28
|
+
end
|
@@ -10,7 +10,7 @@ module Geokit
|
|
10
10
|
logger.debug "Making Geocode request to: #{geo_url}"
|
11
11
|
res = self.call_geocoder_service(geo_url)
|
12
12
|
|
13
|
-
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
13
|
+
# return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
14
14
|
json = res.body
|
15
15
|
logger.debug "Google V3 geocoding. Address: #{address}. Result: #{json}"
|
16
16
|
return self.convert_json_to_geoloc(json)
|
@@ -23,7 +23,7 @@ module Geokit
|
|
23
23
|
def self.do_geocode(address, options={})
|
24
24
|
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
|
25
25
|
res = self.call_geocoder_service(self.get_url(address, options))
|
26
|
-
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
26
|
+
# return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
27
27
|
logger.debug "Yahoo PlaceFinder geocoding. Address: #{address}. Result: #{res}"
|
28
28
|
|
29
29
|
return self.parse_body(Yajl::Parser.parse(res.body))
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geocoder_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Fajar A B
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-09 00:00:00 +07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -135,6 +135,7 @@ extra_rdoc_files:
|
|
135
135
|
- README
|
136
136
|
- README.rdoc
|
137
137
|
files:
|
138
|
+
- lib/geocoder.rb
|
138
139
|
- lib/geocoder_plus.rb
|
139
140
|
- lib/geocoders/google_v3_geocoder.rb
|
140
141
|
- lib/geocoders/yahoo_place_finder_geocoder.rb
|