geocoder_plus 0.2.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,8 @@ module Geokit
3
3
 
4
4
  @@cache = nil
5
5
  @@hydra = nil
6
+ @@typhoeus_timeout = 30000
7
+ @@typhoeus_cache_timeout = 604800
6
8
  __define_accessors
7
9
 
8
10
  # The Geocoder base class which defines the interface to be used by all
@@ -16,9 +18,9 @@ module Geokit
16
18
  end
17
19
 
18
20
  # 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
21
+ def self.initialize_cache
22
+ if (Geokit::Geocoders::cache && Geokit::Geocoders::hydra && self.respond_to?(:success_response?))
23
+ @cache = Geokit::Geocoders::cache
22
24
  Geokit::Geocoders::hydra.cache_setter do |request|
23
25
  @cache.set(request.cache_key, request.response, request.cache_timeout)
24
26
  end
@@ -28,18 +30,21 @@ module Geokit
28
30
  end
29
31
  return true
30
32
  end
31
- end
33
+ end
32
34
 
33
35
  # Wraps the geocoder call around a proxy if necessary.
34
36
  # Use typhoeus and memcache if defined
35
37
  def self.do_get(url)
36
38
  uri = URI.parse(url)
37
39
  if (cache_query)
38
- req = Typhoeus::Request.new(url, :cache_timeout => 604800) # cache response for 1 week
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}")}"})
39
44
  Geokit::Geocoders::hydra.queue(req)
40
45
  Geokit::Geocoders::hydra.run
41
46
  return req.response
42
- else
47
+ else
43
48
  req = Net::HTTP::Get.new(url)
44
49
  req.basic_auth(uri.user, uri.password) if uri.userinfo
