google_static_maps_helper 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -84,12 +84,24 @@ Another thing you might want to do is to override the center point and zoom leve
84
84
 
85
85
 
86
86
 
87
- == Paths
88
- Paths are not yet supported.
87
+ == Paths and Polygons
88
+ It is also possible to create Paths and Polygons and to use them together with markers. To create a Path you need two Points and a Path object:
89
+ path = GoogleStaticMapsHelper::Path.new
90
+ start_point = {:lat => 1, :lng => 2} # You can also use any object which responds to lat and lng here
91
+ end_point = {:lat => 1, :lng => 2}
92
+ path << start_point << end_point
93
+ map << path
94
+ map.url
95
+
96
+ To create a polygon you need one more point, and to set the fillcolor of the path to a valid color. Setting the fill
97
+ color is what will trigger the creation of polygons for the static map API.
98
+ path = GoogleStaticMapsHelper::Path.new(:fillcolor => :red, :weight => 3, :color => :blue)
99
+ path << point1 << point2 << point3
100
+ map << path
101
+ map.url
89
102
 
90
103
 
91
104
  == TODO
92
- * Paths
93
105
  * Ruby 1.9 support
94
106
 
95
107
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.3.0
data/changelog.txt CHANGED
@@ -1,3 +1,9 @@
1
+ = v.1.3.0
2
+ * It is now possible to create Paths which is used to represent lines and polygons in the map. This is not included in the little DSL we have though.
3
+ * The map may now request a URL for a specified image format. As an exampe, set map.format = :jpg to get smaller images (but also less quality).
4
+ * You can now request different map types, like satellite, hybrid, terrain etc.
5
+ * The marker has no responsibility for it's location any more. It hands off the work to Location object if it is asked for anything related to Location.
6
+
1
7
  = v.1.2.3
2
8
  * It is no longer needed to send in key, sensor and size to the Map's constructor as long as these values are set on GoogleStaticMapsHelper.
3
9
  * A map does now respond to width and height.
@@ -1,6 +1,8 @@
1
1
  require 'uri'
2
2
  require File.dirname(__FILE__) + '/google_static_maps_helper/map'
3
+ require File.dirname(__FILE__) + '/google_static_maps_helper/location'
3
4
  require File.dirname(__FILE__) + '/google_static_maps_helper/marker'
5
+ require File.dirname(__FILE__) + '/google_static_maps_helper/path'
4
6
 
5
7
  module GoogleStaticMapsHelper
6
8
  API_URL = 'http://maps.google.com/maps/api/staticmap'
@@ -8,6 +10,8 @@ module GoogleStaticMapsHelper
8
10
  class OptionMissing < ArgumentError; end # Raised when required options is not sent in during construction
9
11
  class OptionNotExist < ArgumentError; end # Raised when incoming options include keys which is invalid
10
12
  class BuildDataMissing < Exception; end # Raised when incoming options include keys which is invalid
13
+ class UnsupportedFormat < ArgumentError; end # Raised when a format is not supported
14
+ class UnsupportedMaptype < ArgumentError; end # Raised when the map type is not supported
11
15
 
12
16
  class << self
13
17
  attr_accessor :key, :size, :sensor
@@ -0,0 +1,44 @@
1
+ module GoogleStaticMapsHelper
2
+
3
+ # Represents a location (latitude and longitude)
4
+ #
5
+ # This class will also hold logic like travel_to(heading, distance) which will make
6
+ # drawing paths and polygons easier.
7
+ class Location
8
+ class NoLngMethod < NoMethodError; end
9
+ class NoLatMethod < NoMethodError; end
10
+ class NoLatKey < ArgumentError; end
11
+ class NoLngKey < ArgumentError; end
12
+
13
+ attr_accessor :lat, :lng
14
+
15
+ def initialize(*args)
16
+ raise ArgumentError, "Must have some arguments." if args.length == 0
17
+
18
+ if args.first.is_a? Hash
19
+ extract_location_from_hash!(args.first)
20
+ else
21
+ extract_location_from_object(args.shift)
22
+ end
23
+ end
24
+
25
+ def to_url
26
+ [lat, lng].join(',')
27
+ end
28
+
29
+ private
30
+ def extract_location_from_hash!(location_hash)
31
+ raise NoLngKey unless location_hash.has_key? :lng
32
+ raise NoLatKey unless location_hash.has_key? :lat
33
+ @lat = location_hash.delete(:lat)
34
+ @lng = location_hash.delete(:lng)
35
+ end
36
+
37
+ def extract_location_from_object(location)
38
+ raise NoLngMethod unless location.respond_to? :lng
39
+ raise NoLatMethod unless location.respond_to? :lat
40
+ @lat = location.lat
41
+ @lng = location.lng
42
+ end
43
+ end
44
+ end
@@ -1,6 +1,6 @@
1
1
  module GoogleStaticMapsHelper
