static_maps 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7ba7e06afbb871683653df0ff74a584fbb80f35
4
+ data.tar.gz: 444573695bd919136dc1462cb903b797b68fa498
5
+ SHA512:
6
+ metadata.gz: d1e9b7c18bf39716ae529e9e2a7854072abc61b6fd810509e9f5cde8ad32c021e45a0a88ece7606d5e613ddd5d8a78b4c0394b2661b691d6c7a8124bc2a65d2e
7
+ data.tar.gz: 6ef8a38f120243ec2f362ad30cc959d823e143efe2c56d953aa904a1f63b56d8639a39918d85c3a207df4ea749e831037af88b6087821fda497ff8fdc24ae6ea
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in static_maps.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nicholas Hwang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # StaticMaps (Google Static Maps V2 API)
2
+
3
+ (Work in Progress...)
4
+
5
+ Simple Ruby Wrapper for Google Static Maps V2 API
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'static_maps'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install static_maps
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ StaticMaps::API.fetch
25
+ => "http://maps.googleapis.com/maps/api/staticmap?visual_refresh=true&center=Stan,Mountain+View,CA&size=640x640&sensor=false&zoom=16"
26
+
27
+ StaticMaps::API.fetch({:center => 'Boston, MA', :size => '200x200'})
28
+ => "http://maps.googleapis.com/maps/api/staticmap?visual_refresh=true&center=Boston,MA&size=200x200&sensor=false&zoom=16"
29
+
30
+ ...To be continued
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/gmaps ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Simple CLI wrapper
4
+
5
+ require 'optparse'
6
+ require 'static_maps'
7
+
8
+ # CLI Arguments
9
+ params = {}
10
+ options = {}
11
+
12
+ OptionParser.new do |opts|
13
+
14
+ opts.on('-c','--center LOCATION', String,'Location of map center') do |c|
15
+ params[:center] = c
16
+ end
17
+
18
+ options[:open] = false
19
+ opts.on('-o','--[no-]open', 'Open in browser') do |o|
20
+ options[:open] = o
21
+ end
22
+
23
+ opts.on_tail("-h", "--help", "Show this message") do
24
+ puts opts
25
+ exit
26
+ end
27
+
28
+ end.parse!
29
+
30
+ # Fetch Google Maps URL
31
+ url = StaticMaps::API.fetch params
32
+
33
+ # Outputs
34
+ if options[:open]
35
+ puts %x{open #{url}}
36
+ else
37
+ puts 'Generated URL:', url
38
+ end
@@ -0,0 +1,4 @@
1
+ require "static_maps/version"
2
+ require "static_maps/validations"
3
+ require "static_maps/params"
4
+ require "static_maps/API"
@@ -0,0 +1,9 @@
1
+ module StaticMaps
2
+ module API
3
+ class << self
4
+ def fetch(params={})
5
+ Params.build_url(params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ module StaticMaps
2
+ module Params
3
+ extend self
4
+
5
+ BASE_URL = 'http://maps.googleapis.com/maps/api/staticmap'
6
+
7
+ DEFAULTS = {
8
+ :visual_refresh => true, # Use new color scheme
9
+ :center => 'Stan,Mountain View,CA', # GooglePlex's Stan
10
+ :size => '640x640', # Max size (free API)
11
+ :sensor => false, # No sensor
12
+ :zoom => 16 # No sensor
13
+ }.freeze
14
+
15
+ ALLOWED = [:visual_refresh, :center, :size, :sensor, :language, :region,
16
+ :zoom, :scale, :markers, :format, :maptype, :visible].freeze
17
+
18
+ def filter_allowed(params)
19
+ DEFAULTS.merge params.select {|k,v| ALLOWED.include? k}
20
+ end
21
+
22
+ def validated_params(params)
23
+ {}.tap do |p|
24
+ filter_allowed(params).map {|k,v| p[k] = Validations::validate(k,v)}
25
+ end
26
+ end
27
+
28
+ def concat_params(params)
29
+ [].tap do |url|
30
+ validated_params(params).each_with_index do |(k,v),i|
31
+ url << "#{i==0 ? '?' : '&'}#{k}=#{v}"
32
+ end
33
+ end.join('')
34
+ end
35
+
36
+ def url_safe(str)
37
+ str.strip.gsub(/[ |]+/,' ' => '+', '|' => '%7C')
38
+ end
39
+
40
+ def build_url(params)
41
+ url_safe(BASE_URL + concat_params(params))
42
+ end
43
+
44
+ end
45
+ end
46
+
@@ -0,0 +1,55 @@
1
+ module StaticMaps
2
+ module Validations
3
+ extend self
4
+
5
+ def validate(key,value)
6
+ value = case key
7
+ when :visual_refresh
8
+ to_bool(value, true)
9
+
10
+ when :sensor
11
+ to_bool(value, false)
12
+
13
+ when :size
14
+ size = value.match(/([\d]+)[\sx-]*([\d]+)?/)
15
+ if size
16
+ size[2].nil? ? size[1]+'x'+size[1] : size[1]+'x'+size[2]
17
+ else
18
+ '640x640'
19
+ end
20
+
21
+ when :scale
22
+ [1, value.to_i, 2].sort[1]
23
+
24
+ when :zoom
25
+ value.to_i == 0 ? 12 : value.to_i
26
+
27
+ when :format
28
+ ['png8','png','png32','gif','jpg','jpg-baseline'].
29
+ include?(value) ? value : 'png'
30
+
31
+ when :maptype
32
+ ['roadmap','satellite','terrain','hybrid'].
33
+ include?(value) ? value : 'roadmap'
34
+
35
+ else
36
+ value
37
+ end
38
+ end
39
+
40
+
41
+ private
42
+
43
+ # Naive bool force with fallback
44
+ def to_bool(value, fallback = false)
45
+ if !!value == value
46
+ value == true ? true : fallback
47
+ elsif value.class == String
48
+ value.casecmp('true') == 0 ? true : fallback
49
+ else
50
+ fallback
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module StaticMaps
2
+ VERSION = "0.0.1"
3
+ end
data/spec/API_spec.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'static_maps'
3
+
4
+ describe 'Google Maps API' do
5
+ let(:maps) { StaticMaps::API }
6
+
7
+ describe 'default url' do
8
+ it 'should return a simple map url' do
9
+ maps.fetch.should eq base_url+base_params
10
+ end
11
+ end
12
+
13
+ describe 'output' do
14
+ it 'should return a string url' do
15
+ maps.fetch.class.should eq String
16
+ end
17
+ end
18
+
19
+ describe 'response' do
20
+ pending 'should response act accordingly i.e. 400'
21
+ end
22
+
23
+ def base_url
24
+ "http://maps.googleapis.com/maps/api/staticmap?"
25
+ end
26
+ def base_params
27
+ "visual_refresh=true&center=Stan,Mountain+View,CA&"+
28
+ "size=640x640&sensor=false&zoom=16"
29
+ end
30
+ end
31
+
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'static_maps'
3
+
4
+ describe 'Params' do
5
+ let(:params) { StaticMaps::Params }
6
+ let(:base_url) { params::BASE_URL }
7
+ let(:defaults) { params::DEFAULTS }
8
+ let(:allowed) { params::ALLOWED }
9
+
10
+ describe 'base url' do
11
+ it { base_url.should eq 'http://maps.googleapis.com/maps/api/staticmap' }
12
+ end
13
+
14
+ describe 'defaults' do
15
+ it { defaults.frozen?.should eq true }
16
+ it { defaults.should include :visual_refresh }
17
+ it { defaults.should include :center }
18
+ it { defaults.should include :size }
19
+ it { defaults.should include :sensor }
20
+ it { defaults.should include :zoom }
21
+ end
22
+
23
+ describe 'allowed' do
24
+ let(:filtered_params){
25
+ params.filter_allowed({:scale => 2, :invalid => false })
26
+ }
27
+
28
+ it { allowed.frozen?.should eq true }
29
+ it { filtered_params.should include :zoom }
30
+ it { filtered_params.should_not include :invalid }
31
+ end
32
+
33
+ describe 'build url' do
34
+ let(:url) {
35
+ params.build_url({:center => 'City Hall, New York, NY',
36
+ :markers => 'color:red|City Hall, New York, NY'})
37
+ }
38
+
39
+ it { url.should include base_url }
40
+ it { url.should include 'City+Hall,+New+York,+NY' }
41
+ it { url.should_not include ' ' }
42
+ it { url.should include 'color:red%7CCity+Hall,+New+York,+NY' }
43
+ it { url.should_not include '|' }
44
+ end
45
+
46
+ end
47
+
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+
7
+ RSpec.configure do |config|
8
+ # Fail Fast
9
+ config.fail_fast = true
10
+
11
+ # Remove this line if you don't want RSpec's should and should_not
12
+ # methods or matchers
13
+ require 'rspec/expectations'
14
+ config.include RSpec::Matchers
15
+
16
+ # == Mock Framework
17
+ config.mock_framework = :rspec
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'static_maps'
3
+
4
+ describe StaticMaps do
5
+
6
+ it 'must have correct name' do
7
+ StaticMaps.name.should eq 'StaticMaps'
8
+ end
9
+
10
+ it 'must be defined (VERSION)' do
11
+ StaticMaps::VERSION.should_not eq nil
12
+ end
13
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'static_maps'
3
+
4
+ describe 'Param Validation' do
5
+ let(:params) { StaticMaps::Params }
6
+ let(:valids) { StaticMaps::Validations }
7
+
8
+ describe 'Visual Refresh and Sensor' do
9
+ it 'should permit boolean values' do
10
+ valids.validate(:visual_refresh, true).should eq true
11
+ valids.validate(:sensor, false).should eq false
12
+ end
13
+ it 'should permit string values' do
14
+ valids.validate(:visual_refresh, 'true').should eq true
15
+ valids.validate(:sensor, 'FalSE').should eq false
16
+ end
17
+ it 'should fallback to opinionated default' do
18
+ valids.validate(:visual_refresh, 'yes').should eq true
19
+ valids.validate(:sensor, nil).should eq false
20
+ end
21
+ end
22
+
23
+ describe 'Size' do
24
+ it 'should extract regex pattern /\n+x\n+/' do
25
+ valids.validate(:size, '300x300').should eq '300x300'
26
+ valids.validate(:size, 'bad300x300 bad').should eq '300x300'
27
+ end
28
+ it 'should take single value or non-zero value' do
29
+ valids.validate(:size, '300').should eq '300x300'
30
+ valids.validate(:size, 'x300').should eq '300x300'
31
+ valids.validate(:size, '-100x - 300').should eq '100x300'
32
+ end
33
+ it 'should fallback to max size' do
34
+ valids.validate(:size, '').should eq '640x640'
35
+ pending 'test for zero values i.e. 0, 0x0, 00x0000'
36
+ end
37
+ end
38
+
39
+ describe 'Scale' do
40
+ it 'should permit 1 or 2 numbers' do
41
+ valids.validate(:scale,1).should eq 1
42
+ valids.validate(:scale,2).should eq 2
43
+ end
44
+ it 'should permit 1 or 2 string' do
45
+ valids.validate(:scale,"1").should eq 1
46
+ valids.validate(:scale,"2").should eq 2
47
+ end
48
+ it 'should fallback to 1' do
49
+ valids.validate(:scale,"0").should eq 1
50
+ valids.validate(:scale,"").should eq 1
51
+ valids.validate(:scale,"bad").should eq 1
52
+ end
53
+ end
54
+
55
+ describe 'Zoom' do
56
+ it 'should permit numbers' do
57
+ valids.validate(:zoom,16).should eq 16
58
+ end
59
+ it 'should permit strings' do
60
+ valids.validate(:zoom,'16').should eq 16
61
+ end
62
+ it 'should fallback to zoom=12' do
63
+ valids.validate(:zoom,'').should eq 12
64
+ valids.validate(:zoom,'bad').should eq 12
65
+ end
66
+ end
67
+
68
+ describe 'Format' do
69
+ it 'should permit string' do
70
+ valids.validate(:format,'gif').should eq 'gif'
71
+ end
72
+ it 'should fallback to png' do
73
+ valids.validate(:format,'badgif').should eq 'png'
74
+ valids.validate(:format,false).should eq 'png'
75
+ end
76
+ end
77
+
78
+ describe 'Map Type' do
79
+ it 'should permit string' do
80
+ valids.validate(:maptype,'terrain').should eq 'terrain'
81
+ end
82
+ it 'should fallback to roadmap' do
83
+ valids.validate(:maptype,'badtype').should eq 'roadmap'
84
+ valids.validate(:maptype,false).should eq 'roadmap'
85
+ end
86
+ end
87
+
88
+ pending 'center (lat/long or loc) - comma delimit'
89
+ pending 'marker {size{tiny, mid, small}, color{HEX/basic}, label {A-Z0-9}}'
90
+ pending 'style, paths, & complex markers...TBD'
91
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'static_maps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "static_maps"
8
+ spec.version = StaticMaps::VERSION
9
+ spec.authors = ["Nicholas Hwang"]
10
+ spec.email = ["nick.joosung.hwang@gmail.com"]
11
+ spec.description = ["Static Google Maps v2 API"]
12
+ spec.summary = ["Ruby wrapper for Static Google Maps v2 API"]
13
+ spec.homepage = "https://github.com/geekjuice/static_google_maps_v2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ # spec.executables = %w( maps )
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov"
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_maps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Hwang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: '["Static Google Maps v2 API"]'
70
+ email:
71
+ - nick.joosung.hwang@gmail.com
72
+ executables:
73
+ - gmaps
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/gmaps
83
+ - lib/static_maps.rb
84
+ - lib/static_maps/API.rb
85
+ - lib/static_maps/params.rb
86
+ - lib/static_maps/validations.rb
87
+ - lib/static_maps/version.rb
88
+ - spec/API_spec.rb
89
+ - spec/params_spec.rb
90
+ - spec/spec_helper.rb
91
+ - spec/static_maps_spec.rb
92
+ - spec/validations_spec.rb
93
+ - static_maps.gemspec
94
+ homepage: https://github.com/geekjuice/static_google_maps_v2
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.1.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: '["Ruby wrapper for Static Google Maps v2 API"]'
118
+ test_files:
119
+ - spec/API_spec.rb
120
+ - spec/params_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/static_maps_spec.rb
123
+ - spec/validations_spec.rb