gmaps4rails 1.5.2 → 1.5.3
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/.gitignore +1 -0
- data/Gemfile.lock +7 -1
- data/README.rdoc +2 -2
- data/app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee +37 -96
- data/app/assets/javascripts/gmaps4rails/gmaps4rails.bing.js.coffee +11 -0
- data/app/assets/javascripts/gmaps4rails/gmaps4rails.googlemaps.js.coffee +85 -9
- data/app/assets/javascripts/gmaps4rails/gmaps4rails.mapquest.js.coffee +13 -2
- data/app/assets/javascripts/gmaps4rails/gmaps4rails.openlayers.js.coffee +54 -2
- data/app/views/gmaps4rails/_gmaps4rails.html.erb +1 -1
- data/gmaps4rails.gemspec +1 -0
- data/lib/generators/gmaps4rails/install_generator.rb +28 -12
- data/lib/gmaps4rails/acts_as_gmappable.rb +3 -46
- data/lib/gmaps4rails/api_wrappers/base_net_methods.rb +40 -0
- data/lib/gmaps4rails/api_wrappers/direction.rb +87 -0
- data/lib/gmaps4rails/api_wrappers/geocoder.rb +54 -0
- data/lib/gmaps4rails/api_wrappers/places.rb +74 -0
- data/lib/gmaps4rails/base.rb +80 -22
- data/lib/gmaps4rails/extensions/{array.rb → enumerable.rb} +1 -1
- data/lib/gmaps4rails/js_builder.rb +9 -17
- data/lib/gmaps4rails/json_builder.rb +1 -7
- data/lib/gmaps4rails/model_handler.rb +79 -0
- data/lib/gmaps4rails/version.rb +1 -1
- data/lib/gmaps4rails/view_helper.rb +4 -6
- data/public/javascripts/gmaps4rails/gmaps4rails.base.js +35 -112
- data/public/javascripts/gmaps4rails/gmaps4rails.bing.js +12 -0
- data/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js +123 -7
- data/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js +12 -0
- data/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js +53 -2
- data/spec/dummy/app/views/users/index.html.erb +88 -85
- data/spec/fixtures/google_direction_valid.json +65 -0
- data/spec/fixtures/google_geocoding_toulon_france.json +58 -0
- data/spec/fixtures/google_places_valid.json +45 -0
- data/spec/fixtures/google_wrong_geocoding.json +4 -0
- data/spec/lib/base_spec.rb +59 -0
- data/spec/lib/direction_spec.rb +53 -0
- data/spec/lib/geocoder_spec.rb +46 -0
- data/spec/{base/base_spec.rb → lib/js_builder_spec.rb} +0 -6
- data/spec/lib/json_builder_spec.rb +232 -0
- data/spec/lib/places_spec.rb +25 -0
- data/spec/models/user_spec.rb +75 -357
- data/spec/spec_helper.rb +1 -0
- data/spec/support/geocoding.rb +27 -1
- metadata +47 -12
- data/lib/gmaps4rails/geocoding.rb +0 -113
- data/lib/gmaps4rails/google_places.rb +0 -76
- data/spec/base/geocoding_spec.rb +0 -17
@@ -1,113 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'uri'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module Gmaps4rails
|
6
|
-
|
7
|
-
# This method geocodes an address using the GoogleMaps webservice
|
8
|
-
# options are:
|
9
|
-
# * address: string, mandatory
|
10
|
-
# * lang: to set the language one wants the result to be translated (default is english)
|
11
|
-
# * raw: to get the raw response from google, default is false
|
12
|
-
def Gmaps4rails.geocode(address, lang="en", raw = false, protocol = "http")
|
13
|
-
if address.nil? || address.empty?
|
14
|
-
raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address"
|
15
|
-
else #coordinates are valid
|
16
|
-
geocoder = "#{protocol}://maps.googleapis.com/maps/api/geocode/json?language=#{lang}&address="
|
17
|
-
output = "&sensor=false"
|
18
|
-
#send request to the google api to get the lat/lng
|
19
|
-
request = geocoder + address + output
|
20
|
-
url = URI.escape(request)
|
21
|
-
Gmaps4rails.handle_geocoding_response(request, Gmaps4rails.get_response(url), raw)
|
22
|
-
end # end address valid
|
23
|
-
end
|
24
|
-
|
25
|
-
# This method retrieves destination results provided by GoogleMaps webservice
|
26
|
-
# options are:
|
27
|
-
# * start_end: Hash { "from" => string, "to" => string}, mandatory
|
28
|
-
# * options: details given in the github's wiki
|
29
|
-
# * output: could be "pretty", "raw" or "clean"; filters the output from google
|
30
|
-
#output could be raw, pretty or clean
|
31
|
-
def Gmaps4rails.destination(start_end, options={}, output="pretty")
|
32
|
-
if start_end["from"].nil? || start_end["to"].empty?
|
33
|
-
raise Gmaps4rails::DirectionInvalidQuery, "Origin and destination must be provided in a hash as first argument"
|
34
|
-
else #great, we have stuff to work with
|
35
|
-
geocoder = "http://maps.googleapis.com/maps/api/directions/json?origin=#{start_end["from"]}&destination=#{start_end["to"]}"
|
36
|
-
#if value is an Array, it means it contains the waypoints, otherwise it's chained normally
|
37
|
-
dest_options = options.empty? ? "" : "&" + options.map {|k,v| v.is_a?(Array) ? k + "=" + v * ("|") : k + "=" + v }*("&")
|
38
|
-
#send request to the google api to get the directions
|
39
|
-
request = geocoder + dest_options + "&sensor=false"
|
40
|
-
url = URI.escape(request)
|
41
|
-
Gmaps4rails.handle_destination_response(request, Gmaps4rails.get_response(url), output)
|
42
|
-
end # end origin + destination exist
|
43
|
-
end #end destination
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def Gmaps4rails.handle_geocoding_response(request, response, raw)
|
48
|
-
#parse result if result received properly
|
49
|
-
if response.is_a?(Net::HTTPSuccess)
|
50
|
-
#parse the json
|
51
|
-
parse = JSON.parse(response.body)
|
52
|
-
#check if google went well
|
53
|
-
if parse["status"] == "OK"
|
54
|
-
return parse if raw == true
|
55
|
-
array = []
|
56
|
-
parse["results"].each do |result|
|
57
|
-
array << {
|
58
|
-
:lat => result["geometry"]["location"]["lat"],
|
59
|
-
:lng => result["geometry"]["location"]["lng"],
|
60
|
-
:matched_address => result["formatted_address"],
|
61
|
-
:bounds => result["geometry"]["bounds"],
|
62
|
-
:full_data => result
|
63
|
-
}
|
64
|
-
end
|
65
|
-
return array
|
66
|
-
else #status != OK
|
67
|
-
raise Gmaps4rails::GeocodeStatus, "The address you passed seems invalid, status was: #{parse["status"]}.
|
68
|
-
Request was: #{request}"
|
69
|
-
end #end parse status
|
70
|
-
|
71
|
-
else #if not http success
|
72
|
-
raise Gmaps4rails::GeocodeNetStatus, "The request sent to google was invalid (not http success): #{request}.
|
73
|
-
Response was: #{response}"
|
74
|
-
end #end resp test
|
75
|
-
end
|
76
|
-
|
77
|
-
def Gmaps4rails.handle_destination_response(request, response, output)
|
78
|
-
if response.is_a?(Net::HTTPSuccess)
|
79
|
-
#parse the json
|
80
|
-
parse = JSON.parse(response.body)
|
81
|
-
#check if google went well
|
82
|
-
if parse["status"] == "OK"
|
83
|
-
legs = []
|
84
|
-
#Each element in the legs array specifies a single leg of the journey from the origin to the destination in the calculated route
|
85
|
-
parse["routes"].first["legs"].each do |leg|
|
86
|
-
#delete coded polyline elements from legs and store it in polylines to make output cleaner
|
87
|
-
polylines = leg["steps"].map {|step| step.delete("polyline")} if output == "pretty" || output == "clean"
|
88
|
-
legs << {
|
89
|
-
"duration" => { "text" => leg["duration"]["text"], "value" => leg["duration"]["value"].to_f },
|
90
|
-
"distance" => { "text" => leg["distance"]["text"], "value" => leg["distance"]["value"].to_f },
|
91
|
-
"steps" => leg["steps"]
|
92
|
-
}
|
93
|
-
if output == "pretty"
|
94
|
-
#polylines contain levels data, which are not that useful.
|
95
|
-
polylines.map{|poly| poly.delete("levels")}
|
96
|
-
#create valid json from all polylines, this could be directly passed to javascript for display
|
97
|
-
json = polylines.map { |poly| {"coded_array" => poly["points"]} }.to_json
|
98
|
-
#merge results in legs
|
99
|
-
legs.last.merge!({ "polylines" => json })
|
100
|
-
end
|
101
|
-
end
|
102
|
-
return legs
|
103
|
-
else #status != OK
|
104
|
-
raise Gmaps4rails::DirectionStatus, "The query you passed seems invalid, status was: #{parse["status"]}.
|
105
|
-
Request was: #{request}"
|
106
|
-
end #end parse status
|
107
|
-
else #if not http success
|
108
|
-
raise Gmaps4rails::DirectionNetStatus, "The request sent to google was invalid (not http success): #{request}.
|
109
|
-
Response was: #{response}"
|
110
|
-
end #end resp test
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
module Gmaps4rails
|
2
|
-
|
3
|
-
# does two things... 1) gecode the given address string and 2) triggers a a places query around that geo location
|
4
|
-
# optionally a keyword can be given for a filter over all places fields (e.g. "Bungy" to give all Bungy related places)
|
5
|
-
# IMPORTANT: Places API calls require an API key (param "key")
|
6
|
-
|
7
|
-
def Gmaps4rails.places_for_address(address, key, keyword = nil, radius = 7500, lang="en", raw = false)
|
8
|
-
if address.nil?
|
9
|
-
raise Gmaps4rails::GeocodeInvalidQuery, "you must provide an address for a places_for_address query"
|
10
|
-
elsif key.nil?
|
11
|
-
raise "Google Places API requires an API key"
|
12
|
-
else
|
13
|
-
res = Gmaps4rails.geocode(address) # will throw exception if nothing could be geocoded
|
14
|
-
Gmaps4rails.places(res.first[:lat], res.first[:lng], key, keyword, radius, lang, raw)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# does a places query around give geo location (lat/lng)
|
19
|
-
# optionally a keyword can be given for a filter over all places fields (e.g. "Bungy" to give all Bungy related places)
|
20
|
-
# IMPORTANT: Places API calls require an API key (param "key")
|
21
|
-
def Gmaps4rails.places(lat, lng, key, keyword = nil, radius = 7500, lang="en", raw = false, protocol = "https")
|
22
|
-
if lat.nil? || lng.nil?
|
23
|
-
raise Gmaps4rails::PlacesInvalidQuery, "You must provide at least a lat/lon for a Google places query"
|
24
|
-
elsif key.nil?
|
25
|
-
raise "Google Places API requires an API key"
|
26
|
-
else #lat/lng are valid
|
27
|
-
geocoder = "#{protocol}://maps.googleapis.com/maps/api/place/search/json?language=#{lang}&location=#{[lat.to_s, lng.to_s].join(",")}"
|
28
|
-
output = "&sensor=false&radius=#{radius}&key=#{key}"
|
29
|
-
|
30
|
-
# add filter keyword
|
31
|
-
geocoder += "&keyword=#{keyword}" unless keyword.nil?
|
32
|
-
|
33
|
-
#send request to the google api to get the lat/lng
|
34
|
-
request = geocoder + output
|
35
|
-
url = URI.escape(request)
|
36
|
-
uri = URI.parse(url)
|
37
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
38
|
-
http.use_ssl = true # Places API wants https
|
39
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # to avoid any cert issues
|
40
|
-
|
41
|
-
Gmaps4rails.handle_places_response(request, http.request(Net::HTTP::Get.new(uri.request_uri)), raw)
|
42
|
-
end # end lat/lng valid
|
43
|
-
end
|
44
|
-
|
45
|
-
# handles the places query response
|
46
|
-
def Gmaps4rails.handle_places_response(request, response, raw)
|
47
|
-
#parse result if result received properly
|
48
|
-
if response.is_a?(Net::HTTPSuccess)
|
49
|
-
#parse the json
|
50
|
-
parse = JSON.parse(response.body)
|
51
|
-
#check if google went well
|
52
|
-
if parse["status"] == "OK"
|
53
|
-
return parse if raw == true
|
54
|
-
array = []
|
55
|
-
parse["results"].each do |result|
|
56
|
-
array << {
|
57
|
-
:lat => result["geometry"]["location"]["lat"],
|
58
|
-
:lng => result["geometry"]["location"]["lng"],
|
59
|
-
:name => result["name"],
|
60
|
-
:reference => result["reference"],
|
61
|
-
:vicinity => result["vicinity"],
|
62
|
-
:full_data => result
|
63
|
-
}
|
64
|
-
end
|
65
|
-
return array
|
66
|
-
else #status != OK
|
67
|
-
raise Gmaps4rails::PlacesStatus, "The address you passed seems invalid, status was: #{parse["status"]}.
|
68
|
-
Request was: #{request}"
|
69
|
-
end #end parse status
|
70
|
-
|
71
|
-
else #if not http success
|
72
|
-
raise Gmaps4rails::PlacesNetStatus, "The request sent to google was invalid (not http success): #{request}.
|
73
|
-
Response was: #{response}"
|
74
|
-
end #end resp test
|
75
|
-
end
|
76
|
-
end
|
data/spec/base/geocoding_spec.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "Geocode" do
|
4
|
-
|
5
|
-
it "should raise an error when address invalid" do
|
6
|
-
lambda { Gmaps4rails.geocode("home")}.should raise_error Gmaps4rails::GeocodeStatus
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should return results in the specified language" do
|
10
|
-
result = Gmaps4rails.geocode("Toulon, France", "de")
|
11
|
-
result.first[:matched_address].should eq "Toulon, Frankreich"
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should raise an error when net connection failed" #TODO: Damn, I don't know how to test that!
|
15
|
-
|
16
|
-
end
|
17
|
-
|