2
2
  # Represents the map we are generating
3
- # It holds markers and iterates over them to build the URL
3
+ # It holds markers and paths and iterates over them to build the URL
4
4
  # to be used in an image tag.
5
5
  class Map
6
6
  include Enumerable
@@ -8,8 +8,11 @@ module GoogleStaticMapsHelper
8
8
  MAX_WIDTH = 640
9
9
  MAX_HEIGHT = 640
10
10
 
11
+ VALID_FORMATS = %w{png png8 png32 gif jpg jpg-basedline}
12
+ VALID_MAP_TYPES = %w{roadmap satellite terrain hybrid}
13
+
11
14
  REQUIRED_OPTIONS = [:key, :size, :sensor]
12
- OPTIONAL_OPTIONS = [:center, :zoom, :format, :maptype, :mobile, :language]
15
+ OPTIONAL_OPTIONS = [:center, :zoom, :format, :maptype, :mobile, :language, :format, :maptype]
13
16
 
14
17
  attr_accessor *(REQUIRED_OPTIONS + OPTIONAL_OPTIONS)
15
18
  attr_accessor :width, :height
@@ -24,11 +27,11 @@ module GoogleStaticMapsHelper
24
27
  validate_options(options)
25
28
 
26
29
  options.each_pair { |k, v| send("#{k}=", v) }
27
- @markers = []
30
+ @map_enteties = []
28
31
  end
29
32
 
30
33
  def url
31
- raise BuildDataMissing, "We have to have markers or center and zoom set when url is called!" unless can_build?
34
+ raise BuildDataMissing, "We have to have markers, paths or center and zoom set when url is called!" unless can_build?
32
35
 
33
36
  out = "#{API_URL}?"
34
37
 
@@ -45,33 +48,45 @@ module GoogleStaticMapsHelper
45
48
  params << "markers=#{marker_options_as_url_params}|#{markers_locations}"
46
49
  end
47
50
  out += "&#{params.join('&')}" unless params.empty?
51
+
52
+ params = []
53
+ paths.each {|path| params << path.url_params}
54
+ out += "&#{params.join('&')}" unless params.empty?
48
55
 
49
56
  out
50
57
  end
51
58
 
59
+ def markers
60
+ @map_enteties.select {|e| e.is_a? Marker}
61
+ end
62
+
52
63
  def grouped_markers
53
- inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker|
64
+ markers.inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker|
54
65
  groups[marker.options_to_url_params] << marker
55
66
  groups
56
67
  end
57
68
  end
69
+
70
+ def paths
71
+ @map_enteties.select {|e| e.is_a? Path}
72
+ end
58
73
 
59
- def <<(marker)
60
- @markers << marker
61
- @markers.uniq!
74
+ def <<(entity)
75
+ @map_enteties << entity
76
+ @map_enteties.uniq!
62
77
  self
63
78
  end
64
79
 
65
80
  def each
66
- @markers.each {|m| yield(m)}
81
+ @map_enteties.each {|m| yield(m)}
67
82
  end
68
83
 
69
84
  def empty?
70
- @markers.empty?
85
+ @map_enteties.empty?
71
86
  end
72
87
 
73
88
  def length
74
- @markers.length
89
+ @map_enteties.length
75
90
  end
76
91
 
77
92
  def marker(*args)
@@ -111,9 +126,20 @@ module GoogleStaticMapsHelper
111
126
  end
112
127
  end
113
128
 
