geocoder_plus 0.3.0 → 0.4.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.
@@ -1,58 +1,44 @@
1
1
  module Geokit
2
2
  module Geocoders
3
3
 
4
- @@cache = nil
5
- @@hydra = nil
6
- @@typhoeus_timeout = 30000
7
- @@typhoeus_cache_timeout = 604800
4
+ @@api_cache = nil
5
+ @@api_cache_valid = 86400
6
+ @@api_cache_timeout = 5
7
+
8
8
  __define_accessors
9
9
 
10
10
  # The Geocoder base class which defines the interface to be used by all
11
11
  # other geocoders.
12
12
  class Geocoder
13
13
 
14
- private
15
-
16
- def self.cache_query
17
- @_cache ||= initialize_cache
14
+ def self.normalize_address(addr)
15
+ addr.gsub(/\s*,\s*/, ',').strip
18
16
  end
19
17
 
20
- # Set hydra and memcache if defined in configuration file
21
- def self.initialize_cache
22
- if (Geokit::Geocoders::cache && Geokit::Geocoders::hydra && self.respond_to?(:success_response?))
23
- @cache = Geokit::Geocoders::cache
24
- Geokit::Geocoders::hydra.cache_setter do |request|
25
- @cache.set(request.cache_key, request.response, request.cache_timeout)
26
- end
27
-
28
- Geokit::Geocoders::hydra.cache_getter do |request|
29
- @cache.get(request.cache_key) rescue nil
30
- end
31
- return true
32
- end
33
- end
34
-
18
+ private
19
+
35
20
  # Wraps the geocoder call around a proxy if necessary.
36
21
  # Use typhoeus and memcache if defined
37
- def self.do_get(url)
38
- uri = URI.parse(url)
39
- if (cache_query)
40
- req = Typhoeus::Request.new(url,
41
- :timeout => Geokit::Geocoders::typhoeus_timeout, # milliseconds
42
- :cache_timeout => Geokit::Geocoders::typhoeus_cache_timeout)
43
- # :headers => {"Authorization" => "Basic #{Base64.b64encode("#{uri.user}:#{uri.password}")}"})
44
- Geokit::Geocoders::hydra.queue(req)
45
- Geokit::Geocoders::hydra.run
46
- return req.response
47
- else
22
+ def self.do_get(url)
23
+ if (defined?(APICache) && Geokit::Geocoders::api_cache)
24
+ body = APICache.get(url, {
25
+ :cache => Geokit::Geocoders::api_cache,
26
+ :valid => Geokit::Geocoders::api_cache_valid,
27
+ :timeout => Geokit::Geocoders::api_cache_timeout})
28
+ # fake Net::HTTPOK so that caching will work for all geocoders
29
+ res = Net::HTTPOK.new(1.0, 200, 'OK')
30
+ res.instance_variable_set(:@body, body)
31
+ res.instance_variable_set(:@read, true)
32
+ else
33
+ uri = URI.parse(url)
48
34
  req = Net::HTTP::Get.new(url)
49
35
  req.basic_auth(uri.user, uri.password) if uri.userinfo
50
36
  res = Net::HTTP::Proxy(GeoKit::Geocoders::proxy_addr,
51
37
  GeoKit::Geocoders::proxy_port,
52
38
  GeoKit::Geocoders::proxy_user,
53
- GeoKit::Geocoders::proxy_pass).start(uri.host, uri.port) { |http| http.get(uri.path + "?" + uri.query) }
54
- return res
39
+ GeoKit::Geocoders::proxy_pass).start(uri.host, uri.port) { |http| http.get(uri.path + "?" + uri.query) }
55
40
  end
41
+ return res
56
42
  end
57
43
  end
58
44
 
@@ -15,14 +15,10 @@ require 'geocoders/google_v3_geocoder'
15
15
 
16
16
  module GeocoderPlus
17
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)
18
+ # by default, try requiring api_cache -- if that works, use it
23
19
  begin
24
- require 'typhoeus'
20
+ require 'api_cache'
25
21
  rescue LoadError
26
- puts "Typhoeus is not installed, using Net:HTTP. Install typhoeus to enable query caching."
22
+ puts "APICache is not installed. Install it to enable query caching, gem install 'api_cache'"
27
23
  end
28
24
  end
@@ -7,11 +7,7 @@ module Geokit
7
7
  private
8
8
 
9
9
  def self.success_response?(res)
10
- if (defined?(Typhoeus) && res.is_a?(Typhoeus::Response))
11
- return res.success?
12
- else
13
- return res.is_a?(Net::HTTPSuccess)
14
- end
10
+ res.is_a?(Net::HTTPSuccess) || res.is_a?(String)
15
11
  end
16
12
 
17
13
  def self.get_url(address, options={})
@@ -21,24 +17,33 @@ module Geokit
21
17
  end
22
18
 
23
19
  def self.do_geocode(address, options = {})
24
- geo_url = self.get_url(address, options)
20
+ # debugger
21
+ geo_url = self.get_url(normalize_address(address), options)
25
22
  logger.debug "Making Geocode request to: #{geo_url}"
