static-gmaps 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,5 +1,11 @@
1
+ === 0.0.2 / 2008-02-25
2
+
3
+ * 1 Major Enhancement:
4
+ * Now supports Markers!
5
+ * 1 Minor Enhancement:
6
+ * Removed StaticGmaps::Map.to_blob
7
+
1
8
  === 0.0.1 / 2008-02-25
2
9
 
3
10
  * 1 major enhancement
4
11
  * Birthday!
5
-
data/Manifest.txt CHANGED
@@ -5,4 +5,3 @@ README.txt
5
5
  Rakefile
6
6
  lib/static_gmaps.rb
7
7
  spec/static_gmaps_spec.rb
8
- spec/test_image.gif
data/README.txt CHANGED
@@ -8,7 +8,7 @@ Provides an interface to the Google Static Maps API.
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
11
- * See SYNOPSIS.
11
+ * Provides an interface to the Google Static Maps API.
12
12
 
13
13
  == SYNOPSIS:
14
14
 
@@ -16,12 +16,15 @@ Provides an interface to the Google Static Maps API.
16
16
  GOOGLE_MAPS_API_KEY = ''
17
17
 
18
18
  map = StaticGmaps::Map.new :center => [ 40.714728, -73.998672 ],
19
- :zoom => 12,
19
+ :zoom => 5,
20
20
  :size => [ 500, 400 ],
21
21
  :map_type => :roadmap,
22
22
  :key => GOOGLE_MAPS_API_KEY
23
- map.url => 'http://maps.google.com/staticmap?center=40.714728,-73.998672&zoom=12&size=500x400&map_type=roadmap&key=KEY
24
- map.to_blob => GIF Data
23
+ map.markers << StaticGmaps::Marker.new(:latitude => 40,
24
+ :longitude => -73,
25
+ :color => :blue,
26
+ :alpha_character => :b)
27
+ map.url => 'http://maps.google.com/staticmap?center=40.714728,-73.998672&key=GOOGLE_MAPS_API_KEY&map_type=roadmap&markers=40,-73,blueb&size=500x400&zoom=5'
25
28
 
26
29
  == REQUIREMENTS:
27
30
 
data/Rakefile CHANGED
@@ -39,6 +39,7 @@ Hoe.new('static-gmaps', StaticGmaps::VERSION) do |p|
39
39
  p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
40
40
  p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
41
41
  p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
42
+ p.remote_rdoc_dir = ''
42
43
  end
43
44
 
44
45
  desc "Run all examples"
data/lib/static_gmaps.rb CHANGED
@@ -23,7 +23,7 @@
23
23
  require 'net/http'
24
24
 
25
25
  module StaticGmaps
26
- VERSION = '0.0.1'
26
+ VERSION = '0.0.2'
27
27
 
28
28
  class Map
29
29
  DEFAULT_CENTER = [ 0, 0 ]
@@ -31,12 +31,14 @@ module StaticGmaps
31
31
  DEFAULT_SIZE = [ 500, 400 ]
32
32
  DEFAULT_MAP_TYPE = :roadmap
33
33
  DEFAULT_KEY = 'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA'
34
+ DEFAULT_MARKERS = [ ]
34
35
 
35
36
  attr_accessor :center,
36
37
  :zoom,
37
38
  :size,
38
39
  :map_type,
39
- :key
40
+ :key,
41
+ :markers
40
42
 
41
43
  def initialize(options = {})
42
44
  self.center = options[:center] || DEFAULT_CENTER
@@ -44,6 +46,7 @@ module StaticGmaps
44
46
  self.size = options[:size] || DEFAULT_SIZE
45
47
  self.map_type = options[:map_type] || DEFAULT_MAP_TYPE
46
48
  self.key = options[:key] || DEFAULT_KEY
49
+ self.markers = options[:markers] || DEFAULT_MARKERS
47
50
  end
48
51
 
49
52
  def width
@@ -54,8 +57,33 @@ module StaticGmaps
54
57
  size[1]
55
58
  end
56
59
 
60
+ # http://code.google.com/apis/maps/documentation/staticmaps/index.html#URL_Parameters
57
61
  def url
