Module: GGeocode
Defined Under Namespace
Modules: Response
Constant Summary collapse
- Version =
'0.0.3'
- AddressPattern =
/\w/iox
- Url =
URI.parse("http://maps.google.com/maps/api/geocode/json?")
Class Method Summary collapse
Instance Method Summary collapse
- #address_for(string) ⇒ Object
- #geocode(*args, &block) ⇒ Object
- #get(url) ⇒ Object
- #latlng_for(string) ⇒ Object
- #query_for(options = {}) ⇒ Object
- #result_for(response) ⇒ Object
- #reverse_geocode(*args, &block) ⇒ Object (also: #rgeocode)
- #url_for(options = {}) ⇒ Object
Class Method Details
.call(*args, &block) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/ggeocode.rb', line 48 def GGeocode.call(*args, &block) = Map.(args) string = args.join(' ') reverse = string !~ AddressPattern || [:reverse] reverse ? GGeocode.reverse_geocode(string) : GGeocode.geocode(string) end |
Instance Method Details
#address_for(string) ⇒ Object
60 61 62 |
# File 'lib/ggeocode.rb', line 60 def address_for(string) string.to_s.strip end |
#geocode(*args, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ggeocode.rb', line 27 def geocode(*args, &block) = Map.(args) if [:reverse] args.push() return reverse_geocode(*args, &block) end string = args.join(' ') address = address_for(string) response = get(url_for(:address => address)) result_for(response) end |
#get(url) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/ggeocode.rb', line 111 def get(url) url = URI.parse(url.to_s) unless url.is_a?(URI) begin Net::HTTP.get_response(url) rescue SocketError, TimeoutError return nil end end |
#latlng_for(string) ⇒ Object
55 56 57 58 |
# File 'lib/ggeocode.rb', line 55 def latlng_for(string) lat, lng = string.scan(/[^\s,]+/) latlng = [lat, lng].join(',') end |
#query_for(options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ggeocode.rb', line 93 def query_for( = {}) pairs = [] .each do |key, values| key = key.to_s values = [values].flatten values.each do |value| value = value.to_s if value.empty? pairs << [ CGI.escape(key) ] else pairs << [ CGI.escape(key), CGI.escape(value) ].join('=') end end end pairs.replace pairs.sort_by{|pair| pair.size} pairs.join('&') end |
#result_for(response) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ggeocode.rb', line 64 def result_for(response) return nil unless response hash = JSON.parse(response.body) return nil unless hash['status']=='OK' map = Map.new map.extend(Response) map.response = response map['status'] = hash['status'] map['results'] = hash['results'] map end |
#reverse_geocode(*args, &block) ⇒ Object Also known as: rgeocode
39 40 41 42 43 44 45 |
# File 'lib/ggeocode.rb', line 39 def reverse_geocode(*args, &block) = Map.(args) string = args.join(' ') latlng = latlng_for(string) response = get(url_for(:latlng => latlng)) result_for(response) end |
#url_for(options = {}) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/ggeocode.rb', line 86 def url_for( = {}) [:sensor] = false unless .has_key?(:sensor) url = Url.dup url.query = query_for() url end |