129
+ def format=(format)
130
+ @format = format.to_s
131
+ raise UnsupportedFormat unless VALID_FORMATS.include? @format
132
+ end
133
+
134
+ def maptype=(type)
135
+ @maptype = type.to_s
136
+ raise UnsupportedMaptype unless VALID_MAP_TYPES.include? @maptype
137
+ end
138
+
139
+
114
140
  private
115
141
  def can_build?
116
- !@markers.empty? || (center && zoom)
142
+ !@map_enteties.empty? || (center && zoom)
117
143
  end
118
144
 
119
145
  def validate_required_options(options)
@@ -3,11 +3,6 @@ module GoogleStaticMapsHelper
3
3
  # The wrapper keeps track of additional parameters for the Google map
4
4
  # to be used, like size color and label.
5
5
  class Marker
6
- class NoLngMethod < NoMethodError; end
7
- class NoLatMethod < NoMethodError; end
8
- class NoLatKey < ArgumentError; end
9
- class NoLngKey < ArgumentError; end
10
-
11
6
  # These options are the one we build our parameters from
12
7
  DEFAULT_OPTIONS = {
13
8
  :color => 'red',
@@ -15,7 +10,7 @@ module GoogleStaticMapsHelper
15
10
  :label => nil
16
11
  }
17
12
 
18
- attr_accessor :lat, :lng, *DEFAULT_OPTIONS.keys
13
+ attr_accessor :location, *DEFAULT_OPTIONS.keys
19
14
 
20
15
  # Initialize a new Marker
21
16
  #
@@ -55,7 +50,7 @@ module GoogleStaticMapsHelper
55
50
 
56
51
  # Concatination of lat and lng value, used when building the url
57
52
  def location_to_url
58
- [lat, lng].join(',')
53
+ @location.to_url
59
54
  end
60
55
 
61
56
  def label
@@ -67,19 +62,21 @@ module GoogleStaticMapsHelper
67
62
  end
68
63
 
69
64
 
65
+ def method_missing(method, *args)
66
+ return @location.send(method, *args) if @location.respond_to? method
67
+ super
68
+ end
69
+
70
70
  private
71
71
  def extract_location_from_hash!(location_hash)
72
- raise NoLngKey unless location_hash.has_key? :lng
73
- raise NoLatKey unless location_hash.has_key? :lat
74
- @lat = location_hash.delete(:lat)
75
- @lng = location_hash.delete(:lng)
72
+ to_object = {}
73
+ to_object[:lat] = location_hash.delete :lat if location_hash.has_key? :lat
74
+ to_object[:lng] = location_hash.delete :lng if location_hash.has_key? :lng
75
+ @location = Location.new(to_object)
76
76
  end
77
77
 
78
78
  def extract_location_from_object(location)
79
- raise NoLngMethod unless location.respond_to? :lng
80
- raise NoLatMethod unless location.respond_to? :lat
81
- @lat = location.lat
82
- @lng = location.lng
79
+ @location = Location.new(location)
83
80
  end
84
81
 
85
82
  def validate_options(options)