58
- return "http://maps.google.com/staticmap?center=#{center[0]},#{center[1]}&zoom=#{zoom}&size=#{size[0]}x#{size[1]}&map_type=#{map_type}&key=#{key}"
62
+ raise MissingArgument.new("Size must be set before a url can be generated for Map.") if !size || !size[0] || !size[1]
63
+ raise MissingArgument.new("Key must be set before a url can be generated for Map.") if !key
64
+ if !(markers && markers.size > 1)
65
+ raise MissingArgument.new("Center must be set before a url can be generated for Map (or multiple markers can be specified).") if !center
66
+ raise MissingArgument.new("Zoom must be set before a url can be generated for Map (or multiple markers can be specified).") if !zoom
67
+ end
68
+ parameters = {}
69
+ parameters[:size] = "#{size[0]}x#{size[1]}"
70
+ parameters[:key] = "#{key}"
71
+ parameters[:map_type] = "#{map_type}" if map_type
72
+ parameters[:center] = "#{center[0]},#{center[1]}" if center
73
+ parameters[:zoom] = "#{zoom}" if zoom
74
+ parameters[:markers] = "#{markers_url_fragment}" if markers_url_fragment
75
+ parameters = parameters.to_a.sort { |a, b| a[0].to_s <=> b[0].to_s }
76
+ parameters = parameters.collect { |parameter| "#{parameter[0]}=#{parameter[1]}" }
77
+ parameters = parameters.join '&'
78
+ return "http://maps.google.com/staticmap?#{parameters}"
79
+ end
80
+
81
+ def markers_url_fragment
82
+ if markers && markers.any?
83
+ return markers.collect{|marker| marker.url_fragment }.join('|')
84
+ else
85
+ return nil
86
+ end
59
87
  end
60
88
 
61
89
  def to_blob
@@ -74,4 +102,55 @@ module StaticGmaps
74
102
  end
75
103
  end
76
104
  end
105
+
106
+ # http://code.google.com/apis/maps/documentation/staticmaps/index.html#Markers
107
+ class Marker
108
+ DEFAULT_LATITUDE = nil
109
+ DEFAULT_LONGITUDE = nil
110
+ DEFAULT_COLOR = nil
111
+ DEFAULT_ALPHA_CHARACTER = nil
112
+ VALID_COLORS = [ :red, :green, :blue ]
113
+ VALID_ALPHA_CHARACTERS = [ :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z ]
114
+
115
+ attr_accessor :latitude,
116
+ :longitude,
117
+ :color,
118
+ :alpha_character
119
+
120
+ def initialize(options = {})
121
+ self.latitude = options[:latitude] || DEFAULT_LATITUDE
122
+ self.longitude = options[:longitude] || DEFAULT_LONGITUDE
123
+ self.color = options[:color] || DEFAULT_COLOR
124
+ self.alpha_character = options[:alpha_character] || DEFAULT_ALPHA_CHARACTER
125
+ end
126
+
127
+ def color=(value)
128
+ if value
129
+ value = value.to_s.downcase.to_sym
130
+ if !VALID_COLORS.include?(value)
131
+ raise ArgumentError.new("#{value} is not a supported color. Supported colors are #{VALID_COLORS.join(', ')}.")
132
+ end
133
+ end
134
+ @color = value
135
+ end
136
+
137
+ def alpha_character=(value)
138
+ if value
139
+ value = value.to_s.downcase.to_sym
140
+ if !VALID_ALPHA_CHARACTERS.include?(value)
141
+ raise ArgumentError.new("#{value} is not a supported alpha_character. Supported colors are #{VALID_ALPHA_CHARACTERS.join(', ')}.")
142
+ end
143
+ end
144
+ @alpha_character = value
145
+ end
146
+
147
+ def url_fragment
148
+ raise MissingArgument.new("Latitude must be set before a url_fragment can be generated for Marker.") if !latitude
149
+ raise MissingArgument.new("Longitude must be set before a url_fragment can be generated for Marker.") if !longitude
150
+ x = "#{latitude},#{longitude}"
151
+ x += ",#{color}" if color
152
+ x += "#{alpha_character}" if alpha_character
153
+ return x
154
+ end
155
+ end
77
156
  end
