google_maps 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/google_maps.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "google_maps"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tushar Ranka"]
12
- s.date = "2012-05-15"
12
+ s.date = "2012-05-30"
13
13
  s.description = "General Purpose Library to interact with Google Maps v3 api. Geocoder. "
14
14
  s.email = "tusharranka@gmail.com"
15
15
  s.extra_rdoc_files = [
data/lib/google_maps.rb CHANGED
@@ -11,6 +11,14 @@ module GoogleMaps
11
11
  block.call(self)
12
12
  end
13
13
 
14
+ def self.client(*args)
15
+ if args.present?
16
+ @@client = args.first
17
+ else
18
+ @@client
19
+ end
20
+ end
21
+
14
22
  def self.key(*args)
15
23
  if args.present?
16
24
  @@key = args.first
@@ -18,7 +26,7 @@ module GoogleMaps
18
26
  @@key
19
27
  end
20
28
  end
21
-
29
+
22
30
  def self.use_enterprise_account(*args)
23
31
  if args.first.present? && args.first == false
24
32
  @@enterprise_account = false
@@ -31,11 +39,4 @@ module GoogleMaps
31
39
  @@enterprise_account || false
32
40
  end
33
41
 
34
- def self.key_name
35
- enterprise_account? ? :client : :key
36
- end
37
-
38
- def self.geocoder_key_name
39
- :clientId if enterprise_account?
40
- end
41
42
  end
@@ -1,3 +1,5 @@
1
+ require 'base64'
2
+ require 'openssl'
1
3
  require 'google_maps/geocoder/result'
2
4
  require 'google_maps/geocoder/location'
3
5
 
@@ -10,7 +12,8 @@ module GoogleMaps
10
12
 
11
13
  module Geocoder
12
14
 
13
- URI_BASE = "maps.googleapis.com/maps/api/geocode/json"
15
+ URI_DOMAIN = "maps.googleapis.com"
16
+ URI_BASE = "/maps/api/geocode/json"
14
17
 
15
18
  def self.locate!(address, options = { })
16
19
  options = {
@@ -26,22 +29,32 @@ module GoogleMaps
26
29
  end
27
30
 
28
31
  def self.url(options)
29
- ssl = options.delete(:ssl)
30
-
31
- if !options[:clientId] && ::GoogleMaps.enterprise_account?
32
- options.merge!(::GoogleMaps.geocoder_key_name => ::GoogleMaps.key)
33
- end
32
+ ssl = options.delete(:ssl)
33
+ # for enterprise account
34
+ client = options.delete(:client) || ::GoogleMaps.client
35
+ key = options.delete(:key) || ::GoogleMaps.key
34
36
 
35
37
  parameters = []
36
- options.each do |key, value|
37
- parameters << "#{key}=#{CGI.escape(value.to_s)}"
38
+ options.each do |k, v|
39
+ parameters << "#{k}=#{CGI.escape(v.to_s)}"
40
+ end
41
+
42
+ if ::GoogleMaps.enterprise_account? && client && key
43
+ parameters << "client=#{CGI.escape(client)}"
44
+ sign_str = "#{URI_BASE}?#{parameters.join('&')}"
45
+ sha1 = OpenSSL::Digest::Digest.new('sha1')
46
+ binary_key = Base64.decode64(key.tr('-_','+/'))
47
+ binary_signature = OpenSSL::HMAC.digest(sha1, binary_key, sign_str)
48
+ signature = Base64.encode64(binary_signature).tr('+/','-_').strip
49
+ parameters << "signature=#{signature}"
38
50
  end
51
+
39
52
  "#{uri_base_path(:ssl => ssl)}?#{parameters.join('&')}"
40
53
  end
41
54
 
42
55
  def self.uri_base_path(options = { })
43
56
  protocol = options[:ssl] ? "https" : "http"
44
- "#{protocol}://#{URI_BASE}"
57
+ "#{protocol}://#{URI_DOMAIN}#{URI_BASE}"
45
58
  end
46
59
  end
47
- end
60
+ end
@@ -64,9 +64,9 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
64
64
 
65
65
 
66
66
  def test_url_base_path
67
- assert_equal "http://#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path
68
- assert_equal "http://#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path(:ssl => false)
69
- assert_equal "https://#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path(:ssl => true)
67
+ assert_equal "http://#{GoogleMaps::Geocoder::URI_DOMAIN}#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path
68
+ assert_equal "http://#{GoogleMaps::Geocoder::URI_DOMAIN}#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path(:ssl => false)
69
+ assert_equal "https://#{GoogleMaps::Geocoder::URI_DOMAIN}#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path(:ssl => true)
70
70
  end
71
71
 
72
72
  def test_url
@@ -74,11 +74,11 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
74
74
  assert_url_parts expected_url("address=50+Castilian+Drive%2C+Goleta%2C+CA&sensor=false", true), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :ssl => true, :address => "50 Castilian Drive, Goleta, CA"))
