google_static_maps_helper 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.swp
7
+ *.vim
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Thorbjørn Hermansen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = Google Static Maps Helper
2
+
3
+ This gem provides a simple interface to the Google Static Maps V2 API (http://code.google.com/apis/maps/documentation/staticmaps/)
4
+
5
+ == Marker
6
+ A marker object is, not surprisingly, representing a marker in the Google static map. It is pushed into the Map object and is used
7
+ to generate the URL for the static map. Markers which are of the same "type" (same color, label and size) are grouped together in the generated URL
8
+ so that we obey the schema of URL which Google has defined as "markers=markerStyles|markerLocation1|markerLocation2|... etc."
9
+
10
+ === How to build a Marker?
11
+ The easiest way to build a marker is by simply sending in an object which responds to lng and lat, or
12
+ by sending lng and lat in as a hash.
13
+ marker = GoogleStaticMapsHelper::Marker.new(location)
14
+ marker = GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2)
15
+
16
+ Google's static maps supports some options on markers. You can change the color, the label and the size.
17
+ (http://code.google.com/apis/maps/documentation/staticmaps/#MarkerStyles). You send in options as a second
18
+ parameter to the new method if you gave a location object, or include it in the hash where lng and lat is.
19
+ marker = GoogleStaticMapsHelper::Marker.new(location, :color => 'blue')
20
+ marker = GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2, :color => 'blue')
21
+
22
+
23
+ == Map
24
+ A Map holds many markers, pluss some additional options like the size of the static image, Google key, what zoom level and center point it should be fixed to
25
+ etc. If no zoom or center point is give it will calculate the map view based on the markers. You can leave markers out, but then you have to supply
26
+ zoom and center. More info can be found here: http://code.google.com/apis/maps/documentation/staticmaps/#URL_Parameters
27
+
28
+ === How to build a map?
29
+ When building a map object you have to supply key, sensor and size. Other options are optional.
30
+ map = GoogleStaticMapsHelper::Map.new(:key => YOUR_GOOGLE_KEY, :sensor => false, :size => '200x300')
31
+
32
+
33
+ == Generate static map URL
34
+ With the map object made, we are ready to add some markers to it:
35
+ map << marker
36
+ map << another_marker
37
+
38
+ We can now ask the map for it's URL to where we'll get the requested map from. This URL can be used as src attribute on an image tag.
39
+ map.url
40
+
41
+ Another thing you might want to do is to override the center point and zoom level. This is done during map construction
42
+ map = GoogleStaticMapsHelper::Map.new(:key => YOUR_GOOGLE_KEY,
43
+ :sensor => false,
44
+ :size => '200x300',
45
+ :center => '1,2',
46
+ :zoom => 4)
47
+
48
+ == Paths
49
+ Paths are not yet supported.
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2009 Thorbjørn Hermansen. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "google_static_maps_helper"
8
+ gem.summary = %Q{This gem provides a simple interface to the Google Static Maps V2 API}
9
+ gem.description = %Q{This gem provides a simple interface to the Google Static Maps V2 API.}
10
+ gem.email = "thhermansen@gmail.com"
11
+ gem.homepage = "http://github.com/thhermansen/google_static_maps_helper"
12
+ gem.authors = ["Thorbjørn Hermansen"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION')
39
+ version = File.read('VERSION')
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "google_static_maps_helper #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google_static_maps_helper}
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Thorbj\303\270rn Hermansen"]
12
+ s.date = %q{2009-10-13}
13
+ s.description = %q{This gem provides a simple interface to the Google Static Maps V2 API.}
14
+ s.email = %q{thhermansen@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "google_static_maps_helper.gemspec",
27
+ "lib/google_static_maps_helper.rb",
28
+ "lib/google_static_maps_helper/map.rb",
29
+ "lib/google_static_maps_helper/marker.rb",
30
+ "spec/map_spec.rb",
31
+ "spec/marker_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/thhermansen/google_static_maps_helper}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{This gem provides a simple interface to the Google Static Maps V2 API}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/marker_spec.rb",
42
+ "spec/map_spec.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 0"])
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ require 'uri'
2
+ require File.dirname(__FILE__) + '/google_static_maps_helper/map'
3
+ require File.dirname(__FILE__) + '/google_static_maps_helper/marker'
4
+
5
+ module GoogleStaticMapsHelper
6
+ API_URL = 'http://maps.google.com/maps/api/staticmap'
7
+
8
+ class OptionMissing < ArgumentError; end # Raised when required options is not sent in during construction
9
+ class OptionNotExist < ArgumentError; end # Raised when incoming options include keys which is invalid
10
+ class BuildDataMissing < Exception; end # Raised when incoming options include keys which is invalid
11
+ end
@@ -0,0 +1,87 @@
1
+ module GoogleStaticMapsHelper
2
+ # Represents the map we are generating
3
+ # It holds markers and iterates over them to build the URL
4
+ # to be used in an image tag.
5
+ class Map
6
+ include Enumerable
7
+
8
+ REQUIRED_OPTIONS = [:key, :size, :sensor]
9
+ OPTIONAL_OPTIONS = [:center, :zoom, :format, :maptype, :mobile, :language]
10
+
11
+ attr_accessor *(REQUIRED_OPTIONS + OPTIONAL_OPTIONS)
12
+
13
+ # Initialize a new Map object
14
+ #
15
+ # Takes a hash of options where :key, :size and :sensor are required.
16
+ # Other options are center, zoom, format, maptype, mobile and language
17
+ def initialize(options)
18
+ validate_required_options(options)
19
+ validate_options(options)
20
+
21
+ options.each_pair { |k, v| send("#{k}=", v) }
22
+ @markers = []
23
+ end
24
+
25
+ def url
26
+ raise BuildDataMissing, "We have to have markers or center and zoom set when url is called!" unless can_build?
27
+
28
+ out = "#{API_URL}?"
29
+
30
+ params = []
31
+ (REQUIRED_OPTIONS + OPTIONAL_OPTIONS).each do |key|
32
+ value = send(key)
33
+ params << "#{key}=#{URI.escape(value.to_s)}" if value
34
+ end
35
+ out += params.join('&')
36
+
37
+ params = []
38
+ grouped_markers.each_pair do |marker_options_as_url_params, markers|
39
+ markers_locations = markers.map { |m| m.location_to_url }.join('|')
40
+ params << "markers=#{marker_options_as_url_params}|#{markers_locations}"
41
+ end
42
+ out += "&#{params.join('&')}" unless params.empty?
43
+
44
+ out
45
+ end
46
+
47
+ def grouped_markers
48
+ inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker|
49
+ groups[marker.options_to_url_params] << marker
50
+ groups
51
+ end
52
+ end
53
+
54
+ def <<(marker)
55
+ @markers << marker
56
+ @markers.uniq!
57
+ end
58
+
59
+ def each
60
+ @markers.each {|m| yield(m)}
61
+ end
62
+
63
+ def empty?
64
+ @markers.empty?
65
+ end
66
+
67
+ def length
68
+ @markers.length
69
+ end
70
+
71
+
72
+ private
73
+ def can_build?
74
+ !@markers.empty? || (center && zoom)
75
+ end
76
+
77
+ def validate_required_options(options)
78
+ missing_options = REQUIRED_OPTIONS - options.keys
79
+ raise OptionMissing, "The following required options are missing: #{missing_options.join(', ')}" unless missing_options.empty?
80
+ end
81
+
82
+ def validate_options(options)
83
+ invalid_options = options.keys - REQUIRED_OPTIONS - OPTIONAL_OPTIONS
84
+ raise OptionNotExist, "The following options does not exist: #{invalid_options.join(', ')}" unless invalid_options.empty?
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,90 @@
1
+ module GoogleStaticMapsHelper
2
+ # Simple wrapper around an object which should respond to lat and lng.
3
+ # The wrapper keeps track of additional parameters for the Google map
4
+ # to be used, like size color and label.
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
+ # These options are the one we build our parameters from
12
+ DEFAULT_OPTIONS = {
13
+ :color => 'red',
14
+ :size => 'mid',
15
+ :label => nil
16
+ }
17
+
18
+ attr_accessor :lat, :lng, *DEFAULT_OPTIONS.keys
19
+
20
+ # Initialize a new Marker
21
+ #
22
+ # Can wither take an object which responds to lng and lat
23
+ # GoogleStaticMapsHelper::Marker.new(location)
24
+ #
25
+ # Or it can take a has which includes lng and lat
26
+ # GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2)
27
+ #
28
+ # You can also send in options like color, size and label in the hash,
29
+ # or as a secnond parameter if the first was an object.
30
+ def initialize(*args)
31
+ raise ArgumentError, "Must have one or two arguments." if args.length == 0
32
+
33
+ if args.first.is_a? Hash
34
+ extract_location_from_hash!(args.first)
35
+ else
36
+ extract_location_from_object(args.shift)
37
+ end
38
+
39
+ options = DEFAULT_OPTIONS.merge(args.shift || {})
40
+ validate_options(options)
41
+ options.each_pair { |k, v| send("#{k}=", v) }
42
+ end
43
+
44
+ # Returns a string wich is what Google Static map is using to
45
+ # set the style on the marker. This ill include color, size and label
46
+ def options_to_url_params
47
+ params = DEFAULT_OPTIONS.keys.inject([]) do |params, attr|
48
+ value = send(attr)
49
+ params << "#{attr}:#{value}" if value
50
+ params
51
+ end
52
+
53
+ params.join('|')
54
+ end
55
+
56
+ # Concatination of lat and lng value, used when building the url
57
+ def location_to_url
58
+ [lat, lng].join(',')
59
+ end
60
+
61
+ def label
62
+ @label.to_s.upcase if @label
63
+ end
64
+
65
+ def color
66
+ @color.downcase if @color
67
+ end
68
+
69
+
70
+ private
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)
76
+ end
77
+
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
83
+ end
84
+
85
+ def validate_options(options)
86
+ invalid_options = options.keys - DEFAULT_OPTIONS.keys
87
+ raise OptionNotExist, "The following options does not exist: #{invalid_options.join(', ')}" unless invalid_options.empty?
88
+ end
89
+ end
90
+ end
data/spec/map_spec.rb ADDED
@@ -0,0 +1,191 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe GoogleStaticMapsHelper::Map do
4
+ @@require_options = {
5
+ :size => '800x600',
6
+ :key => 'MY_GOOGLE_KEY',
7
+ :sensor => false
8
+ }
9
+
10
+
11
+ describe "initialize" do
12
+ @@require_options.each_key do |key|
13
+ it "should raise OptionMissing if #{key} is not given" do
14
+ option_with_missing_option = @@require_options.dup
15
+ option_with_missing_option.delete(key)
16
+ lambda {GoogleStaticMapsHelper::Map.new(option_with_missing_option)}.should raise_error(GoogleStaticMapsHelper::OptionMissing)
17
+ end
18
+ end
19
+
20
+ it "should raise OptionNotExist if incomming option doesn't exists" do
21
+ lambda {GoogleStaticMapsHelper::Map.new(@@require_options.merge(:invalid_option => 'error?'))}.should raise_error(GoogleStaticMapsHelper::OptionNotExist)
22
+ end
23
+
24
+ it "should be able to read initialized key option from object" do
25
+ GoogleStaticMapsHelper::Map.new(@@require_options).key.should == @@require_options[:key]
26
+ end
27
+ end
28
+
29
+ describe "markers" do
30
+ before :each do
31
+ @marker = GoogleStaticMapsHelper::Marker.new(:lat => 1, :lng => 2)
32
+ @map = GoogleStaticMapsHelper::Map.new(@@require_options)
33
+ end
34
+
35
+ it "should be empty as default" do
36
+ @map.should be_empty
37
+ end
38
+
39
+ it "should be able to push markers onto map" do
40
+ @map << @marker
41
+ end
42
+
43
+ it "should not be possible to push the same marker twice" do
44
+ @map << @marker
45
+ @map << @marker
46
+ @map.length.should == 1
47
+ end
48
+ end
49
+
50
+
51
+ describe "Grouped markers" do
52
+ before :each do
53
+ @marker1 = GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2)
54
+ @marker11 = GoogleStaticMapsHelper::Marker.new(:lng => 3, :lat => 4)
55
+
56
+ @marker2 = GoogleStaticMapsHelper::Marker.new(:lng => 5, :lat => 6, :color => 'green')
57
+ @marker22 = GoogleStaticMapsHelper::Marker.new(:lng => 7, :lat => 8, :color => 'green')
58
+ @map = GoogleStaticMapsHelper::Map.new(:key => @key, :size => @size, :sensor => @sensor)
59
+ end
60
+
61
+ it "should return options_to_url_params as key, array with markers as value" do
62
+ @map << @marker1
63
+ @map << @marker11
64
+ @map << @marker2
65
+ @map << @marker22
66
+ @map.grouped_markers.should == {
67
+ @marker1.options_to_url_params => [@marker1, @marker11],
68
+ @marker2.options_to_url_params => [@marker2, @marker22]
69
+ }
70
+ end
71
+ end
72
+
73
+
74
+
75
+ describe "URL" do
76
+ before :each do
77
+ @key = 'MY_GOOGLE_KEY'
78
+ @size = '400x600'
79
+ @sensor = true
80
+ @map = GoogleStaticMapsHelper::Map.new(:key => @key, :size => @size, :sensor => @sensor)
81
+
82
+ @marker1 = GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2)
83
+ @marker11 = GoogleStaticMapsHelper::Marker.new(:lng => 3, :lat => 4)
84
+
85
+ @marker2 = GoogleStaticMapsHelper::Marker.new(:lng => 5, :lat => 6, :color => 'green')
86
+ @marker22 = GoogleStaticMapsHelper::Marker.new(:lng => 7, :lat => 8, :color => 'green')
87
+ end
88
+
89
+ describe "valid state to run URL" do
90
+ it "should raise exception if called with no markers nor center and zoom" do
91
+ lambda{@map.url}.should raise_error(GoogleStaticMapsHelper::BuildDataMissing)
92
+ end
93
+
94
+ it "should not raise exception if markers are in map" do
95
+ @map << @marker1
96
+ lambda{@map.url}.should_not raise_error(GoogleStaticMapsHelper::BuildDataMissing)
97
+ end
98
+
99
+ it "should not raise exception if center and zoom is set" do
100
+ @map.zoom = 1
101
+ @map.center = '1,1'
102
+ lambda{@map.url}.should_not raise_error(GoogleStaticMapsHelper::BuildDataMissing)
103
+ end
104
+ end
105
+
106
+ describe "required parameters" do
107
+ before :each do
108
+ @map.zoom = 1
109
+ @map.center = '1,1'
110
+ end
111
+
112
+ it "should start with the URL to the API" do
113
+ @map.url.should include(GoogleStaticMapsHelper::API_URL)
114
+ end
115
+
116
+ it "should include the key" do
117
+ @map.url.should include("key=#{@key}")
118
+ end
119
+
120
+ it "should include the size" do
121
+ @map.url.should include("size=#{@size}")
122
+ end
123
+
124
+ it "should include the sensor" do
125
+ @map.url.should include("sensor=#{@sensor}")
126
+ end
127
+ end
128
+
129
+ describe "with no markers in map" do
130
+ before :each do
131
+ @map.zoom = 1
132
+ @map.center = '1,1'
133
+ end
134
+
135
+ it "should contain center=2,3" do
136
+ @map.url.should include("center=1,1")
137
+ end
138
+
139
+ it "should contain zoom=1" do
140
+ @map.url.should include("zoom=1")
141
+ end
142
+
143
+ it "should not include markers param" do
144
+ @map.url.should_not include("markers=")
145
+ end
146
+ end
147
+
148
+
149
+ describe "with markers, no one grouped" do
150
+ before :each do
151
+ @map << @marker1
152
+ @map << @marker2
153
+ end
154
+
155
+ [
156
+ ['key', 'MY_GOOGLE_KEY'],
157
+ ['sensor', 'true'],
158
+ ['size', '400x600'],
159
+ ['markers', 'size:mid|color:green|6,5'],
160
+ ['markers', 'size:mid|color:red|2,1']
161
+ ].each do |pair|
162
+ key, value = pair
163
+ it "should have key: #{key} and value: #{value}" do
164
+ @map.url.should include("#{key}=#{value}")
165
+ end
166
+ end
167
+ end
168
+
169
+ describe "with markers grouped together" do
170
+ before :each do
171
+ @map << @marker1
172
+ @map << @marker11
173
+ @map << @marker2
174
+ @map << @marker22
175
+ end
176
+
177
+ [
178
+ ['key', 'MY_GOOGLE_KEY'],
179
+ ['sensor', 'true'],
180
+ ['size', '400x600'],
181
+ ['markers', 'size:mid|color:green|6,5|8,7'],
182
+ ['markers', 'size:mid|color:red|2,1|4,3']
183
+ ].each do |pair|
184
+ key, value = pair
185
+ it "should have key: #{key} and value: #{value}" do
186
+ @map.url.should include("#{key}=#{value}")
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,129 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe GoogleStaticMapsHelper::Marker 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::Marker.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::Marker.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::Marker.new(mock(:location, :lat => 10))}.should raise_error(GoogleStaticMapsHelper::Marker::NoLngMethod)
24
+ end
25
+
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)
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::Marker.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::Marker.new(:lat => 10)}.should raise_error(GoogleStaticMapsHelper::Marker::NoLngKey)
41
+ end
42
+
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)
45
+ end
46
+ end
47
+
48
+
49
+ describe "options" do
50
+ describe "defaults" do
51
+ it "should have a predefined color which location should use" do
52
+ marker = GoogleStaticMapsHelper::Marker.new(@location_object)
53
+ marker.color.should == 'red'
54
+ end
55
+
56
+ it "should have a predefined size" do
57
+ marker = GoogleStaticMapsHelper::Marker.new(@location_object)
58
+ marker.size.should == 'mid'
59
+ end
60
+
61
+ it "should have a predefined label which should be nil" do
62
+ marker = GoogleStaticMapsHelper::Marker.new(@location_object)
63
+ marker.label.should be_nil
64
+ end
65
+ end
66
+
67
+ describe "override options as second parameters, location given as object as first param" do
68
+ {:color => 'blue', :size => 'small', :label => 'A'}.each_pair do |key, value|
69
+ it "should be possible to override #{key} to #{value}" do
70
+ marker = GoogleStaticMapsHelper::Marker.new(@location_object, {key => value})
71
+ marker.send(key).should == value
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "override options as first parameter, location mixed into the same hash" do
77
+ {:color => 'blue', :size => 'small', :label => 'A'}.each_pair do |key, value|
78
+ it "should be possible to override #{key} to #{value}" do
79
+ marker = GoogleStaticMapsHelper::Marker.new(@location_hash.merge({key => value}))
80
+ marker.send(key).should == value
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ it "should raise OptionNotExist if incomming option doesn't exists" do
87
+ lambda {GoogleStaticMapsHelper::Marker.new(:lng => 1, :lat => 2, :invalid_option => 'error?')}.should raise_error(GoogleStaticMapsHelper::OptionNotExist)
88
+ end
89
+ end
90
+
91
+
92
+
93
+ it "should upcase the label" do
94
+ GoogleStaticMapsHelper::Marker.new(@location_hash.merge(:label => 'a')).label.should == 'A'
95
+ end
96
+
97
+ it "should downcase the color" do
98
+ GoogleStaticMapsHelper::Marker.new(@location_hash.merge(:color => 'Green')).color.should == 'green'
99
+ end
100
+
101
+
102
+ describe "generating url parameters" do
103
+ before :each do
104
+ @options = {:lat => 1, :lng => 2, :color => 'Green', :label => :a, :size => 'small'}
105
+ @marker = GoogleStaticMapsHelper::Marker.new(@options)
106
+ end
107
+
108
+ it "should contain color param" do
109
+ @marker.options_to_url_params.should include('color:green')
110
+ end
111
+
112
+ it "should contain label param" do
113
+ @marker.options_to_url_params.should include('label:A')
114
+ end
115
+
116
+ it "should contain size param" do
117
+ @marker.options_to_url_params.should include('size:small')
118
+ end
119
+
120
+ it "should not contain label param if it is nil" do
121
+ marker = GoogleStaticMapsHelper::Marker.new(:lat => 1, :lng => 1)
122
+ marker.options_to_url_params.should_not include('label')
123
+ end
124
+
125
+ it "should build location_to_url" do
126
+ @marker.location_to_url.should == '1,2'
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'google_static_maps_helper'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_static_maps_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Thorbj\xC3\xB8rn Hermansen"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-13 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: This gem provides a simple interface to the Google Static Maps V2 API.
26
+ email: thhermansen@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - google_static_maps_helper.gemspec
42
+ - lib/google_static_maps_helper.rb
43
+ - lib/google_static_maps_helper/map.rb
44
+ - lib/google_static_maps_helper/marker.rb
45
+ - spec/map_spec.rb
46
+ - spec/marker_spec.rb
47
+ - spec/spec_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/thhermansen/google_static_maps_helper
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: This gem provides a simple interface to the Google Static Maps V2 API
76
+ test_files:
77
+ - spec/spec_helper.rb
78
+ - spec/marker_spec.rb
79
+ - spec/map_spec.rb