@@ -1,40 +1,81 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
3
  require File.join(File.dirname(__FILE__), '..', 'lib', 'static_gmaps')
4
+ include StaticGmaps
4
5
 
5
6
  STATIC_GOOGLE_MAP_DEFAULT_PARAMETERS_TEST_IMAGE_PATH = File.join File.dirname(__FILE__), 'test_image.gif'
6
7
 
7
- describe StaticGmaps::Map, 'with no attributes set' do
8
- before(:each) do @static_google_map = StaticGmaps::Map.new end
8
+ describe StaticGmaps do
9
+ describe Map, 'initialize with no attributes' do
10
+ before(:each) do @map = StaticGmaps::Map.new end
11
+
12
+ it 'should set center to default' do @map.center.should == StaticGmaps::Map::DEFAULT_CENTER end
13
+ it 'should set zoom to default' do @map.zoom.should == StaticGmaps::Map::DEFAULT_ZOOM end
14
+ it 'should set size to default' do @map.size.should == StaticGmaps::Map::DEFAULT_SIZE end
15
+ it 'should set map_type to default' do @map.map_type.should == StaticGmaps::Map::DEFAULT_MAP_TYPE end
16
+ it 'should set key to default' do @map.key.should == StaticGmaps::Map::DEFAULT_KEY end
17
+ it 'should set its markers default' do @map.markers.should == StaticGmaps::Map::DEFAULT_MARKERS end
18
+ end
9
19
 
10
- it 'should have a center set to default' do @static_google_map.center.should == StaticGmaps::Map::DEFAULT_CENTER end
11
- it 'should have a zoom set to default' do @static_google_map.zoom.should == StaticGmaps::Map::DEFAULT_ZOOM end
12
- it 'should have a size set to default' do @static_google_map.size.should == StaticGmaps::Map::DEFAULT_SIZE end
13
- it 'should have a map_type set to default' do @static_google_map.map_type.should == StaticGmaps::Map::DEFAULT_MAP_TYPE end
14
- it 'should have a key set to default' do @static_google_map.key.should == StaticGmaps::Map::DEFAULT_KEY end
15
- end
16
-
17
- describe StaticGmaps::Map, 'with all attributes set' do
18
- before(:each) do
19
- @static_google_map = StaticGmaps::Map.new :center => [ 40.714728, -73.998672 ],
20
- :zoom => 12,
21
- :size => [ 500, 400 ],
22
- :map_type => :roadmap,
23
- :key => 'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA'
20
+ describe Map, 'initialize with all attributes' do
21
+ before(:each) do
22
+ @marker = StaticGmaps::Marker.new :latitude => 0, :longitude => 0
23
+ @map = StaticGmaps::Map.new :center => [ 40.714728, -73.998672 ],
24
+ :zoom => 12,
25
+ :size => [ 500, 400 ],
26
+ :map_type => :roadmap,
27
+ :key => 'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA',
28
+ :markers => [ @marker ]
29
+ end
30
+
31
+ it 'should set center' do @map.center.should == [ 40.714728, -73.998672 ] end
32
+ it 'should set zoom' do @map.zoom.should == 12 end
33
+ it 'should set size' do @map.size.should == [ 500, 400 ] end
34
+ it 'should set map_type' do @map.map_type.should == :roadmap end
35
+ it 'should set key' do @map.key.should == 'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA' end
36
+ it 'should set markers' do @map.markers.should == [ @marker ] end
24
37
  end
25
38
 
26
- it 'should have a center' do @static_google_map.center.should == [ 40.714728, -73.998672 ] end
27
- it 'should have a zoom' do @static_google_map.zoom.should == 12 end
28
- it 'should have a size' do @static_google_map.size.should == [ 500, 400 ] end
29
- it 'should have a map_type' do @static_google_map.map_type.should == :roadmap end
30
- it 'should have a key' do @static_google_map.key.should == 'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA' end
31
- end
32
-
33
- describe StaticGmaps::Map do
34
- before(:each) do @static_google_map = StaticGmaps::Map.new end
39
+ describe Map, 'with default attributes' do
40
+ before(:each) do @map = StaticGmaps::Map.new end
41
+
42
+ it 'should have a url' do @map.url.should == 'http://maps.google.com/staticmap?center=0,0&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA&map_type=roadmap&size=500x400&zoom=1' end
43
+ it 'should have a width' do @map.width.should == 500 end
44
+ it 'should have a height' do @map.height.should == 400 end
45
+ #it 'should respond to to_blob with an image' do @map.to_blob.should == File.read(STATIC_GOOGLE_MAP_DEFAULT_PARAMETERS_TEST_IMAGE_PATH) end
46
+ end
35
47
 