@@ -0,0 +1,67 @@
1
+ module GoogleStaticMapsHelper
2
+ class Path
3
+ include Enumerable
4
+
5
+ OPTIONAL_OPTIONS = [:weight, :color, :fillcolor]
6
+
7
+ attr_accessor :points, *OPTIONAL_OPTIONS
8
+
9
+ def initialize(options = {})
10
+ @points = []
11
+ options.each_pair {|k, v| send("#{k}=", v)}
12
+ end
13
+
14
+
15
+ def url_params
16
+ raise BuildDataMissing, "Need at least 2 points to create a path!" unless can_build?
17
+ out = 'path='
18
+
19
+ path_params = OPTIONAL_OPTIONS.inject([]) do |path_params, attribute|
20
+ value = send(attribute)
21
+ path_params << "#{attribute}:#{URI.escape(value.to_s)}" unless value.nil?
22
+ path_params
23
+ end.join('|')
24
+
25
+ out += "#{path_params}|" unless path_params.empty?
26
+
27
+ out += inject([]) do |point_params, point|
28
+ point_params << point.to_url
29
+ end.join('|')
30
+ end
31
+
32
+
33
+ def points=(array)
34
+ raise ArgumentError unless array.is_a? Array
35
+ @points = []
36
+ array.each {|point| self << point}
37
+ end
38
+
39
+ def each
40
+ @points.each {|p| yield p}
41
+ end
42
+
43
+ def length
44
+ @points.length
45
+ end
46
+
47
+ def empty?
48
+ length == 0
49
+ end
50
+
51
+ def <<(point)
52
+ @points << ensure_point_is_location_object(point)
53
+ @points.uniq!
54
+ self
55
+ end
56
+
57
+ private
58
+ def ensure_point_is_location_object(point)
59
+ return point if point.instance_of? Location
60
+ Location.new(point)
61
+ end
62
+
63
+ def can_build?
64
+ length > 1
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe GoogleStaticMapsHelper::Location do
4
+ before :each do
5
+ @location_hash = {:lat => 10, :lng => 20}
6
+ @location_object = mock(:location, @location_hash)
7
+ end
8
+
9
+ describe "initialize" do
10
+ it "should raise ArgumentError if no arguments are given" do
11
+ lambda {GoogleStaticMapsHelper::Location.new}.should raise_error(ArgumentError)
12
+ end
13
+
14
+ describe "get location as object" do
15
+ [:lat, :lng].each do |location_property|
16
+ it "should extract #{location_property} from first argument if that is object" do
17
+ marker = GoogleStaticMapsHelper::Location.new(@location_object)
18
+ marker.send(location_property).should == @location_object.send(location_property)
19
+ end
20
+ end
21
+
22
+ it "should raise NoLngMethod if object doesn't respond to lng" do
23
+ lambda {GoogleStaticMapsHelper::Location.new(mock(:location, :lat => 10))}.should raise_error(GoogleStaticMapsHelper::Location::NoLngMethod)
24
+ end
25
+
26
+ it "should raise NoLatMethod if object doesn't respond to lat" do
27
+ lambda {GoogleStaticMapsHelper::Location.new(mock(:location, :lng => 20))}.should raise_error(GoogleStaticMapsHelper::Location::NoLatMethod)
28
+ end
29
+ end
30
+
31
+ describe "get location from hash" do
32
+ [:lat, :lng].each do |location_property|
33
+ it "should extract #{location_property} from hash" do
34
+ marker = GoogleStaticMapsHelper::Location.new(@location_hash)
35
+ marker.send(location_property).should == @location_object.send(location_property)
36
+ end
37
+ end
38
+
39
+ it "should raise NoLngKey if hash doesn't have key lng" do
40
+ lambda {GoogleStaticMapsHelper::Location.new(:lat => 10)}.should raise_error(GoogleStaticMapsHelper::Location::NoLngKey)
41
+ end
42
+
43
+ it "should raise NoLatKey if hash doesn't have key lat" do
44
+ lambda {GoogleStaticMapsHelper::Location.new(:lng => 20)}.should raise_error(GoogleStaticMapsHelper::Location::NoLatKey)
45
+ end
46
+ end
47
+ end
48
+
49
+ it "should return to_url with its lat and lng value" do
50
+ GoogleStaticMapsHelper::Location.new(@location_hash).to_url.should == '10,20'
51
+ end
52
+ end
data/spec/map_spec.rb CHANGED
@@ -62,6 +62,12 @@ describe GoogleStaticMapsHelper::Map do
62
62
  @map << @marker << GoogleStaticMapsHelper::Marker.new(:lat => 3, :lng => 5)
63
63
  @map.length.should == 2
64
64
  end
65
+
66
+ it "should return it's markers via markers" do
67
+ @map << @marker
68
+ @map << GoogleStaticMapsHelper::Path.new
69
+ @map.markers.should == [@marker]
70
+ end
65
71
  end
66
72
 
67
73
 
@@ -87,6 +93,29 @@ describe GoogleStaticMapsHelper::Map do
87
93
  end
88
94
  end
89
95
 
96
+ describe "paths" do
97
+ before do
98
+ @path = GoogleStaticMapsHelper::Path.new
99
+ @point = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
100
+ @point2 = GoogleStaticMapsHelper::Location.new(:lat => 3, :lng => 4)
101
+ @path << @point << @point2
102
+
103
+ @map = GoogleStaticMapsHelper::Map.new(@@require_options)
104
+ end
105
+
106
+ it "should be able to push paths on to map" do
107
+ @map << @path
108
+ @map.first.should == @path
109
+ end
110
+
111
+ it "should be able to paths via paths" do
112
+ @marker = GoogleStaticMapsHelper::Marker.new(:lat => 1, :lng => 2)
113
+ @map << @path
114
+ @map << @marker
115
+ @map.paths.should == [@path]
116
+ end
117
+ end
118
+
90
119
 
