googlestaticmap 1.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.1.2 / 2012-04-27
2
+ * Fixed bug from 1.1.1 when specifying https when calling get_map (thanks aduncan for catching it!)
3
+
4
+ === 1.1.1 / 2012-04-22
5
+ * Added support for specifying the protocol to use (thanks andruby!)
6
+
1
7
  === 1.1.0 / 2011-10-18
2
8
  * Added an option to make requests through a proxy server (thanks bardsley!)
3
9
 
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  = googlestaticmap gem
2
2
 
3
- This gem is on Gemcutter, simply type "gem install googlestaticmap" to install it.
3
+ This gem is on Rubygems, simply type "gem install googlestaticmap" to install it.
4
4
 
5
5
  Available on github at http://github.com/brentsowers1/googlestaticmap
6
6
 
@@ -14,14 +14,20 @@ image to a file.
14
14
  == Examples:
15
15
 
16
16
  Get a simple static map centered at Washington, DC, with the default size
17
- (500 x 350), zoomed to level 11
17
+ (500 x 350), zoomed to level 11. image will be the binary data of the map
18
18
  require 'googlestaticmap'
19
19
  map = GoogleStaticMap.new(:zoom => 11, :center => MapLocation.new(:address => "Washington, DC"))
20
20
  image = map.get_map
21
21
 
22
+ Get the URL of the image described in the previous example, so you can insert
23
+ this URL as the src of an img element on an HTML page
24
+ require 'googlestaticmap'
25
+ map = GoogleStaticMap.new(:zoom => 11, :center => MapLocation.new(:address => "Washington, DC"))
26
+ image_url = map.url(:auto)
27
+
22
28
  Get a map with blue markers at the White House and the Supreme Court, zoomed
23
29
  the closest that the map can be with both markers visible, at the default
24
- size.
30
+ size. image will be the binary data of the map
25
31
  require 'googlestaticmap'
26
32
  map = GoogleStaticMap.new
27
33
  map.markers << MapMarker.new(:color => "blue", :location => MapLocation.new(:address => "1600 Pennsylvania Ave., Washington, DC"))
@@ -51,4 +57,4 @@ Author:: Brent Sowers (mailto:brent@coordinatecommons.com)
51
57
  License:: You're free to do whatever you want with this
52
58
 
53
59
  To post comments about this gem, go to my blog posting at
54
- http://rails.brentsowers.com/2010/08/gem-for-getting-google-static-maps.html
60
+ http://www.brentsowers.com/2010/08/gem-for-getting-google-static-maps.html
@@ -1,6 +1,6 @@
1
1
  # = googlestaticmap gem
2
2
  #
3
- # This gem is on Gemcutter, simply type "gem install googlestaticmap" to install it.
3
+ # This gem is on Rubygems, simply type "gem install googlestaticmap" to install it.
4
4
  #
5
5
  # Available on github at http://github.com/brentsowers1/googlestaticmap
6
6
  #
@@ -18,6 +18,12 @@
18
18
  # map = GoogleStaticMap.new(:zoom => 11, :center => MapLocation.new(:address => "Washington, DC"))
19
19
  # image = map.get_map
20
20
  #
21
+ # Get the URL of the image described in the previous example, so you can insert
22
+ # this URL as the src of an img element on an HTML page
23
+ # require 'googlestaticmap'
24
+ # map = GoogleStaticMap.new(:zoom => 11, :center => MapLocation.new(:address => "Washington, DC"))
25
+ # image_url = map.url(:auto)
26
+ #
21
27
  # Get a map with blue markers at the White House and the Supreme Court, zoomed
22
28
  # the closest that the map can be with both markers visible, at the default
23
29
  # size.
@@ -45,6 +51,9 @@
45
51
  #
46
52
  # Author:: Brent Sowers (mailto:brent@coordinatecommons.com)
47
53
  # License:: You're free to do whatever you want with this
54
+ #
55
+ # To post comments about this gem, go to my blog posting at
56
+ # http://www.brentsowers.com/2010/08/gem-for-getting-google-static-maps.html
48
57
 
49
58
  require 'cgi'
50
59
  require 'net/http'
@@ -119,11 +128,18 @@ class GoogleStaticMap
119
128
  # Returns the full URL to retrieve this static map. You can use this as the
120
129
  # src for an img to display an image directly on a web page
121
130
  # Example - "http://maps.google.com/maps/api/staticmap?params..."
122
- def url
131
+ # +protocol+ can be 'http', 'https' or :auto. Specifying :auto will not return
132
+ # a protocol in the URL ("//maps.google.com/..."), allowing the browser to
133
+ # select the appropriate protocol (if the page is loaded with https, it will
134
+ # use https). Defaults to http
135
+ def url(protocol='http')
123
136
  unless @center || @markers.length > 0 || @paths.length > 0
124
137
  raise Exception.new("Need to specify either a center, markers, or a path")
125
138
  end