36
- it 'should have a url' do @static_google_map.url.should == 'http://maps.google.com/staticmap?center=0,0&zoom=1&size=500x400&map_type=roadmap&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA' end
37
- it 'should have a width' do @static_google_map.width.should == 500 end
38
- it 'should have a height' do @static_google_map.height.should == 400 end
39
- #it 'should respond to to_blob with an image' do @static_google_map.to_blob.should == File.read(STATIC_GOOGLE_MAP_DEFAULT_PARAMETERS_TEST_IMAGE_PATH) end
48
+ describe Map, 'with default attributes and markers' do
49
+ before(:each) do
50
+ @marker_1 = StaticGmaps::Marker.new :latitude => 10, :longitude => 20, :color => 'green', :alpha_character => 'A'
51
+ @marker_2 = StaticGmaps::Marker.new :latitude => 15, :longitude => 25, :color => 'blue', :alpha_character => 'B'
52
+ @map = StaticGmaps::Map.new :markers => [ @marker_1, @marker_2 ]
53
+ end
54
+
55
+ it 'should have a markers_url_fragment' do @map.markers_url_fragment.should == '10,20,greena|15,25,blueb' end
56
+ it 'should include the markers_url_fragment its url' do @map.url.should include(@map.markers_url_fragment) end
57
+ end
58
+
59
+ describe Marker, 'initialize with no attributes' do
60
+ before(:each) do @marker = StaticGmaps::Marker.new end
61
+
62
+ it 'should set latitude to default' do @marker.latitude.should == StaticGmaps::Marker::DEFAULT_LATITUDE end
63
+ it 'should set longitude to default' do @marker.longitude.should == StaticGmaps::Marker::DEFAULT_LONGITUDE end
64
+ it 'should set color to default' do @marker.color.should == StaticGmaps::Marker::DEFAULT_COLOR end
65
+ it 'should set alpha_character to default' do @marker.alpha_character.should == StaticGmaps::Marker::DEFAULT_ALPHA_CHARACTER end
66
+ end
67
+
68
+ describe Marker, 'initialize with all attributes' do
69
+ before(:each) do
70
+ @marker = StaticGmaps::Marker.new :latitude => 12,
71
+ :longitude => 34,
72
+ :color => 'red',
73
+ :alpha_character => 'z'
74
+ end
75
+
76
+ it 'should set latitude' do @marker.latitude.should == 12 end
77
+ it 'should set longitude' do @marker.longitude.should == 34 end
78
+ it 'should set color' do @marker.color.should == :red end
79
+ it 'should set alpha_character' do @marker.alpha_character.should == :z end
80
+ end
40
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static-gmaps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wulff
@@ -21,7 +21,7 @@ dependencies:
21
21
  - !ruby/object:Gem::Version
22
22
  version: 1.5.0
23
23
  version:
24
- description: "== DESCRIPTION: Provides an interface to the Google Static Maps API. == FEATURES/PROBLEMS: * See SYNOPSIS."
24
+ description: "== DESCRIPTION: Provides an interface to the Google Static Maps API. == FEATURES/PROBLEMS: * Provides an interface to the Google Static Maps API."
25
25
  email: johnwulff@gmail.com
26
26
  executables: []
27
27
 
@@ -40,7 +40,6 @@ files:
40
40
  - Rakefile
41
41
  - lib/static_gmaps.rb
42
42
  - spec/static_gmaps_spec.rb
43
- - spec/test_image.gif
44
43
  has_rdoc: true
45
44
  homepage:
46
45
  post_install_message:
data/spec/test_image.gif DELETED
Binary file