91
120
  describe "size" do
92
121
  before do
@@ -141,6 +170,50 @@ describe GoogleStaticMapsHelper::Map do
141
170
  end
142
171
  end
143
172
 
173
+ describe "format" do
174
+ before do
175
+ @map = GoogleStaticMapsHelper::Map.new(@@require_options)
176
+ end
177
+
178
+ %w{png png8 png32 gif jpg jpg-basedline}.each do |format|
179
+ it "should be possible to set a format to #{format}" do
180
+ @map.format = format
181
+ @map.format.should == format
182
+ end
183
+ end
184
+
185
+ it "should be able to set format as symbol" do
186
+ @map.format = :jpg
187
+ @map.format.should == 'jpg'
188
+ end
189
+
190
+ it "should raise an error if format is not supported" do
191
+ lambda {@map.format = :not_supported}.should raise_error(GoogleStaticMapsHelper::UnsupportedFormat)
192
+ end
193
+ end
194
+
195
+ describe "map type" do
196
+ before do
197
+ @map = GoogleStaticMapsHelper::Map.new(@@require_options)
198
+ end
199
+
200
+ %w{roadmap satellite terrain hybrid}.each do |type|
201
+ it "should be possible to set map type to #{type}" do
202
+ @map.maptype = type
203
+ @map.maptype.should == type
204
+ end
205
+ end
206
+
207
+ it "should be possible to set map type as a symbol" do
208
+ @map.maptype = :satellite
209
+ @map.maptype.should == 'satellite'
210
+ end
211
+
212
+ it "should raise error if map type is not supported" do
213
+ lambda {@map.maptype = :not_supported}.should raise_error(GoogleStaticMapsHelper::UnsupportedMaptype)
214
+ end
215
+ end
216
+
144
217
  describe "URL" do
145
218
  before :each do
146
219
  @key = 'MY_GOOGLE_KEY'
@@ -193,6 +266,24 @@ describe GoogleStaticMapsHelper::Map do
193
266
  it "should include the sensor" do
194
267
  @map.url.should include("sensor=#{@sensor}")
195
268
  end
269
+
270
+ it "should not include format as default" do
271
+ @map.url.should_not include("format=")
272
+ end
273
+
274
+ it "should include format if it has been set" do
275
+ @map.format = :jpg
276
+ @map.url.should include("format=jpg")
277
+ end
278
+
279
+ it "should not include map type as default" do
280
+ @map.url.should_not include("maptype=")
281
+ end
282
+
283
+ it "should include map type if it has been set" do
284
+ @map.maptype = :satellite
285
+ @map.url.should include("maptype=satellite")
286
+ end
196
287
  end
197
288
 
198
289
  describe "with no markers in map" do
@@ -257,6 +348,40 @@ describe GoogleStaticMapsHelper::Map do
257
348
  end
258
349
  end
259
350
  end
351
+
352
+ describe "paths" do
353
+ before do
354
+ @path = GoogleStaticMapsHelper::Path.new
355
+ @point = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
356
+ @point2 = GoogleStaticMapsHelper::Location.new(:lat => 3, :lng => 4)
357
+ @path << @point << @point2
358
+ end
359
+
360
+ it "should not include path in url if no paths are represented in the map" do
361
+ @map.center = '1,2'
362
+ @map.zoom = 11
363
+ @map.url.should_not include("path=")
364
+ end
365
+
366
+ it "should include path in url if paths are represented in the map" do
367
+ @map << @path
368
+ @map.url.should include("path=")
369
+ end
370
+
371
+ [
372
+ ['key', 'MY_GOOGLE_KEY'],
373
+ ['sensor', 'false'],
374
+ ['size', '400x600'],
375
+ ['path', 'weight:5|1,2|3,4'],
376
+ ].each do |pair|
377
+ key, value = pair
378
+ it "should have key: #{key} and value: #{value}" do
379
+ @path.weight = 5
380
+ @map << @path
381
+ @map.url.should include("#{key}=#{value}")
382
+ end
383
+ end
384
+ end
260
385
  end
