google_maps 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/google_maps.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "google_maps"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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"]
@@ -20,6 +20,11 @@ module GoogleMaps
20
20
 
21
21
  def self.url(options)
22
22
  ssl = options.delete(:ssl)
23
+
24
+ if !options[:clientId] && ::GoogleMaps.enterprise_account
25
+ options.merge!(::GoogleMaps.geocoder_key_name => ::GoogleMaps.key)
26
+ end
27
+
23
28
  parameters = []
24
29
  options.each do |key, value|
25
30
  parameters << "#{key}=#{CGI.escape(value.to_s)}"
data/lib/google_maps.rb CHANGED
@@ -2,3 +2,15 @@ require 'rubygems'
2
2
  require 'rest_client'
3
3
  require 'active_support/all'
4
4
  require 'google_maps/geocoder'
5
+
6
+ module GoogleMaps
7
+ mattr_accessor :key, :enterprise_account
8
+
9
+ def self.key_name
10
+ enterprise_account ? "client" : "key"
11
+ end
12
+
13
+ def self.geocoder_key_name
14
+ "clientId" if enterprise_account
15
+ end
16
+ end
@@ -1,10 +1,11 @@
1
1
  require 'test_helper'
2
2
  require 'mocha'
3
3
 
4
- class GoogleMaps::GeocoderTest < Test::Unit::TestCase
4
+ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
5
5
 
6
6
  def test_locate__castilian
7
- RestClient.expects(:get).with(expected_url("address=50+Castilian+Drive%2C+Goleta%2C+CA&sensor=false")).returns(castilian_json)
7
+ GoogleMaps::Geocoder.expects(:url).with(:address => "50 Castilian Drive, Goleta, CA", :ssl => false, :sensor => false).returns(:url)
8
+ RestClient.expects(:get).with(:url).returns(castilian_json)
8
9
  result = GoogleMaps::Geocoder.locate!("50 Castilian Drive, Goleta, CA")
9
10
 
10
11
  assert result.status.ok?
@@ -20,7 +21,8 @@ class GoogleMaps::GeocoderTest < Test::Unit::TestCase
20
21
  end
21
22
 
22
23
  def test_locate__new_york
23
- RestClient.expects(:get).with(expected_url("address=New+York&sensor=false")).returns(new_york_json)
24
+ GoogleMaps::Geocoder.expects(:url).with(:address => "New York", :ssl => false, :sensor => false).returns(:url)
25
+ RestClient.expects(:get).with(:url).returns(new_york_json)
24
26
  result = GoogleMaps::Geocoder.locate!("New York")
25
27
  assert result.status.ok?
26
28
 
@@ -52,18 +54,48 @@ class GoogleMaps::GeocoderTest < Test::Unit::TestCase
52
54
  end
53
55
 
54
56
  def test_url
55
- assert_equal expected_url("address=50+Castilian+Drive%2C+Goleta%2C+CA&sensor=true"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => true, :ssl => false, :address => "50 Castilian Drive, Goleta, CA"))
56
- assert_equal 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"))
57
+ assert_url_parts expected_url("address=50+Castilian+Drive%2C+Goleta%2C+CA&sensor=true"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => true, :ssl => false, :address => "50 Castilian Drive, Goleta, CA"))
58
+ 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"))
59
+ end
60
+
61
+ def test_url__with_client_id
62
+ GoogleMaps.key = "bar"
63
+ GoogleMaps.enterprise_account = true
64
+ assert_url_parts expected_url("address=A&sensor=false&clientId=foo"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A", :clientId => 'foo'))
65
+ assert_url_parts expected_url("address=A&sensor=false&clientId=bar"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A"))
66
+ #assert_equal expected_url("address=A&sensor=false&clientId=foo"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A", :clientId => 'foo'))
67
+ #
57
68
  end
58
69
 
59
70
  private
60
71
 
72
+ def assert_url_parts(expected, actual)
73
+ expected_base_url, expected_url_params = expected.split('?')
74
+ actual_base_url, actual_url_params = actual.split('?')
75
+ assert_equal expected_base_url, actual_base_url
76
+
77
+ expected_params = url_params(expected_url_params)
78
+ actual_params = url_params(actual_url_params)
79
+ assert_equal expected_params, actual_params
80
+ end
81
+
82
+ def url_params(params)
83
+ {}.tap do |hash|
84
+ params.split('&').each do |param|
85
+ hash[param.split('=').first] = param.split('=').last
86
+ end
87
+ end
88
+ end
89
+
61
90
  def expected_url(parameters, ssl = false)
62
91
  "http#{'s' if ssl}://#{GoogleMaps::Geocoder::URI_BASE}?#{parameters}"
63
92
  end
64
93
 
65
94
  def ordered_hash(hash)
66
95
  oh = ActiveSupport::OrderedHash.new
96
+ oh[:address] = hash[:address] if hash[:address]
97
+ oh[:sensor] = hash[:sensor] if hash[:sensor]
98
+ oh[:clientId] = hash[:clientId] if hash[:clientId]
67
99
  oh.merge!(hash)
68
100
  oh
69
101
  end
data/test/test_helper.rb CHANGED
@@ -16,7 +16,15 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
16
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
17
  require 'google_maps'
18
18
 
19
- class Test::Unit::TestCase
19
+ class ActiveSupport::TestCase
20
+
21
+ setup :clear_configuration
22
+
23
+ def clear_configuration
24
+ GoogleMaps.enterprise_account = nil
25
+ GoogleMaps.key = nil
26
+ end
27
+
20
28
  def castilian_json
21
29
  "{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"50\",\n \"short_name\" : \"50\",\n \"types\" : [ \"street_number\" ]\n },\n {\n \"long_name\" : \"Castilian Dr\",\n \"short_name\" : \"Castilian Dr\",\n \"types\" : [ \"route\" ]\n },\n {\n \"long_name\" : \"Goleta\",\n \"short_name\" : \"Goleta\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"Santa Barbara\",\n \"short_name\" : \"Santa Barbara\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"California\",\n \"short_name\" : \"CA\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n },\n {\n \"long_name\" : \"93117\",\n \"short_name\" : \"93117\",\n \"types\" : [ \"postal_code\" ]\n }\n ],\n \"formatted_address\" : \"50 Castilian Dr, Goleta, CA 93117, USA\",\n \"geometry\" : {\n \"location\" : {\n \"lat\" : 34.43447590,\n \"lng\" : -119.8639080\n },\n \"location_type\" : \"ROOFTOP\",\n \"viewport\" : {\n \"northeast\" : {\n \"lat\" : 34.43582488029149,\n \"lng\" : -119.8625590197085\n },\n \"southwest\" : {\n \"lat\" : 34.43312691970849,\n \"lng\" : -119.8652569802915\n }\n }\n },\n \"types\" : [ \"street_address\" ]\n }\n ],\n \"status\" : \"OK\"\n}\n"
22
30
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tushar Ranka