gmaps4rails 1.5.8 → 2.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.travis.yml +9 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +149 -0
- data/Guardfile.old +24 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +129 -0
- data/Rakefile +4 -0
- data/app/views/gmaps4rails/_gmaps4rails.html.erb +20 -0
- data/gmaps4rails.gemspec +38 -0
- data/lib/generators/gmaps4rails/install_generator.rb +43 -0
- data/lib/generators/templates/README +2 -0
- data/lib/gmaps4rails.rb +26 -0
- data/lib/gmaps4rails/acts_as_gmappable.rb +54 -0
- 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 +126 -0
- data/lib/gmaps4rails/extensions/enumerable.rb +14 -0
- data/lib/gmaps4rails/extensions/hash.rb +9 -0
- data/lib/gmaps4rails/helper/gmaps4rails_helper.rb +33 -0
- data/lib/gmaps4rails/js_builder.rb +154 -0
- data/lib/gmaps4rails/json_builder.rb +140 -0
- data/lib/gmaps4rails/model_handler.rb +101 -0
- data/lib/gmaps4rails/version.rb +3 -0
- data/lib/gmaps4rails/view_helper.rb +172 -0
- data/lib/tasks/jasmine.rake +8 -0
- data/public/javascripts/gmaps4rails/all.js +2144 -0
- data/public/javascripts/gmaps4rails/base.js +792 -0
- data/public/javascripts/gmaps4rails/google.js +556 -0
- data/public/javascripts/gmaps4rails/openlayers.js +377 -0
- data/public/stylesheets/gmaps4rails.css +24 -0
- data/spec/dummy/.gitignore +3 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +18 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/assets/stylesheets/gmaps4rails.css +24 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +105 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/place.rb +16 -0
- data/spec/dummy/app/models/user.rb +12 -0
- data/spec/dummy/app/views/layouts/application.html.erb +17 -0
- data/spec/dummy/app/views/users/_form.html.erb +37 -0
- data/spec/dummy/app/views/users/edit.html.erb +6 -0
- data/spec/dummy/app/views/users/index.html.erb +268 -0
- data/spec/dummy/app/views/users/new.html.erb +5 -0
- data/spec/dummy/app/views/users/show.html.erb +30 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +62 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/gmaps4rails.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/db/migrate/20120408170155_create_users.rb +16 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/Player.js +22 -0
- data/spec/dummy/public/javascripts/Song.js +7 -0
- data/spec/dummy/public/logo.png +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/factories/place_factory.rb +16 -0
- data/spec/factories/user_factory.rb +23 -0
- 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/launchers/all_but_requests.rb +0 -0
- data/spec/launchers/all_specs.rb +0 -0
- data/spec/launchers/requests.rb +0 -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/lib/js_builder_spec.rb +134 -0
- data/spec/lib/json_builder_spec.rb +232 -0
- data/spec/lib/places_spec.rb +25 -0
- data/spec/models/place_spec.rb +39 -0
- data/spec/models/user_spec.rb +187 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/support/geocoding.rb +40 -0
- data/spec/support/matchers.rb +73 -0
- data/todo +0 -0
- data/vendor/assets/javascripts/gmaps4rails/all.js +1 -0
- data/vendor/assets/javascripts/gmaps4rails/base.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/base/common.coffee +43 -0
- data/vendor/assets/javascripts/gmaps4rails/base/configuration.coffee +7 -0
- data/vendor/assets/javascripts/gmaps4rails/base/gmaps.coffee +13 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/basic_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/controller_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/map_interface.coffee +27 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/marker_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main.coffee +124 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/circle_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/kml_controller.coffee +5 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/marker_controller.coffee +76 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polygon_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polyline_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/circle.coffee +12 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/kml.coffee +8 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/map.coffee +62 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/marker.coffee +24 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/polygon.coffee +11 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/polyline.coffee +11 -0
- data/vendor/assets/javascripts/gmaps4rails/bing.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/main.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/objects/map.coffee +63 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/objects/marker.coffee +78 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/shared.coffee +20 -0
- data/vendor/assets/javascripts/gmaps4rails/google.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/google/main.coffee +47 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/circle.coffee +38 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/kml.coffee +16 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/map.coffee +71 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/marker.coffee +114 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/polygon.coffee +38 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/polyline.coffee +49 -0
- data/vendor/assets/javascripts/gmaps4rails/google/shared.coffee +27 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/main.coffee +138 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/map.coffee +49 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/marker.coffee +68 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/polyline.coffee +39 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/shared.coffee +19 -0
- metadata +378 -14
- checksums.yaml +0 -7
data/lib/gmaps4rails.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
require 'rails'
|
3
|
+
require 'gmaps4rails/base'
|
4
|
+
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
|
7
|
+
initializer "gmaps4rails view helpers" do |app|
|
8
|
+
ActionView::Base.send :include, Gmaps4railsHelper
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
class Railtie < Rails::Railtie
|
14
|
+
|
15
|
+
initializer "include acts_as_gmappable within ORM" do
|
16
|
+
ActiveSupport.on_load(:active_record) do
|
17
|
+
ActiveRecord::Base.send(:include, Gmaps4rails::ActsAsGmappable)
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveSupport.on_load(:mongoid) do
|
21
|
+
Mongoid::Document.send(:include, Gmaps4rails::ActsAsGmappable)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
module ActsAsGmappable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# This is a validation method which triggers the geocoding and save its results
|
6
|
+
def process_geocoding
|
7
|
+
Gmaps4rails::ModelHandler.new(self, gmaps4rails_options).retrieve_coordinates
|
8
|
+
end
|
9
|
+
|
10
|
+
# creates json for one instance
|
11
|
+
def to_gmaps4rails(&block)
|
12
|
+
json = "["
|
13
|
+
object_json = Gmaps4rails.create_json(self, &block)
|
14
|
+
json << object_json.to_s unless object_json.nil?
|
15
|
+
json << "]"
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
def acts_as_gmappable args = {}
|
21
|
+
|
22
|
+
validate :process_geocoding
|
23
|
+
|
24
|
+
#instance method containing all the options to configure the behaviour of the gem regarding the current Model
|
25
|
+
define_method "gmaps4rails_options" do
|
26
|
+
{
|
27
|
+
:process_geocoding => args[:process_geocoding].nil? ? true : args[:process_geocoding],
|
28
|
+
:check_process => args[:check_process].nil? ? true : args[:check_process],
|
29
|
+
:checker => args[:checker] || "gmaps",
|
30
|
+
|
31
|
+
:lat_column => args[:lat] || "latitude",
|
32
|
+
:lng_column => args[:lng] || "longitude",
|
33
|
+
|
34
|
+
# purposefully no default.
|
35
|
+
# Leaving out the :position arg means we are using the default lat/lng to store coordinates
|
36
|
+
:position => args[:position],
|
37
|
+
|
38
|
+
:msg => args[:msg] || "Address invalid",
|
39
|
+
:validation => args[:validation].nil? ? true : args[:validation],
|
40
|
+
|
41
|
+
:language => args[:language] || "en",
|
42
|
+
:protocol => args[:protocol] || "http",
|
43
|
+
|
44
|
+
:address => args[:address] || "gmaps4rails_address",
|
45
|
+
:callback => args[:callback],
|
46
|
+
:normalized_address => args[:normalized_address]
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
|
3
|
+
module BaseNetMethods
|
4
|
+
|
5
|
+
def checked_google_response(&block)
|
6
|
+
raise_net_status unless valid_response?
|
7
|
+
|
8
|
+
raise_query_error unless valid_parsed_response?
|
9
|
+
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
|
13
|
+
def base_url
|
14
|
+
URI.escape base_request
|
15
|
+
end
|
16
|
+
|
17
|
+
def response
|
18
|
+
@response ||= get_response
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid_response?
|
22
|
+
response.is_a?(Net::HTTPSuccess)
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_parsed_response?
|
26
|
+
parsed_response["status"] == "OK"
|
27
|
+
end
|
28
|
+
|
29
|
+
def parsed_response
|
30
|
+
@parsed_response ||= JSON.parse(response.body)
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_response
|
34
|
+
url = URI.parse(base_url)
|
35
|
+
http = Gmaps4rails.http_agent
|
36
|
+
http.get_response(url)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
|
3
|
+
class Direction
|
4
|
+
|
5
|
+
include BaseNetMethods
|
6
|
+
|
7
|
+
attr_reader :ride, :output, :protocol, :options, :polylines, :language
|
8
|
+
attr_accessor :legs
|
9
|
+
|
10
|
+
def initialize(start_end, options= {}, output = "pretty")
|
11
|
+
@ride = OpenStruct.new start_end
|
12
|
+
@output = output
|
13
|
+
@protocol = options.delete(:protocol) || "http"
|
14
|
+
@language = options.delete(:language) || "en"
|
15
|
+
@options = options
|
16
|
+
raise_invalid unless valid?
|
17
|
+
self.legs = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def get
|
21
|
+
checked_google_response do
|
22
|
+
#Each element in the legs array specifies a single leg of the journey from the origin to the destination in the calculated route
|
23
|
+
parsed_response["routes"].first["legs"].each do |leg|
|
24
|
+
extract_polylines(leg) if remove_polylines?
|
25
|
+
self.legs << {
|
26
|
+
"duration" => { "text" => leg["duration"]["text"], "value" => leg["duration"]["value"].to_f },
|
27
|
+
"distance" => { "text" => leg["distance"]["text"], "value" => leg["distance"]["value"].to_f },
|
28
|
+
"steps" => leg["steps"]
|
29
|
+
}
|
30
|
+
merge_polylines_with_leg if output == "pretty"
|
31
|
+
end
|
32
|
+
legs
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def extract_polylines(leg)
|
39
|
+
@polylines = leg["steps"].map {|step| step.delete("polyline")}
|
40
|
+
end
|
41
|
+
|
42
|
+
def polylines_json
|
43
|
+
polylines.map{ |poly| {"coded_array" => poly["points"]} }.to_json
|
44
|
+
end
|
45
|
+
|
46
|
+
def remove_polylines?
|
47
|
+
output == "pretty" || output == "clean"
|
48
|
+
end
|
49
|
+
|
50
|
+
def merge_polylines_with_leg
|
51
|
+
self.legs.last.merge!({ "polylines" => polylines_json })
|
52
|
+
end
|
53
|
+
|
54
|
+
def format_options_for_url
|
55
|
+
return "" if options.empty?
|
56
|
+
"&" + options.map do |k,v|
|
57
|
+
if v.is_a?(Array)
|
58
|
+
k + "=" + v * ("|")
|
59
|
+
else
|
60
|
+
k + "=" + v
|
61
|
+
end
|
62
|
+
end * ("&")
|
63
|
+
end
|
64
|
+
|
65
|
+
def base_request
|
66
|
+
"#{protocol}://maps.googleapis.com/maps/api/directions/json?language=#{language}&origin=#{ride.from}&destination=#{ride.to}&sensor=false" + format_options_for_url
|
67
|
+
end
|
68
|
+
|
69
|
+
def valid?
|
70
|
+
!(ride.from.empty? || ride.to.empty?)
|
71
|
+
end
|
72
|
+
|
73
|
+
def raise_net_status
|
74
|
+
raise Gmaps4rails::DirectionNetStatus, "The request sent to google was invalid (not http success): #{base_request}.\nResponse was: #{response}"
|
75
|
+
end
|
76
|
+
|
77
|
+
def raise_query_error
|
78
|
+
raise raise Gmaps4rails::DirectionStatus, "The query you passed seems invalid, status was: #{parsed_response["status"]}.\nRequest was: #{base_request}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def raise_invalid
|
82
|
+
raise Gmaps4rails::DirectionInvalidQuery, "Origin and destination must be provided in a hash as first argument"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
|
3
|
+
class Geocoder
|
4
|
+
include BaseNetMethods
|
5
|
+
|
6
|
+
attr_reader :address, :language, :raw, :protocol
|
7
|
+
|
8
|
+
def initialize(address, options = {})
|
9
|
+
raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address" if address.empty?
|
10
|
+
|
11
|
+
@address = address
|
12
|
+
@language = options[:language] || "en"
|
13
|
+
@raw = options[:raw] || false
|
14
|
+
@protocol = options[:protocol] || "http"
|
15
|
+
end
|
16
|
+
|
17
|
+
# returns an array of hashes with the following keys:
|
18
|
+
# - lat: mandatory for acts_as_gmappable
|
19
|
+
# - lng: mandatory for acts_as_gmappable
|
20
|
+
# - matched_address: facultative
|
21
|
+
# - bounds: facultative
|
22
|
+
# - full_data: facultative
|
23
|
+
def get_coordinates
|
24
|
+
checked_google_response do
|
25
|
+
return parsed_response if raw
|
26
|
+
parsed_response["results"].inject([]) do |memo, result|
|
27
|
+
memo << {
|
28
|
+
:lat => result["geometry"]["location"]["lat"],
|
29
|
+
:lng => result["geometry"]["location"]["lng"],
|
30
|
+
:matched_address => result["formatted_address"],
|
31
|
+
:bounds => result["geometry"]["bounds"],
|
32
|
+
:full_data => result
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def base_request
|
41
|
+
"#{protocol}://maps.googleapis.com/maps/api/geocode/json?language=#{language}&address=#{address}&sensor=false"
|
42
|
+
end
|
43
|
+
|
44
|
+
def raise_net_status
|
45
|
+
raise Gmaps4rails::GeocodeNetStatus, "The request sent to google was invalid (not http success): #{base_request}.\nResponse was: #{response}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def raise_query_error
|
49
|
+
raise Gmaps4rails::GeocodeStatus, "The address you passed seems invalid, status was: #{parsed_response["status"]}.\nRequest was: #{base_request}"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
|
3
|
+
class Places
|
4
|
+
|
5
|
+
include BaseNetMethods
|
6
|
+
|
7
|
+
attr_reader :lat, :lng
|
8
|
+
delegate :key, :keyword, :radius, :lang, :raw, :protocol, :to => :@options
|
9
|
+
|
10
|
+
def initialize(lat, lng, options = {})
|
11
|
+
@lat, @lng = lat, lng
|
12
|
+
raise_invalid unless valid_position?
|
13
|
+
raise_missing_key unless options[:key]
|
14
|
+
options[:radius] ||= 7500
|
15
|
+
options[:lang] ||= "en"
|
16
|
+
options[:raw] ||= false
|
17
|
+
options[:protocol]||= "http"
|
18
|
+
@options = OpenStruct.new options
|
19
|
+
end
|
20
|
+
|
21
|
+
def get
|
22
|
+
checked_google_response do
|
23
|
+
return parsed_response if raw
|
24
|
+
parsed_response["results"].inject([]) do |memo, result|
|
25
|
+
memo << {
|
26
|
+
:lat => result["geometry"]["location"]["lat"],
|
27
|
+
:lng => result["geometry"]["location"]["lng"],
|
28
|
+
:name => result["name"],
|
29
|
+
:reference => result["reference"],
|
30
|
+
:vicinity => result["vicinity"],
|
31
|
+
:full_data => result
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def base_request
|
40
|
+
req = "#{protocol}://maps.googleapis.com/maps/api/place/search/json?language=#{lang}&location=#{lat},#{lng}&sensor=false&radius=#{radius}&key=#{key}"
|
41
|
+
req += "&keyword=#{keyword}" unless keyword.nil?
|
42
|
+
req
|
43
|
+
end
|
44
|
+
|
45
|
+
def valid_position?
|
46
|
+
!(lat.nil? || lng.nil?)
|
47
|
+
end
|
48
|
+
|
49
|
+
def raise_invalid
|
50
|
+
raise Gmaps4rails::PlacesInvalidQuery, "You must provide at least a lat/lon for a Google places query"
|
51
|
+
end
|
52
|
+
|
53
|
+
def raise_missing_key
|
54
|
+
raise "Google Places API requires an API key"
|
55
|
+
end
|
56
|
+
|
57
|
+
def raise_net_status
|
58
|
+
raise Gmaps4rails::PlacesNetStatus, "The request sent to google was invalid (not http success): #{base_request}.\nResponse was: #{response}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def raise_query_error
|
62
|
+
raise Gmaps4rails::PlacesStatus, "The address you passed seems invalid, status was: #{parsed_response["status"]}.\nRequest was: #{base_request}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_response
|
66
|
+
uri = URI.parse(base_url)
|
67
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
68
|
+
http.use_ssl = true # Places API wants https
|
69
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # to avoid any cert issues
|
70
|
+
http.request(Net::HTTP::Get.new(uri.request_uri))
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
module Gmaps4rails
|
7
|
+
|
8
|
+
require 'gmaps4rails/extensions/enumerable'
|
9
|
+
require 'gmaps4rails/extensions/hash'
|
10
|
+
|
11
|
+
autoload :ModelHandler, 'gmaps4rails/model_handler'
|
12
|
+
autoload :ActsAsGmappable, 'gmaps4rails/acts_as_gmappable'
|
13
|
+
|
14
|
+
autoload :JsBuilder, 'gmaps4rails/js_builder'
|
15
|
+
autoload :JsonBuilder, 'gmaps4rails/json_builder'
|
16
|
+
autoload :ViewHelper, 'gmaps4rails/view_helper'
|
17
|
+
autoload :Gmaps4railsHelper,'gmaps4rails/helper/gmaps4rails_helper'
|
18
|
+
|
19
|
+
autoload :BaseNetMethods, 'gmaps4rails/api_wrappers/base_net_methods'
|
20
|
+
autoload :Geocoder, 'gmaps4rails/api_wrappers/geocoder'
|
21
|
+
autoload :Direction, 'gmaps4rails/api_wrappers/direction'
|
22
|
+
autoload :Places, 'gmaps4rails/api_wrappers/places'
|
23
|
+
autoload :ObjectAccessor, 'gmaps4rails/object_accessor'
|
24
|
+
|
25
|
+
mattr_accessor :http_proxy, :escape_js_url
|
26
|
+
|
27
|
+
self.escape_js_url = true
|
28
|
+
|
29
|
+
# This method geocodes an address using the GoogleMaps webservice
|
30
|
+
# options are:
|
31
|
+
# * address: string, mandatory
|
32
|
+
# * lang: to set the language one wants the result to be translated (default is english)
|
33
|
+
# * raw: to get the raw response from google, default is false
|
34
|
+
def Gmaps4rails.geocode(address, lang="en", raw = false, protocol = "http")
|
35
|
+
::Gmaps4rails::Geocoder.new(address, {
|
36
|
+
:language => lang,
|
37
|
+
:raw => raw,
|
38
|
+
:protocol => protocol
|
39
|
+
}).get_coordinates
|
40
|
+
end
|
41
|
+
|
42
|
+
def Gmaps4rails.create_json(object, &block)
|
43
|
+
::Gmaps4rails::JsonBuilder.new(object).process(&block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def Gmaps4rails.create_js_from_hash(hash)
|
47
|
+
::Gmaps4rails::JsBuilder.new(hash).create_js
|
48
|
+
end
|
49
|
+
|
50
|
+
# This method retrieves destination results provided by GoogleMaps webservice
|
51
|
+
# options are:
|
52
|
+
# * start_end: Hash { "from" => string, "to" => string}, mandatory
|
53
|
+
# * options: details given in the github's wiki
|
54
|
+
# * output: could be "pretty", "raw" or "clean"; filters the output from google
|
55
|
+
#output could be raw, pretty or clean
|
56
|
+
def Gmaps4rails.destination(start_end, options={}, output="pretty")
|
57
|
+
Gmaps4rails::Direction.new(start_end, options, output).get
|
58
|
+
end
|
59
|
+
|
60
|
+
# does two things... 1) gecode the given address string and 2) triggers a a places query around that geo location
|
61
|
+
# optionally a keyword can be given for a filter over all places fields (e.g. "Bungy" to give all Bungy related places)
|
62
|
+
# IMPORTANT: Places API calls require an API key (param "key")
|
63
|
+
|
64
|
+
def Gmaps4rails.places_for_address(address, key, keyword = nil, radius = 7500, lang="en", raw = false)
|
65
|
+
raise Gmaps4rails::GeocodeInvalidQuery, "you must provide an address for a places_for_address query" if address.nil?
|
66
|
+
raise "Google Places API requires an API key" if key.nil?
|
67
|
+
res = Gmaps4rails.geocode(address) # will throw exception if nothing could be geocoded
|
68
|
+
Gmaps4rails.places(res.first[:lat], res.first[:lng], key, keyword, radius, lang, raw)
|
69
|
+
end
|
70
|
+
|
71
|
+
# does a places query around give geo location (lat/lng)
|
72
|
+
# optionally a keyword can be given for a filter over all places fields (e.g. "Bungy" to give all Bungy related places)
|
73
|
+
# IMPORTANT: Places API calls require an API key (param "key")
|
74
|
+
def Gmaps4rails.places(lat, lng, key, keyword = nil, radius = 7500, lang="en", raw = false, protocol = "https")
|
75
|
+
Gmaps4rails::Places.new(lat, lng, {
|
76
|
+
:key => key,
|
77
|
+
:keyword => keyword,
|
78
|
+
:radius => radius,
|
79
|
+
:lang => lang,
|
80
|
+
:raw => raw,
|
81
|
+
:protocol => protocol
|
82
|
+
}).get
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
class GeocodeStatus < StandardError; end
|
88
|
+
class GeocodeNetStatus < StandardError; end
|
89
|
+
class GeocodeInvalidQuery < StandardError; end
|
90
|
+
|
91
|
+
class DirectionStatus < StandardError; end
|
92
|
+
class DirectionNetStatus < StandardError; end
|
93
|
+
class DirectionInvalidQuery < StandardError; end
|
94
|
+
|
95
|
+
class PlacesStatus < StandardError; end
|
96
|
+
class PlacesNetStatus < StandardError; end
|
97
|
+
class PlacesInvalidQuery < StandardError; end
|
98
|
+
|
99
|
+
def Gmaps4rails.condition_eval(object, condition)
|
100
|
+
case condition
|
101
|
+
when Symbol, String then object.send condition
|
102
|
+
when Proc then condition.call(object)
|
103
|
+
when TrueClass, FalseClass then condition
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# looks for proxy settings and returns a Net::HTTP or Net::HTTP::Proxy class
|
108
|
+
def Gmaps4rails.http_agent
|
109
|
+
proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] || self.http_proxy
|
110
|
+
if proxy
|
111
|
+
proxy = URI.parse(proxy)
|
112
|
+
http_agent = Net::HTTP::Proxy(proxy.host,proxy.port)
|
113
|
+
else
|
114
|
+
http_agent = Net::HTTP
|
115
|
+
end
|
116
|
+
http_agent
|
117
|
+
end
|
118
|
+
|
119
|
+
# checks whether or not the app has pipeline enabled
|
120
|
+
# works for Rails 3.0.x and above
|
121
|
+
# @return [Boolean]
|
122
|
+
def Gmaps4rails.pipeline_enabled?
|
123
|
+
Rails.configuration.respond_to?('assets') && Rails.configuration.assets.enabled
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|