261
386
 
262
387
  it "should provide a helper method named marker which will create a new marker and add it to the map" do
data/spec/marker_spec.rb CHANGED
@@ -20,11 +20,11 @@ describe GoogleStaticMapsHelper::Marker do
20
20
  end
21
21
 
22
22
  it "should raise NoLngMethod if object doesn't respond to lng" do
23
- lambda {GoogleStaticMapsHelper::Marker.new(mock(:location, :lat => 10))}.should raise_error(GoogleStaticMapsHelper::Marker::NoLngMethod)
23
+ lambda {GoogleStaticMapsHelper::Marker.new(mock(:location, :lat => 10))}.should raise_error(GoogleStaticMapsHelper::Location::NoLngMethod)
24
24
  end
25
25
 
26
26
  it "should raise NoLatMethod if object doesn't respond to lat" do
27
- lambda {GoogleStaticMapsHelper::Marker.new(mock(:location, :lng => 20))}.should raise_error(GoogleStaticMapsHelper::Marker::NoLatMethod)
27
+ lambda {GoogleStaticMapsHelper::Marker.new(mock(:location, :lng => 20))}.should raise_error(GoogleStaticMapsHelper::Location::NoLatMethod)
28
28
  end
29
29
  end
30
30
 
@@ -37,11 +37,11 @@ describe GoogleStaticMapsHelper::Marker do
37
37
  end
38
38
 
39
39
  it "should raise NoLngKey if hash doesn't have key lng" do
40
- lambda {GoogleStaticMapsHelper::Marker.new(:lat => 10)}.should raise_error(GoogleStaticMapsHelper::Marker::NoLngKey)
40
+ lambda {GoogleStaticMapsHelper::Marker.new(:lat => 10)}.should raise_error(GoogleStaticMapsHelper::Location::NoLngKey)
41
41
  end
42
42
 
43
43
  it "should raise NoLatKey if hash doesn't have key lat" do
44
- lambda {GoogleStaticMapsHelper::Marker.new(:lng => 20)}.should raise_error(GoogleStaticMapsHelper::Marker::NoLatKey)
44
+ lambda {GoogleStaticMapsHelper::Marker.new(:lng => 20)}.should raise_error(GoogleStaticMapsHelper::Location::NoLatKey)
45
45
  end
46
46
  end
47
47
 