126
- u = "http://maps.google.com/maps/api/staticmap?"
139
+ protocol = 'http' unless protocol == 'http' || protocol == 'https' ||
140
+ protocol == :auto
141
+ protocol = protocol == :auto ? '' : protocol + ":"
142
+ u = "#{protocol}//maps.google.com/maps/api/staticmap?"
127
143
  attrs = GoogleStaticMapHelpers.safe_instance_variables(self,
128
144
  ["markers", "paths", "width", "height", "center",
129
145
  "proxy_address", "proxy_port"],
@@ -137,16 +153,26 @@ class GoogleStaticMap
137
153
 
138
154
  # Returns the URL to retrieve the map, relative to http://maps.google.com
139
155
  # Example - "/maps/api/staticmap?params..."
140
- def relative_url
141
- url.gsub(/http\:\/\/maps\.google\.com/, "")
156
+ def relative_url(protocol='http')
157
+ url(protocol).gsub(/[^\/]*\/\/maps\.google\.com/, "")
142
158
  end
143
159
 
160
+
144
161
  # Connects to Google, retrieves the map, and returns the bytes for the image.
145
162
  # Optionally, pass it an output name and the contents will get written to
146
163
  # this file name
147
- def get_map(output_file=nil)
148
- http = Net::HTTP.Proxy(@proxy_address,@proxy_port).new("maps.google.com", 80)
149
- resp, data = http.get2(relative_url)
164
+ # +output_file+ - optionally give the name of a file to write the output to.
165
+ # Pass nil to not write the output to a file
166
+ # +protocol+ - specify http or https here for the protocol to retrieve the
167
+ # map with. Defaults to http
168
+ # return value - the binary data for the map
169
+ def get_map(output_file=nil, protocol='http')
170
+ protocol = 'http' unless protocol == 'http' || protocol == 'https'
171
+ port = protocol == 'https' ? 443 : 80
172
+ http = Net::HTTP.Proxy(@proxy_address,@proxy_port).new("maps.google.com", port)
173
+ http.use_ssl = protocol == 'https'
174
+
175
+ resp, data = http.get2(relative_url(protocol))
150
176
  if resp && resp.is_a?(Net::HTTPSuccess)
151
177
  if output_file
152
178
  File.open(output_file, "wb") {|f| f << data }
@@ -24,6 +24,9 @@ end
24
24
  class MockHttp #:nodoc: all
25
25
  def initialize
26
26
  end
27
+ def use_ssl=(value)
28
+ true
29
+ end
27
30
  end
28
31
 
29
32
  class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
@@ -68,9 +71,32 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
68
71
  assert !f.include?("http://maps.google.com")
69
72
  end
70
73
 
71
- def test_get_map_success_no_file
74
+ def test_url_auto
75
+ g = default_map
76
+ u = nil
77
+ assert_nothing_raised { u = g.url(:auto) }
78
+ assert_equal 7, u.split("&").length, u
79
+ assert u =~ /^\/\/maps.google.com/, u
80
+ f = nil
81
+ assert_nothing_raised {f = g.relative_url}
82
+ assert_no_match /^\/\/maps.google.com/, f
83
+ end
84
+
85
+ def test_url_https
86
+ g = default_map
87
+ u = nil
88
+ assert_nothing_raised { u = g.url('https') }
89
+ assert_equal 7, u.split("&").length, u
90
+ assert u =~ /^https:\/\/maps.google.com/
91
+ f = nil
92
+ assert_nothing_raised {f = g.relative_url}
93
+ assert_no_match /^https:\/\/maps.google.com/, f
94
+ end
95
+
96
+ def test_get_map_success_no_file_http
72
97
  test_data = "asdf"
73
98
  MockHttp.any_instance.expects(:get2).returns([MockSuccess.new,test_data])
99
+ MockHttp.any_instance.expects(:"use_ssl=").with(false).returns(false)
74
100
  Net::HTTP.expects(:new).returns(MockHttp.new)
75
101
 
76
102
  g = default_map
@@ -79,6 +105,18 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
79
105
  assert_equal r, test_data
80
106
  end
81
107
 
108
+ def test_get_map_success_no_file_https
109
+ test_data = "asdf"
110
+ MockHttp.any_instance.expects(:get2).returns([MockSuccess.new,test_data])
111
+ MockHttp.any_instance.expects(:"use_ssl=").with(true).returns(true)
112
+ Net::HTTP.expects(:new).returns(MockHttp.new)
113
+
114
+ g = default_map
115
+ r = nil
116
+ assert_nothing_raised {r = g.get_map(nil, 'https')}
117
+ assert_equal r, test_data
118
+ end
119
+
82
120
  def test_get_map_success_write_file
83
121
  test_data = "asdf"
84
122
  MockHttp.any_instance.expects(:get2).returns([MockSuccess.new,test_data])
@@ -97,6 +135,24 @@ class GoogleStaticMapTest < Test::Unit::TestCase #:nodoc: all
97
135
  assert_equal file_data, test_data
98
136
  end
99
137
 
138
+ def test_get_map_success_check_url
139
+ test_data = "asdf"
140
+ MockHttp.any_instance.expects(:get2).returns([MockSuccess.new,test_data])
141
+ Net::HTTP.expects(:new).returns(MockHttp.new)
142
+ file_data = ""
143
+ file_name = "testdata.png"
144
+ # File.open should be called, with the name of the file and write binary
145
+ # passed in. The object passed to the yield should be a file object that
146
+ # gets the test data appended to it. If we sub out a string for the
147
+ # file object we can later check the contents
148
+ File.expects(:open).with(file_name, "wb").yields(file_data)
149
+ g = default_map
150
+ r = nil
151
+ assert_nothing_raised {r = g.get_map(file_name)}
152
+ assert_equal r, test_data
153
+ assert_equal file_data, test_data
154
+ end
155
+
100
156
 
101
157
  def test_get_map_failure
102
158
  MockHttp.any_instance.expects(:get2).returns([MockFailure.new,""])
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: googlestaticmap
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.0
5
+ version: 1.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brent Sowers
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-18 00:00:00 Z
13
+ date: 2012-04-27 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Easily retrieve single PNG, GIF, or JPG map images from Google with your own custom markers and paths using the Static Maps API service with this gem. Simply set the attributes you want for your map and GoogleStaticMap will take care of getting the map for you, or giving your the URL to retrieve the map.