static_map 0.1.0 → 0.1.1
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/lib/static_map.rb +11 -11
- data/lib/static_map/version.rb +1 -1
- data/test/static_map_test.rb +12 -9
- metadata +1 -1
data/lib/static_map.rb
CHANGED
@@ -19,10 +19,10 @@ module StaticMap
|
|
19
19
|
# center String - center the map around this location
|
20
20
|
# zoom Integer - zoom level of the map
|
21
21
|
# size String - in pixels of the image. 500x500
|
22
|
-
# sensor Boolean - autodetect user
|
23
|
-
# markers
|
24
|
-
# maptype String -
|
25
|
-
# path String -
|
22
|
+
# sensor Boolean - autodetect user location
|
23
|
+
# markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates
|
24
|
+
# maptype String - type of map (satellite, road, hybrid, terrain)
|
25
|
+
# path String - path to write file to when #save is called
|
26
26
|
# alt String - alt text if using image tag
|
27
27
|
# title String - title text if using image tag
|
28
28
|
attr_accessor :center, :zoom, :size, :sensor, :markers, :maptype, :path, :alt, :title
|
@@ -42,11 +42,11 @@ module StaticMap
|
|
42
42
|
def save
|
43
43
|
raise "Please set the path argument to save the image" unless @path
|
44
44
|
|
45
|
-
File.open(@path, "w") { |f| f.write(open(url).read) }
|
45
|
+
File.open(@path, "w") { |f| f.write( open(url).read ) }
|
46
46
|
end
|
47
47
|
|
48
48
|
def url
|
49
|
-
"#{URL}?#{params}"
|
49
|
+
"#{URL}?#{params}".strip
|
50
50
|
end
|
51
51
|
|
52
52
|
def params
|
@@ -61,12 +61,12 @@ module StaticMap
|
|
61
61
|
def marker_params
|
62
62
|
@markers.map do |marker|
|
63
63
|
str = ["markers="]
|
64
|
-
str << ["color:#{marker[:color]}"] if marker[:color]
|
65
|
-
str << ["label:#{marker[:label]}"] if marker[:label]
|
66
|
-
str << ["#{marker[:location]}"] if marker[:location]
|
64
|
+
str << [CGI.escape("color:#{marker[:color]}")] if marker[:color]
|
65
|
+
str << [CGI.escape("label:#{marker[:label]}")] if marker[:label]
|
66
|
+
str << [CGI.escape("#{marker[:location]}")] if marker[:location]
|
67
67
|
str << ["#{marker[:latitude]},#{marker[:longitude]}"] if marker[:latitude] && marker[:longitude]
|
68
|
-
str.join("
|
69
|
-
end.join("&").gsub(
|
68
|
+
str.map{|v| v }.join("%7C") # %7C
|
69
|
+
end.join("&").gsub(/\=\%7C/i, '=')
|
70
70
|
end
|
71
71
|
|
72
72
|
def to_html
|
data/lib/static_map/version.rb
CHANGED
data/test/static_map_test.rb
CHANGED
@@ -7,22 +7,22 @@ class StaticMapTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
def test_defaults
|
9
9
|
img = StaticMap::Image.new
|
10
|
-
assert_equal "http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true", img.url
|
11
|
-
assert_equal %{<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true' title='' alt=''/>}, img.to_html
|
10
|
+
assert_equal "http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true&maptype=road", img.url
|
11
|
+
assert_equal %{<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true&maptype=road' title='' alt=''/>}, img.to_html
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_params
|
15
15
|
img = StaticMap::Image.new
|
16
|
-
assert_equal "size=500x500&zoom=1&sensor=true", img.params
|
16
|
+
assert_equal "size=500x500&zoom=1&sensor=true&maptype=road", img.params
|
17
17
|
img.size = '900x900'
|
18
|
-
assert_equal "size=900x900&zoom=1&sensor=true", img.params
|
18
|
+
assert_equal "size=900x900&zoom=1&sensor=true&maptype=road", img.params
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_marker_params
|
22
22
|
img = StaticMap::Image.new
|
23
23
|
img.markers << { latitude: 44.477462, longitude: -73.212032, color: "red", label: "A" }
|
24
24
|
img.markers << { location: "Winooski, Vermont", color: "red", label: "B" }
|
25
|
-
assert_equal "markers=color
|
25
|
+
assert_equal "markers=color%3Ared%7Clabel%3AA%7C44.477462,-73.212032&markers=color%3Ared%7Clabel%3AB%7CWinooski%2C+Vermont", img.marker_params
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_url
|
@@ -32,18 +32,21 @@ class StaticMapTest < Test::Unit::TestCase
|
|
32
32
|
img.zoom = 11
|
33
33
|
img.sensor = false
|
34
34
|
img.markers << { latitude: 44.477462, longitude: -73.212032, color: "green", label: "A" }
|
35
|
-
assert_equal "http://maps.google.com/maps/api/staticmap?size=300x300&zoom=11&sensor=false&markers=color
|
35
|
+
assert_equal "http://maps.google.com/maps/api/staticmap?size=300x300&zoom=11&sensor=false&maptype=road&markers=color%3Agreen%7Clabel%3AA%7C44.477462,-73.212032", img.url
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_to_html
|
39
39
|
img = StaticMap::Image.new(:size => "500x500", :alt => "Alt Test", :title => "Title Test")
|
40
|
-
assert_equal "<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true' title='Title Test' alt='Alt Test'/>", img.to_html
|
40
|
+
assert_equal "<img src='http://maps.google.com/maps/api/staticmap?size=500x500&zoom=1&sensor=true&maptype=road' title='Title Test' alt='Alt Test'/>", img.to_html
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_save
|
44
44
|
# TODO fake the remote resource
|
45
|
-
img = StaticMap::Image.new(path: "./test/tmp.png")
|
45
|
+
img = StaticMap::Image.new(path: "./test/tmp.png")
|
46
|
+
img.markers << { latitude: 44.477462, longitude: -73.212032, color: "red", label: "B" }
|
47
|
+
img.markers << { location: "Santa Barbara,California", color: "blue", label: "A" }
|
48
|
+
img.save
|
46
49
|
assert File.exists?("./test/tmp.png")
|
47
|
-
File.delete("./test/tmp.png")
|
50
|
+
# File.delete("./test/tmp.png")
|
48
51
|
end
|
49
52
|
end
|