data/spec/path_spec.rb ADDED
@@ -0,0 +1,134 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe GoogleStaticMapsHelper::Path do
4
+ describe "initialize" do
5
+ @@options = {
6
+ :weight => 5,
7
+ :color => "0x0000ff",
8
+ :fillcolor => "0x110000ff"
9
+ }
10
+
11
+ @@options.each_key do |attribute|
12
+ it "should be able to set and retreive #{attribute} via initializer" do
13
+ GoogleStaticMapsHelper::Path.new(@@options).send(attribute).should == @@options.fetch(attribute)
14
+ end
15
+
16
+ it "should be able to set and retreive #{attribute} via accessor method" do
17
+ path = GoogleStaticMapsHelper::Path.new
18
+ path.send("#{attribute}=", @@options.fetch(attribute))
19
+ path.send(attribute).should == @@options.fetch(attribute)
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "points" do
25
+ before do
26
+ @path = GoogleStaticMapsHelper::Path.new
27
+ @point = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
28
+ @point2 = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
29
+ end
30
+
31
+ it "should be an empty array of points after initialize" do
32
+ @path.points.should == []
33
+ end
34
+
35
+ it "should be able to push points on to a path" do
36
+ @path << @point
37
+ @path.points.length.should == 1
38
+ @path.points.first.should == @point
39
+ end
40
+
41
+ it "should not be able to push the same point twice" do
42
+ @path << @point
43
+ @path << @point
44
+ @path.points.should == [@point]
45
+ end
46
+
47
+ it "should be able to chain push operator" do
48
+ @path << @point << @point2
49
+ @path.points.should == [@point, @point2]
50
+ end
51
+
52
+ it "should respond do each" do
53
+ @path.should respond_to(:each)
54
+ end
55
+
56
+ it "should be able to tell it's length" do
57
+ @path << @point << @point2
58
+ @path.length.should == 2
59
+ end
60
+
61
+ it "should be able to answer empty?" do
62
+ @path.should be_empty
63
+ end
64
+
65
+ it "should wrap a hash which contains lat and lng into a Location object when pushed" do
66
+ @path << {:lat => 1, :lng => 2}
67
+ @path.first.should be_an_instance_of(GoogleStaticMapsHelper::Location)
68
+ end
69
+
70
+ it "should fetch lat and lng values from any object which responds to it" do
71
+ @path << mock(:point, :lat => 1, :lng => 2)
72
+ @path.first.should be_an_instance_of(GoogleStaticMapsHelper::Location)
73
+ end
74
+
75
+ it "should raise an error if points setter doesn't receive an array" do
76
+ lambda {@path.points = nil}.should raise_error(ArgumentError)
77
+ end
78
+
79
+ it "should make sure points-setter ensures that hash-values are wraped into a Location object" do
80
+ @path.points = []
81
+ end
82
+ end
83
+
84
+
85
+ describe "url_params" do
86
+ before do
87
+ @path = GoogleStaticMapsHelper::Path.new
88
+ @point = GoogleStaticMapsHelper::Location.new(:lat => 1, :lng => 2)
89
+ @point2 = GoogleStaticMapsHelper::Location.new(:lat => 3, :lng => 4)
90
+ @path << @point << @point2
91
+ end
92
+
93
+ it "should respond to url_params" do
94
+ @path.should respond_to(:url_params)
95
+ end
96
+
97
+ it "should raise an error if a path doesn't include any points" do
98
+ @path.points = []
99
+ lambda {@path.url_params}.should raise_error(GoogleStaticMapsHelper::BuildDataMissing)
100
+ end
101
+
102
+ it "should not raise an error if path have points" do
103
+ lambda {@path.url_params}.should_not raise_error(GoogleStaticMapsHelper::BuildDataMissing)
104
+ end
105
+
106
+ it "should begin with path=" do
107
+ @path.url_params.should match(/^path=/)
108
+ end
109
+
110
+ it "should include points' locations" do
111
+ @path.url_params.should include('1,2')
112
+ end
113
+
114
+ @@options.each do |attribute, value|
115
+ it "should not include #{attribute} as default in url" do
116
+ @path.url_params.should_not include("#{attribute}=")
117
+ end
118
+
119
+ it "should include #{attribute} when set on path" do
120
+ @path.send("#{attribute}=", value)
121
+ @path.url_params.should include("#{attribute}:#{value}")
122
+ end
123
+ end
124
+
125
+ it "should concat path options and point locations correctly together" do
126
+ @path.weight = 3
127
+ @path.url_params.should == 'path=weight:3|1,2|3,4'
128
+ end
129
+
130
+ it "should concat point locations without any path options" do
131
+ @path.url_params.should == 'path=1,2|3,4'
132
+ end
133
+ end
134
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_static_maps_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Thorbj\xC3\xB8rn Hermansen"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-25 00:00:00 +02:00
12
+ date: 2009-10-28 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -41,11 +41,15 @@ files:
41
41
  - changelog.txt
42
42
  - google_static_maps_helper.gemspec
43
43
  - lib/google_static_maps_helper.rb
44
+ - lib/google_static_maps_helper/location.rb
44
45
  - lib/google_static_maps_helper/map.rb
45
46
  - lib/google_static_maps_helper/marker.rb
47
+ - lib/google_static_maps_helper/path.rb
46
48
  - spec/google_static_maps_helper_spec.rb
49
+ - spec/location_spec.rb
47
50
  - spec/map_spec.rb
48
51
  - spec/marker_spec.rb
52
+ - spec/path_spec.rb
49
53
  - spec/spec_helper.rb
50
54
  has_rdoc: true
51
55
  homepage: http://github.com/thhermansen/google_static_maps_helper
@@ -79,4 +83,6 @@ test_files:
79
83
  - spec/spec_helper.rb
80
84
  - spec/marker_spec.rb
81
85
  - spec/google_static_maps_helper_spec.rb
86
+ - spec/location_spec.rb
82
87
  - spec/map_spec.rb
88
+ - spec/path_spec.rb