75
75
  end
76
76
 
77
- def test_url__with_client_id
78
- GoogleMaps.key "bar"
77
+ def test_url__with_enterprise_account
78
+ GoogleMaps.client "clientID"
79
+ GoogleMaps.key "vNIXE0xscrmjlyV-12Nj_BvUPaw="
79
80
  GoogleMaps.use_enterprise_account
80
- assert_url_parts expected_url("address=A&sensor=false&clientId=foo"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A", :clientId => 'foo'))
81
- assert_url_parts expected_url("address=A&sensor=false&clientId=bar"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A"))
81
+ assert_equal expected_url("address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA="), GoogleMaps::Geocoder.url(ordered_hash(:address => "New York", :sensor => false))
82
82
  end
83
83
 
84
84
  private
@@ -102,7 +102,7 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
102
102
  end
103
103
 
104
104
  def expected_url(parameters, ssl = false)
105
- "http#{'s' if ssl}://#{GoogleMaps::Geocoder::URI_BASE}?#{parameters}"
105
+ "http#{'s' if ssl}://#{GoogleMaps::Geocoder::URI_DOMAIN}#{GoogleMaps::Geocoder::URI_BASE}?#{parameters}"
106
106
  end
107
107
 
108
108
  def ordered_hash(hash)
@@ -113,4 +113,4 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
113
113
  oh.merge!(hash)
114
114
  oh
115
115
  end
116
- end
116
+ end
@@ -1,28 +1,22 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
4
- def test_configure__free_key
4
+ def test_configure__free_account
5
5
  GoogleMaps.configure do |maps|
6
- maps.key "foobar"
7
6
  end
8
7
 
9
- assert_equal "foobar", GoogleMaps.key
10
8
  assert_equal false, GoogleMaps.enterprise_account?
11
- assert_equal :key, GoogleMaps.key_name
12
- assert_equal nil, GoogleMaps.geocoder_key_name
13
9
  end
14
10
 
15
- def test_configure__enterprise_key
11
+ def test_configure__enterprise_account
16
12
  GoogleMaps.configure do |maps|
17
- maps.key "foobarenterprise"
13
+ maps.client "foobar"
14
+ maps.key "foobarkey"
18
15
  maps.use_enterprise_account
19
16
  end
20
17
 
21
- assert_equal "foobarenterprise", GoogleMaps.key
18
+ assert_equal "foobar", GoogleMaps.client
19
+ assert_equal "foobarkey", GoogleMaps.key
22
20
  assert GoogleMaps.enterprise_account?
23
- assert_equal :client, GoogleMaps.key_name
24
- assert_equal :clientId, GoogleMaps.geocoder_key_name
25
21
  end
26
-
27
-
28
22
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_maps
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tushar Ranka
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-15 00:00:00 Z
18
+ date: 2012-05-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime