gmapper 0.1.0 → 0.2.0

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/Manifest CHANGED
@@ -1,8 +1,14 @@
1
1
  README.rdoc
2
2
  Rakefile
3
- gmapper.gemspec
3
+ lib/autoload.rb
4
+ lib/exceptions.rb
4
5
  lib/gmapper.rb
5
- lib/google/autoload.rb
6
+ lib/google/maps/static/items/base.rb
6
7
  lib/google/maps/static/map.rb
7
- lib/google/maps/static/marker.rb
8
+ lib/google/maps/static/markers.rb
9
+ lib/google/maps/static/path.rb
10
+ lib/google/maps/static/visible.rb
11
+ spec/gmapper/map_spec.rb
12
+ spec/matchers.rb
13
+ spec/spec_helper.rb
8
14
  Manifest
@@ -1,6 +1,6 @@
1
1
  = GMapper
2
2
 
3
- A version agnostic library for the Google Maps API.
3
+ A (mostly) version agnostic, dependency free Ruby library for the Google Maps API.
4
4
 
5
5
  Currently only supports Google Static Maps.
6
6
 
@@ -10,11 +10,6 @@ Currently only supports Google Static Maps.
10
10
  gem install gmapper
11
11
 
12
12
 
13
- == Dependancies
14
-
15
- active_support/inflector
16
-
17
-
18
13
  == Usage
19
14
 
20
15
  To create a map of a location, say the Googleplex:
@@ -23,7 +18,7 @@ To create a map of a location, say the Googleplex:
23
18
 
24
19
  Then, lets say we want to add a marker to point out where it is:
25
20
 
26
- map.markers << Google::Maps::Static::Marker.new [37.422, 122.084], :color => :blue, :label => 'G'
21
+ map << Google::Maps::Static::Marker.new([37.422, 122.084], :color => :blue, :label => 'G')
27
22
 
28
23
  To save the map to your local disk:
29
24
 
@@ -32,8 +27,7 @@ To save the map to your local disk:
32
27
 
33
28
  == Todo
34
29
 
35
- * I still need to add some tests
36
- * There are a few other API's that I have to refactor before I add
30
+ * There are a few other classes that I have to refactor before I add
37
31
 
38
32
 
39
33
  == Lisence
data/Rakefile CHANGED
@@ -1,14 +1,35 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'rake/rdoctask'
3
4
  require 'echoe'
5
+ require 'spec/rake/spectask'
4
6
 
5
- Echoe.new( 'gmapper', '0.1.0' ) do |p|
6
- p.description = "A version agnostic library for the Google Maps API."
7
- p.url = "http://github.com/tpeden/gmapper"
7
+ Echoe.new( 'gmapper', '0.2.0' ) do |p|
8
8
  p.author = "TJ Peden"
9
9
  p.email = "tj.peden@tj-coding.com"
10
+ p.summary = "A version agnostic, dependency free Ruby library for the Google Maps API."
11
+ p.description = <<-EOS
12
+ A (mostly) version agnostic, dependency free Ruby library for the Google Maps API.
13
+
14
+ Currently only supports Google Static Maps.
15
+ EOS
16
+ p.url = "http://github.com/tpeden/gmapper"
10
17
  p.ignore_pattern = ["tmp/*", "script/*"]
11
18
  p.development_dependencies = []
12
19
  end
13
20
 
21
+ Spec::Rake::SpecTask.new(:html) do |t|
22
+ t.warning = true
23
+ t.spec_files = FileList['spec/**/*_spec.rb']
24
+ t.spec_opts = %w(--format html:doc/reports/tools/failing_examples.html)
25
+ t.fail_on_error = false
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:nested) do |t|
29
+ # t.warning = true
30
+ t.spec_files = FileList['spec/**/*_spec.rb']
31
+ t.spec_opts = %w{--backtrace --format nested --color}
32
+ t.fail_on_error = false
33
+ end
34
+
14
35
  Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -2,21 +2,24 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{gmapper}
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Peden"]
9
- s.date = %q{2010-09-01}
10
- s.description = %q{A version agnostic library for the Google Maps API.}
9
+ s.date = %q{2010-09-02}
10
+ s.description = %q{A (mostly) version agnostic, dependency free Ruby library for the Google Maps API.
11
+
12
+ Currently only supports Google Static Maps.
13
+ }
11
14
  s.email = %q{tj.peden@tj-coding.com}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/gmapper.rb", "lib/google/autoload.rb", "lib/google/maps/static/map.rb", "lib/google/maps/static/marker.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "gmapper.gemspec", "lib/gmapper.rb", "lib/google/autoload.rb", "lib/google/maps/static/map.rb", "lib/google/maps/static/marker.rb", "Manifest"]