45
50
  res = Net::HTTP::Proxy(GeoKit::Geocoders::proxy_addr,
@@ -3,47 +3,54 @@
3
3
  module Geokit
4
4
  module Geocoders
5
5
  class GoogleV3Geocoder < Geocoder
6
- private
7
-
8
- def self.do_geocode(address, options = {})
9
- geo_url = self.get_url(address, options)
10
- logger.debug "Making Geocode request to: #{geo_url}"
11
- res = self.call_geocoder_service(geo_url)
12
6
 
13
- # return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
14
- json = res.body
15
- logger.debug "Google V3 geocoding. Address: #{address}. Result: #{json}"
16
- return self.convert_json_to_geoloc(json)
7
+ private
8
+
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
17
15
  end
18
-
16
+
19
17
  def self.get_url(address, options={})
20
18
  bias = options[:bias] || ''
21
19
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
22
20
  return "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(address_str)}&region=#{bias.to_s.downcase}"
23
21
  end
24
22
 
25
- def self.convert_json_to_geoloc(json)
26
- begin
27
- data = Yajl::Parser.parse(json)
28
- rescue
29
- logger.error "Could not parse JSON from Google: #{json}"
23
+ def self.do_geocode(address, options = {})
24
+ geo_url = self.get_url(address, options)
25
+ 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
30
32
  return GeoLoc.new
31
33
  end
32
-
33
- if data['status'] != "OK"
34
- if data['status' ] == 'OVER_QUERY_LIMIT'
35
- raise Geokit::TooManyQueriesError "Google returned OVER_QUERY_LIMIT: #{json}"
36
- elsif data['status'] == 'ZERO_RESULTS'
34
+ rescue Exception => e
35
+ logger.info "Caught an error during Google V3 geocoding call: #{e.inspect}"
36
+ return GeoLoc.new
37
+ end
38
+
39
+ def self.parse_body(body)
40
+ if body['status'] != "OK"
41
+ if body['status' ] == 'OVER_QUERY_LIMIT'
42
+ raise Geokit::TooManyQueriesError "Google returned OVER_QUERY_LIMIT: #{body}"
43
+ elsif body['status'] == 'ZERO_RESULTS'
37
44
  logger.info "Found no results from google v3"
38
45
  return GeoLoc.new
39
46
  end
40
- logger.error "Got an error from google, response: #{json}"
47
+ logger.error "Got an error from google, response: #{body}"
41
48
  # Otherwise, we don't know what to do
42
49
  return GeoLoc.new
43
50
  end
44
51
 
45
52
  begin
46
- results = data['results']
53
+ results = body['results']
47
54
  geoloc = nil
48
55
  results.each do |result|
49
56
  extracted_geoloc = self.extract_location(result)
@@ -1,8 +1,17 @@
1
1
  module Geokit
2
2
  module Geocoders
3
3
  class YahooPlaceFinderGeocoder < Geocoder
4
+
4
5
  private
5
6
 
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
13
+ end
14
+
6
15
  def self.get_url(address, options={})
7
16
  if (options[:reverse])
8
17
  latlng=LatLng.normalize(address)
@@ -17,16 +26,24 @@ module Geokit
17
26
  def self.do_reverse_geocode(latlng)
18
27
  res = self.call_geocoder_service(self.get_url(latlng, :reverse => true))
19
28
  logger.debug "Yahoo PlaceFinder reverse-geocoding. LL: #{latlng}. Result: #{res}"
20
- return self.parse_body(Yajl::Parser.parse(res.body))
29
+
30
+ if success_response?(res)
31
+ return self.parse_body(Yajl::Parser.parse(res.body))
32
+ else
33
+ return GeoLoc.new
34
+ end
21
35
  end
22
36
 
23
37
  def self.do_geocode(address, options={})
24
38
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
25
39
  res = self.call_geocoder_service(self.get_url(address, options))
26
- # return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
27
40
  logger.debug "Yahoo PlaceFinder geocoding. Address: #{address}. Result: #{res}"
28
41
 
29
- return self.parse_body(Yajl::Parser.parse(res.body))
42
+ if success_response?(res)
43
+ return self.parse_body(Yajl::Parser.parse(res.body))
44
+ else
45
+ return GeoLoc.new
46
+ end
30
47
  rescue
31
48
  logger.info "Caught an error during Yahoo PlaceFinder geocoding call: "+$!
32
49
  return GeoLoc.new
@@ -63,7 +80,7 @@ module Geokit
63
80
  geoloc.lat = result['latitude']
64
81
  geoloc.lng = result['longitude']
65
82
  geoloc.country_code = result['countrycode']
66
- geoloc.provider = 'Yahoo! PlaceFinder'
83
+ geoloc.provider = 'yahoo_place_finder'
67
84
 
68
85
  # extended -- false if not not available
69
86
  geoloc.street_address = result['line1']
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geocoder_plus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- version: 0.2.0
5
+ version: 0.2.3
11
6
  platform: ruby
12
7
  authors:
13
8
  - Fajar A B
@@ -15,115 +10,86 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-04-09 00:00:00 +07:00
13
+ date: 2011-04-13 00:00:00 +07:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
17
+ name: geokit
22
18
  requirement: &id001 !ruby/object:Gem::Requirement
23
19
  none: false
24
20
  requirements:
25
21
  - - ">="
26
22
  - !ruby/object:Gem::Version
27
- hash: 3
28
- segments:
29
- - 1
30
- - 5
31
- - 0
32
23
  version: 1.5.0
33
- version_requirements: *id001
34
- name: geokit
35
- prerelease: false
36
24
  type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
37
27
  - !ruby/object:Gem::Dependency
28
+ name: yajl-ruby
38
29
  requirement: &id002 !ruby/object:Gem::Requirement
39
30
  none: false
40
31
  requirements:
41
32
  - - ">="
42
33
  - !ruby/object:Gem::Version
43
- hash: 59
44
- segments:
45
- - 0
46
- - 8
47
- - 2
48
34
  version: 0.8.2
49
- version_requirements: *id002
50
- name: yajl-ruby
51
- prerelease: false
52
35
  type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
53
38
  - !ruby/object:Gem::Dependency
39
+ name: shoulda
54
40
  requirement: &id003 !ruby/object:Gem::Requirement
55
41
  none: false
56
42
  requirements:
57
43
  - - ">="
58
44
  - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
45
  version: "0"
63
- version_requirements: *id003
64
- name: shoulda
65
- prerelease: false
66
46
  type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
67
49
  - !ruby/object:Gem::Dependency
50
+ name: mocha
68
51
  requirement: &id004 !ruby/object:Gem::Requirement
69
52
  none: false
70
53
  requirements:
71
54
  - - ">="
72
55
  - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
56
  version: "0"
77
- version_requirements: *id004
78
- name: mocha
79
- prerelease: false
80
57
  type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
81
60
  - !ruby/object:Gem::Dependency
61
+ name: bundler
82
62
  requirement: &id005 !ruby/object:Gem::Requirement
83
63
  none: false
84
64
  requirements:
85
65
  - - ~>
86
66
  - !ruby/object:Gem::Version
87
- hash: 23
88
- segments:
89
- - 1
90
- - 0
91
- - 0
92
67
  version: 1.0.0
93
- version_requirements: *id005
94
- name: bundler
95
- prerelease: false
96
68
  type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
97
71
  - !ruby/object:Gem::Dependency
72
+ name: jeweler
98
73
  requirement: &id006 !ruby/object:Gem::Requirement
99
74
  none: false
100
75
  requirements:
101
76
  - - ~>
102
77
  - !ruby/object:Gem::Version
103
- hash: 7
104
- segments:
105
- - 1
106
- - 5
107
- - 2
108
78
  version: 1.5.2
109
- version_requirements: *id006
110
- name: jeweler
111
- prerelease: false
112
79
  type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
113
82
  - !ruby/object:Gem::Dependency
83
+ name: rcov
114
84
  requirement: &id007 !ruby/object:Gem::Requirement
115
85
  none: false
116
86
  requirements:
117
87
  - - ">="
118
88
  - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
121
- - 0
122
89
  version: "0"
123
- version_requirements: *id007
124
- name: rcov
125
- prerelease: false
126
90
  type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
127
93
  description: This gem consists two additional geocoders for geokit, Yahoo! PlaceFinder and Google V3
128
94
  email: fajar.ab@gmail.com
129
95
  executables: []
@@ -160,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
126
  requirements:
161
127
  - - ">="
162
128
  - !ruby/object:Gem::Version
163
- hash: 3
129
+ hash: -4398613690650561579
164
130
  segments:
165
131
  - 0
166
132
  version: "0"
@@ -169,9 +135,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
135
  requirements:
170
136
  - - ">="
171
137
  - !ruby/object:Gem::Version
172
- hash: 3
173
- segments:
174
- - 0
175
138
  version: "0"
176
139
  requirements: []
177
140