26
- res = self.call_geocoder_service(geo_url)
27
- logger.debug "Google V3 geocoding. Address: #{address}. Result: #{res}"
28
-
29
- if success_response?(res)
30
- return self.parse_body(Yajl::Parser.parse(res.body))
31
- else
32
- return GeoLoc.new
33
- end
23
+ res = self.call_geocoder_service(geo_url)
24
+ return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
25
+ logger.debug "Google V3 geocoding. Address: #{address}. Result: #{res}"
26
+ # if success_response?(res)
27
+ # return self.parse_body(res)
28
+ # else
29
+ # return GeoLoc.new
30
+ # end
31
+ return self.parse_body(res)
34
32
  rescue Exception => e
35
33
  logger.info "Caught an error during Google V3 geocoding call: #{e.inspect}"
36
34
  return GeoLoc.new
37
35
  end
38
36
 
39
- def self.parse_body(body)
37
+ def self.parse_body(res)
38
+ # debugger
39
+ # if res.is_a?(String)
40
+ # body = Yajl::Parser.parse(res)
41
+ # else
42
+ body = Yajl::Parser.parse(res.body)
43
+ # end
44
+
40
45
  if body['status'] != "OK"
41
- if body['status' ] == 'OVER_QUERY_LIMIT'
46
+ if body['status'] == 'OVER_QUERY_LIMIT'
42
47
  raise Geokit::TooManyQueriesError "Google returned OVER_QUERY_LIMIT: #{body}"
43
48
  elsif body['status'] == 'ZERO_RESULTS'
44
49
  logger.info "Found no results from google v3"
@@ -5,11 +5,7 @@ module Geokit
5
5
  private
6
6
 
7
7
  def self.success_response?(res)
8
- if (defined?(Typhoeus) && res.is_a?(Typhoeus::Response))
9
- return res.success?
10
- else
11
- return res.is_a?(Net::HTTPSuccess)
12
- end
8
+ res.is_a?(Net::HTTPSuccess) || res.is_a?(String)
13
9
  end
14
10
 
15
11
  def self.get_url(address, options={})
@@ -28,7 +24,7 @@ module Geokit
28
24
  logger.debug "Yahoo PlaceFinder reverse-geocoding. LL: #{latlng}. Result: #{res}"
29
25
 
30
26
  if success_response?(res)
31
- return self.parse_body(Yajl::Parser.parse(res.body))
27
+ return self.parse_body(Yajl::Parser.parse(res))
32
28
  else
33
29
  return GeoLoc.new
34
30
  end
@@ -36,20 +32,28 @@ module Geokit
36
32
 
37
33
  def self.do_geocode(address, options={})
38
34
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
39
- res = self.call_geocoder_service(self.get_url(address, options))
35
+ res = self.call_geocoder_service(self.get_url(normalize_address(address), options))
36
+ return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
40
37
  logger.debug "Yahoo PlaceFinder geocoding. Address: #{address}. Result: #{res}"
41
38
 
42
- if success_response?(res)
43
- return self.parse_body(Yajl::Parser.parse(res.body))
44
- else
45
- return GeoLoc.new
46
- end
39
+ # if success_response?(res)
40
+ # return self.parse_body(res)
41
+ # else
42
+ # return GeoLoc.new
43
+ # end
44
+ return self.parse_body(res)
47
45
  rescue
48
46
  logger.info "Caught an error during Yahoo PlaceFinder geocoding call: "+$!
49
47
  return GeoLoc.new
50
48
  end
51
49
 
52
- def self.parse_body(body)
50
+ def self.parse_body(res)
51
+ # if res.is_a?(String)
52
+ # body = Yajl::Parser.parse(res)
53
+ # else
54
+ body = Yajl::Parser.parse(res.body)
55
+ # end
56
+
53
57
  count = body['query']['count']
54
58
  if (count == 1)
55
59
  return extract_place(body['query']['results']['Result'])
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: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.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-13 00:00:00 +07:00
18
+ date: 2011-04-24 00:00:00 +07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -124,7 +124,39 @@ dependencies:
124
124
  name: rcov
125
125
  prerelease: false
126
126
  type: :development
127
- description: This gem consists two additional geocoders for geokit, Yahoo! PlaceFinder and Google V3
127
+ - !ruby/object:Gem::Dependency
128
+ requirement: &id008 !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ hash: 23
134
+ segments:
135
+ - 0
136
+ - 2
137
+ - 0
138
+ version: 0.2.0
139
+ version_requirements: *id008
140
+ name: api_cache
141
+ prerelease: false
142
+ type: :development
143
+ - !ruby/object:Gem::Dependency
144
+ requirement: &id009 !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ hash: 7
150
+ segments:
151
+ - 0
152
+ - 6
153
+ - 0
154
+ version: 0.6.0
155
+ version_requirements: *id009
156
+ name: moneta
157
+ prerelease: false
158
+ type: :development
159
+ description: This gem consists two additional geocoders for geokit, Yahoo! PlaceFinder and Google V3. And add support for caching using api_caching gem.
128
160
  email: fajar.ab@gmail.com
129
161
  executables: []
130
162