15
+ s.extra_rdoc_files = ["README.rdoc", "lib/autoload.rb", "lib/exceptions.rb", "lib/gmapper.rb", "lib/google/maps/static/items/base.rb", "lib/google/maps/static/map.rb", "lib/google/maps/static/markers.rb", "lib/google/maps/static/path.rb", "lib/google/maps/static/visible.rb"]
16
+ s.files = ["README.rdoc", "Rakefile", "lib/autoload.rb", "lib/exceptions.rb", "lib/gmapper.rb", "lib/google/maps/static/items/base.rb", "lib/google/maps/static/map.rb", "lib/google/maps/static/markers.rb", "lib/google/maps/static/path.rb", "lib/google/maps/static/visible.rb", "spec/gmapper/map_spec.rb", "spec/matchers.rb", "spec/spec_helper.rb", "Manifest", "gmapper.gemspec"]
14
17
  s.homepage = %q{http://github.com/tpeden/gmapper}
15
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gmapper", "--main", "README.rdoc"]
16
19
  s.require_paths = ["lib"]
17
20
  s.rubyforge_project = %q{gmapper}
18
21
  s.rubygems_version = %q{1.3.7}
19
- s.summary = %q{A version agnostic library for the Google Maps API.}
22
+ s.summary = %q{A version agnostic, dependency free Ruby library for the Google Maps API.}
20
23
 
21
24
  if s.respond_to? :specification_version then
22
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -0,0 +1,26 @@
1
+ module GMapper
2
+
3
+ module Autoload
4
+
5
+ def autoload( const_name, path = nil )
6
+ full_name = [self.name, const_name.to_s, path].compact.join('::')
7
+ location = path || underscore( full_name )
8
+
9
+ super const_name, location
10
+ end
11
+
12
+ # I take no credit for this, it's obviously ActiveSupport
13
+ # code, just don't need the depenancy
14
+ def underscore(camel_cased_word)
15
+ word = camel_cased_word.to_s.dup
16
+ word.gsub!(/::/, '/')
17
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
18
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
19
+ word.tr!("-", "_")
20
+ word.downcase!
21
+ word
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,5 @@
1
+ module GMapper
2
+
3
+ class Error < StandardError; end
4
+
5
+ end
@@ -1,6 +1,7 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
- require 'google/autoload'
3
+ require 'autoload'
4
+ require 'exceptions'
4
5
 
5
6
  unless [].respond_to? :extract_options!
6
7
  class Array
@@ -15,13 +16,22 @@ module Google
15
16
  module Maps
16
17
 
17
18
  module Static
18
- extend Google::Autoload
19
+ extend GMapper::Autoload
19
20
 
20
21
  autoload :Map
21
- autoload :Marker
22
+ autoload :Markers
23
+ autoload :Path
24
+ autoload :Visible
25
+
26
+ module Items
27
+ extend GMapper::Autoload # Again?
28
+
29
+ autoload :Base
30
+
31
+ end
22
32
 
23
33
  end # => Static
24
34
 
25
35
  end # => Maps
26
36
 
27
- end # => Static
37
+ end # => Google
@@ -0,0 +1,41 @@
1
+ module Google
2
+
3
+ module Maps
4
+
5
+ module Static
6
+
7
+ module Items
8
+
9
+ class Base
10
+
11
+ def initialize *args
12
+ @options = args.extract_options!
13
+
14
+ raise GMapper::Error, "One or more locations is required for the feature." if args.empty?
15
+ @locations = args
16
+ @item = self.class.name.split('::').last.downcase
17
+ end
18
+
19
+ def to_s
20
+ raise GMapper::Error, "@item cannot be nil!" if @item.nil?
21
+
22
+ result = @options.map do |key, value|
23
+ "#{key.to_s}:#{value.to_s}|"
24
+ end.join
25
+
26
+ result += @locations.map do |loc|
27
+ loc.is_a?(Array) ? loc.join(',') : loc.to_s
28
+ end.join('|')
29
+
30
+ "#{@item}=#{result}"
31
+ end
32
+
33
+ end # => Google::Maps::Static::Items::Base
34
+
35
+ end # => Google::Maps::Static::Items
36
+
37
+ end # => Google::Maps::Static
38
+
39
+ end # => Google::Maps
40
+
41
+ end # => Google
@@ -5,24 +5,56 @@ module Google
5
5
  module Static
6
6
 
7
7
  class Map
8
- attr_reader :markers
9
-
10
8
  def initialize *args
11
9
  @base = "http://maps.google.com/maps/api/staticmap"
12
10
 
13
- @options = args.extract_options!
14
- @options[:sensor] = false unless @options.key?(:sensor)
11
+ @options = {
12
+ # :zoom => 12,
13
+ :size => [400, 400],
14
+ :sensor => false
15
+ }
16
+ @options.merge! args.extract_options!
15
17
 
16
- @markers = []
18
+ @items = args.select { |item| item.is_a?(Items::Base) }.map(&:to_s)
19
+ end
20
+
21
+ def []= key, value
22
+ @options[key] = value
23
+ @url = nil if @url # Object has become dirty, url should be regenerated
17
24
  end
18
25
 
26
+ def << item
27
+ raise GMapper::Error, "Please only use Items::Base decendants." unless item.is_a?(Items::Base)
28
+ @items << item.to_s
29
+ @url = nil if @url # Object has become dirty, url should be regenerated
30
+ end
31
+ alias_method :add_item, :<<
32
+
19
33
  def save file
20
- response = ::Net::HTTP.get( ::URI.parse( url ) )
34
+ response = ::Net::HTTP.get( ::URI.parse( url._encode ) )
21
35
  File.open( file, 'wb' ) { |f| f.write response }
22
36
  end
23
37
 
24
38
  def url
25
- @url ||= "#{@base}?#{parameters}"
39
+ unless @options.key?(:center) || !@items.empty?
40
+ raise GMapper::Error, "Must specify the center location unless you add one or more map items."
41
+ end
42
+
43
+ unless @options.key?(:zoom) || !@items.empty?
44
+ raise GMapper::Error, "Must specify the zoom level unless you add one or more map items."
45
+ end
46
+
47
+ if @url.nil?
48
+ url = "#{@base}?#{parameters}"
49
+
50
+ def url._encode
51
+ gsub(/\|/, '%7C')
52
+ end
53
+
54
+ @url = url
55
+ end
56
+
57
+ @url
26
58
  end
27
59
  alias_method :to_s, :url
28
60
 
@@ -37,10 +69,12 @@ module Google
37
69
  else object.to_s
38
70
  end
39
71
 
40
- "#{::URI.escape( key.to_s )}=#{::URI.escape( value )}"
72
+ "#{key.to_s}=#{value}"
41
73
  end.join('&')
42
74
 
43
- result += '&' + @markers.map(&:to_s).join('&') unless @markers.empty?
75
+ result += '&' + @items.join('&') unless @items.empty?
76
+
77
+ result.gsub!(/\s/, '+')
44
78
 
45
79
  result
46
80
  end
@@ -0,0 +1,13 @@
1
+ module Google
2
+
3
+ module Maps
4
+
5
+ module Static
6
+
7
+ class Markers < Items::Base; end
8
+
9
+ end # => Google::Maps::Static
10
+
11
+ end # => Google::Maps
12
+
13
+ end # => Google
@@ -0,0 +1,13 @@
1
+ module Google
2
+
3
+ module Maps
4
+
5
+ module Static
6
+
7
+ class Path < Items::Base; end
8
+
9
+ end # => Google::Maps::Static
10
+
11
+ end # => Google::Maps
12
+
13
+ end # => Google
@@ -0,0 +1,13 @@
1
+ module Google
2
+
3
+ module Maps
4
+
5
+ module Static
6
+
7
+ class Visible < Items::Base; end
8
+
9
+ end # => Google::Maps::Static
10
+
11
+ end # => Google::Maps
12
+
13
+ end # => Google
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Google::Maps::Static::Map do
4
+ it "should be able to create a proper url for a given coordinates" do
5
+ map = Google::Maps::Static::Map.new :center => [40.714728, -73.998672], :zoom => 12
6
+
7
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=400x400&sensor=false")
8
+ end
9
+
10
+ it "should be able to create a proper url for a given city and state" do
11
+ options = {
12
+ :center => "Berkeley,CA",
13
+ :zoom => 14
14
+ }
15
+
16
+ map = Google::Maps::Static::Map.new options
17
+
18
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&sensor=false")
19
+ end
20
+
21
+ it "should be able to create a proper url given a location with markers" do
22
+ options = {
23
+ :center => "Brooklyn Bridge,New York,NY",
24
+ :zoom => 14,
25
+ :size => [512, 512],
26
+ :maptype => :roadmap
27
+ }
28
+
29
+ map = Google::Maps::Static::Map.new options
30
+ map << Google::Maps::Static::Markers.new( [40.702147, -74.015794], :color => :blue, :label => 'S' )
31
+ map << Google::Maps::Static::Markers.new( [40.711614, -74.012318], :color => :green, :label => 'G' )
32
+ map << Google::Maps::Static::Markers.new( [40.718217, -73.998284], :color => :red, :label => 'C' )
33
+
34
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|label:C|40.718217,-73.998284&sensor=false")
35
+ end
36
+
37
+ it "should be able to create a proper url given a location with multiple marker of the same style" do
38
+ map = Google::Maps::Static::Map.new
39
+ map[:center] = "Williamsburg,Brooklyn,NY"
40
+ map[:zoom] = 13
41
+ map << Google::Maps::Static::Markers.new( '11211', '11206', '11222', :color => :blue, :label => 'S' )
42
+
43
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=Williamsburg,Brooklyn,NY&zoom=13&size=400x400&markers=color:blue|label:S|11211|11206|11222&sensor=false")
44
+ end
45
+
46
+ it "should be able to create a proper url given a location with a path" do
47
+ locations = [ [40.737102,-73.990318], [40.749825,-73.987963], [40.752946,-73.987384], [40.755823,-73.986397] ]
48
+ map = Google::Maps::Static::Map.new
49
+ map[:size] = [512, 512]
50
+ map << Google::Maps::Static::Path.new( *locations, :color => '0x0000ff', :weight => 5 )
51
+
52
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?path=color:0x0000ff|weight:5|40.737102,-73.990318|40.749825,-73.987963|40.752946,-73.987384|40.755823,-73.986397&size=512x512&sensor=false")
53
+ end
54
+
55
+ it "should be able to create a proper url given a viewport (visible)" do
56
+ locations = %w{77+Massachusetts+Ave,Cambridge,MA Harvard+Square,Cambridge,MA}
57
+ map = Google::Maps::Static::Map.new
58
+ map[:size] = [512, 512]
59
+ map << Google::Maps::Static::Visible.new( *locations )
60
+
61
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?visible=77+Massachusetts+Ave,Cambridge,MA|Harvard+Square,Cambridge,MA&size=512x512&sensor=false")
62
+ end
63
+
64
+ it "should be able to set parameters in the Map#[]= method" do
65
+ map = Google::Maps::Static::Map.new
66
+ map[:center] = "Berkeley,CA"
67
+ map[:zoom] = 14
68
+
69
+ map.url.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&sensor=false")
70
+ end
71
+
72
+ it "should be able to encode the url for use with URI#parse (in Map#save)" do
73
+ map = Google::Maps::Static::Map.new
74
+ map[:center] = "Williamsburg,Brooklyn,NY"
75
+ map[:zoom] = 13
76
+ map << Google::Maps::Static::Markers.new( '11211', '11206', '11222', :color => :blue, :label => 'S' )
77
+
78
+ map.url._encode.should orderlessly_match("http://maps.google.com/maps/api/staticmap?center=Williamsburg,Brooklyn,NY&zoom=13&size=400x400&markers=color:blue%7Clabel:S%7C11211%7C11206%7C11222&sensor=false")
79
+ end
80
+
81
+ it "should not be able to create a url without the :center parameter" do
82
+ map = Google::Maps::Static::Map.new
83
+
84
+ lambda do
85
+ map.url
86
+ end.should raise_error( GMapper::Error, "Must specify the center location unless you add one or more map items." )
87
+ end
88
+ end
@@ -0,0 +1,14 @@
1
+ Spec::Matchers.define :orderlessly_match do |original_string|
2
+ regex = /[\?\|&]/
3
+ match do |given_string|
4
+ original_string.split(regex).sort == given_string.split(regex).sort
5
+ end
6
+
7
+ failure_message_for_should do |given_string|
8
+ "expected\n#{given_string.split(regex).sort.inspect}\nto have the same parameters as\n#{original_string.split(regex).sort.inspect}"
9
+ end
10
+
11
+ failure_message_for_should_not do |given_string|
12
+ "expected\n#{given_string.split(regex).sort.inspect}\nnot to have the same parameters as\n#{original_string.split(regex).sort.inspect}"
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'matchers'
4
+ require 'gmapper'
5
+
6
+ Spec::Runner.configure do |config|
7
+ # Nothing so far
8
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - TJ Peden
@@ -14,11 +14,15 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-01 00:00:00 -04:00
17
+ date: 2010-09-02 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: A version agnostic library for the Google Maps API.
21
+ description: |
22
+ A (mostly) version agnostic, dependency free Ruby library for the Google Maps API.
23
+
24
+ Currently only supports Google Static Maps.
25
+
22
26
  email: tj.peden@tj-coding.com
23
27
  executables: []
24
28
 
@@ -26,19 +30,30 @@ extensions: []
26
30
 
27
31
  extra_rdoc_files:
28
32
  - README.rdoc
33
+ - lib/autoload.rb
34
+ - lib/exceptions.rb
29
35
  - lib/gmapper.rb
30
- - lib/google/autoload.rb
36
+ - lib/google/maps/static/items/base.rb
31
37
  - lib/google/maps/static/map.rb
32
- - lib/google/maps/static/marker.rb
38
+ - lib/google/maps/static/markers.rb
39
+ - lib/google/maps/static/path.rb
40
+ - lib/google/maps/static/visible.rb
33
41
  files:
34
42
  - README.rdoc
35
43
  - Rakefile
36
- - gmapper.gemspec
44
+ - lib/autoload.rb
45
+ - lib/exceptions.rb
37
46
  - lib/gmapper.rb
38
- - lib/google/autoload.rb
47
+ - lib/google/maps/static/items/base.rb
39
48
  - lib/google/maps/static/map.rb
40
- - lib/google/maps/static/marker.rb
49
+ - lib/google/maps/static/markers.rb
50
+ - lib/google/maps/static/path.rb
51
+ - lib/google/maps/static/visible.rb
52
+ - spec/gmapper/map_spec.rb
53
+ - spec/matchers.rb
54
+ - spec/spec_helper.rb
41
55
  - Manifest
56
+ - gmapper.gemspec
42
57
  has_rdoc: true
43
58
  homepage: http://github.com/tpeden/gmapper
44
59
  licenses: []
@@ -76,6 +91,6 @@ rubyforge_project: gmapper
76
91
  rubygems_version: 1.3.7
77
92
  signing_key:
78
93
  specification_version: 3
79
- summary: A version agnostic library for the Google Maps API.
94
+ summary: A version agnostic, dependency free Ruby library for the Google Maps API.
80
95
  test_files: []
81
96
 
@@ -1,16 +0,0 @@
1
- require 'active_support/inflector'
2
-
3
- module Google
4
-
5
- module Autoload
6
-
7
- def autoload( const_name, path = nil )
8
- full_name = [self.name, const_name.to_s, path].compact.join('::')
9
- location = path || ActiveSupport::Inflector.underscore( full_name )
10
-
11
- super const_name, location
12
- end
13
-
14
- end
15
-
16
- end
@@ -1,37 +0,0 @@
1
- module Google
2
-
3
- module Maps
4
-
5
- module Static
6
-
7
- class Marker
8
-
9
- def initialize *args
10
- @options = args.extract_options!
11
-
12
- raise "Must specify one or more points to place markers" if args.empty?
13
-
14
- @locations = args
15
- end
16
-
17
- def to_s
18
- result = "markers="
19
-
20
- result += @options.inject('') do |output, pair|
21
- output + "#{pair.first.to_s}:#{pair.last.to_s}|"
22
- end
23
-
24
- result += @locations.map do |loc|
25
- loc.is_a?(Array) ? log.join(',') : loc.to_s
26
- end.join('|')
27
-
28
- ::URI.encode( result )
29
- end
30
-
31
- end # => Google::Maps::Static::Marker
32
-
33
- end # => Google::Maps::Static
34
-
35
- end # => Google::Maps
36
-
37
- end # => Google