gcoder 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gcoder (0.12.0)
5
+ hashie
6
+ ruby-hmac
7
+ yajl-ruby
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ hashie (1.0.0)
13
+ ruby-hmac (0.4.0)
14
+ yajl-ruby (0.8.2)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ gcoder!
data/README.md CHANGED
@@ -52,6 +52,11 @@ More info [here](http://code.google.com/apis/maps/documentation/geocoding/#Viewp
52
52
  To access the special features of Google Maps API Premier, you must provide a
53
53
  client ID. All client IDs begin with a gme- prefix.
54
54
 
55
+ ### `:key`
56
+
57
+ To access the special features of Google Maps API Premier, you must provide a
58
+ signing key in addition to the client ID.
59
+
55
60
  ### `:storage`
56
61
 
57
62
  Defines the storage adapter to use for caching results from the geocoder to
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems/specification' unless defined?(Gem::Specification)
2
- require 'rake/gempackagetask'
2
+ require 'rubygems/package_task'
3
3
  require 'rake/testtask'
4
4
 
5
5
  def gemspec
@@ -35,7 +35,7 @@ task :release => :package do
35
35
  sh "gem push pkg/#{gemspec.name}-#{gemspec.version}.gem"
36
36
  end
37
37
 
38
- Rake::GemPackageTask.new(gemspec) do |pkg|
38
+ Gem::PackageTask.new(gemspec) do |pkg|
39
39
  pkg.gem_spec = gemspec
40
40
  end
41
41
  task :gem => :gemspec
data/gcoder.gemspec CHANGED
@@ -11,11 +11,12 @@ Gem::Specification.new do |s|
11
11
  s.summary = %q{A nice library for geocoding stuff with Google Maps API V3}
12
12
  s.description = %q{Uses Google Maps Geocoding API (V3) to geocode stuff and optionally caches the results somewhere.}
13
13
 
14
- s.required_rubygems_version = '>= 1.3.6'
14
+ s.required_rubygems_version = '>= 1.8.5'
15
15
  s.rubyforge_project = 'gcoder'
16
16
 
17
17
  s.add_dependency 'hashie'
18
18
  s.add_dependency 'yajl-ruby'
19
+ s.add_dependency 'ruby-hmac'
19
20
 
20
21
  s.files = `git ls-files`.split(?\n)
21
22
  s.test_files = `git ls-files -- {test,spec}/*`.split(?\n)
data/lib/gcoder.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  require 'yajl'
2
2
  require 'hashie'
3
3
  require 'net/http'
4
+ require 'uri'
5
+ require 'base64'
4
6
  require 'timeout'
5
7
  require 'digest/sha1'
8
+ require 'hmac'
9
+ require 'hmac-sha1'
6
10
 
7
11
  $:.unshift(File.dirname(__FILE__))
8
12
 
@@ -24,6 +28,8 @@ module GCoder
24
28
  :append => nil,
25
29
  :region => nil,
26
30
  :bounds => nil,
31
+ :client => nil,
32
+ :key => nil,
27
33
  :storage => nil,
28
34
  :storage_config => nil
29
35
  }.freeze
@@ -5,14 +5,9 @@ module GCoder
5
5
  PATH = '/maps/api/geocode/json'
6
6
 
7
7
  class Request
8
- def self.u(string)
9
- string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
10
- '%' + $1.unpack('H2' * $1.size).join('%').upcase
11
- }.tr(' ', '+')
12
- end
13
8
 
14
9
  def self.to_query(params)
15
- params.map { |key, val| "#{u key}=#{u val}" }.join('&')
10
+ URI.escape(params.map { |key, val| "#{key}=#{val}" }.join('&'))
16
11
  end
17
12
 
18
13
  def self.stubs
@@ -30,17 +25,17 @@ module GCoder
30
25
 
31
26
  def params
32
27
  p = { :sensor => 'false' }
33
- p[:address] = address if @address
34
- p[:latlng] = latlng if @latlng
35
- p[:language] = @config[:language] if @config[:language]
36
- p[:region] = @config[:region] if @config[:region]
37
- p[:bounds] = bounds if @config[:bounds]
38
- p[:client] = @config[:client] if @config[:client]
28
+ p[:address] = address if @address
29
+ p[:latlng] = latlng if @latlng
30
+ p[:language] = @config[:language] if @config[:language]
31
+ p[:region] = @config[:region] if @config[:region]
32
+ p[:bounds] = bounds if @config[:bounds]
33
+ p[:client] = @config[:client] if @config[:client]
39
34
  p
40
35
  end
41
36
 
42
37
  def path
43
- "#{PATH}?#{self.class.to_query(params)}"
38
+ "#{PATH}?#{self.class.to_query(params)}#{"&signature=#{sign_key(@config[:key])}" if @config[:key]}"
44
39
  end
45
40
 
46
41
  def uri
@@ -97,8 +92,23 @@ module GCoder
97
92
  def address
98
93
  @config[:append] ? "#{@address} #{@config[:append]}" : @address
99
94
  end
100
- end
95
+
96
+ def url_safe_base64_decode(string)
97
+ return Base64.decode64(string.tr('-_','+/')).gsub(/\n/,'')
98
+ end
101
99
 
100
+ def url_safe_base64_encode(raw)
101
+ return Base64.encode64(raw).tr('+/','-_').gsub(/\n/,'')
102
+ end
103
+
104
+ def sign_key(key)
105
+ sha1 = HMAC::SHA1.new(url_safe_base64_decode(key))
106
+ sha1 << "#{PATH}?#{self.class.to_query(params)}"
107
+ raw_signature = sha1.digest()
108
+
109
+ url_safe_base64_encode(raw_signature)
110
+ end
111
+ end
102
112
 
103
113
  class Response
104
114
  attr_reader :uri, :data
@@ -1,3 +1,3 @@
1
1
  module GCoder
2
- VERSION = '0.11.0'
2
+ VERSION = '0.12.0'
3
3
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'spec_helper'
2
3
 
3
4
  describe GCoder::Geocoder::Request do
@@ -23,12 +24,18 @@ describe GCoder::Geocoder::Request do
23
24
  end
24
25
 
25
26
  it 'should URI encode a string' do
26
- GCoder::Geocoder::Request.u('hello world').must_equal 'hello+world'
27
+ q = GCoder::Geocoder::Request.to_query(:q => 'hello world', :a => 'test')
28
+ q.must_equal 'q=hello%20world&a=test'
29
+ end
30
+
31
+ it 'should URI encode a UTF-8 string' do
32
+ q = GCoder::Geocoder::Request.to_query(:q => 'मुंबई', :a => 'test')
33
+ q.must_equal 'q=%E0%A4%AE%E0%A5%81%E0%A4%82%E0%A4%AC%E0%A4%88&a=test'
27
34
  end
28
35
 
29
36
  it 'should create a query string' do
30
37
  q = GCoder::Geocoder::Request.to_query(:q => 'hello world', :a => 'test')
31
- q.must_equal 'q=hello+world&a=test'
38
+ q.must_equal 'q=hello%20world&a=test'
32
39
  end
33
40
 
34
41
  it '(when passed a bounds option) should generate correct query params' do
@@ -50,4 +57,10 @@ describe GCoder::Geocoder::Request do
50
57
  req.params[:address].must_equal 'queen and spadina'
51
58
  end
52
59
  end
60
+
61
+ it "(when passed a premier client and key) should generate correct query params" do
62
+ q = GCoder::Geocoder::Request.new('los angeles', :client => "gme-test", :key => "zNf23lb-YIoD4kEFN34C6324cww=").path
63
+ q.must_equal '/maps/api/geocode/json?sensor=false&address=los%20angeles&client=gme-test&signature=p0EiRD298eLhlGTqlBA5RKxv448='
64
+ end
65
+
53
66
  end
@@ -15,4 +15,4 @@
15
15
  :uri: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=overlimit
16
16
 
17
17
  - :file: 6.json
18
- :uri: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=invalid
18
+ :uri: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=invalid
metadata CHANGED
@@ -1,61 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gcoder
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 11
8
- - 0
9
- version: 0.11.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.12.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Carsten Nielsen
13
9
  - Christos Pappas
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2011-02-17 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
13
+ date: 2011-07-15 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: hashie
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2160555480 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- segments:
30
- - 0
31
- version: "0"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
32
23
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
24
+ prerelease: false
25
+ version_requirements: *2160555480
26
+ - !ruby/object:Gem::Dependency
35
27
  name: yajl-ruby
28
+ requirement: &2160554660 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
36
35
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
36
+ version_requirements: *2160554660
37
+ - !ruby/object:Gem::Dependency
38
+ name: ruby-hmac
39
+ requirement: &2160553960 !ruby/object:Gem::Requirement
38
40
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- segments:
43
- - 0
44
- version: "0"
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
45
  type: :runtime
46
- version_requirements: *id002
47
- description: Uses Google Maps Geocoding API (V3) to geocode stuff and optionally caches the results somewhere.
48
- email:
46
+ prerelease: false
47
+ version_requirements: *2160553960
48
+ description: Uses Google Maps Geocoding API (V3) to geocode stuff and optionally caches
49
+ the results somewhere.
50
+ email:
49
51
  - heycarsten@gmail.com
50
52
  executables: []
51
-
52
53
  extensions: []
53
-
54
54
  extra_rdoc_files: []
55
-
56
- files:
55
+ files:
57
56
  - .gitignore
58
57
  - Gemfile
58
+ - Gemfile.lock
59
59
  - LICENSE
60
60
  - README.md
61
61
  - Rakefile
@@ -77,41 +77,31 @@ files:
77
77
  - spec/support/requests/4.json
78
78
  - spec/support/requests/5.json
79
79
  - spec/support/requests/6.json
80
- has_rdoc: true
81
80
  homepage: http://github.com/heycarsten/gcoder
82
81
  licenses: []
83
-
84
82
  post_install_message:
85
83
  rdoc_options: []
86
-
87
- require_paths:
84
+ require_paths:
88
85
  - lib
89
- required_ruby_version: !ruby/object:Gem::Requirement
86
+ required_ruby_version: !ruby/object:Gem::Requirement
90
87
  none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- segments:
95
- - 0
96
- version: "0"
97
- required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
93
  none: false
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- segments:
103
- - 1
104
- - 3
105
- - 6
106
- version: 1.3.6
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: 1.8.5
107
98
  requirements: []
108
-
109
99
  rubyforge_project: gcoder
110
- rubygems_version: 1.3.7
100
+ rubygems_version: 1.8.5
111
101
  signing_key:
112
102
  specification_version: 3
113
103
  summary: A nice library for geocoding stuff with Google Maps API V3
114
- test_files:
104
+ test_files:
115
105
  - spec/gcoder/geocoder_spec.rb
116
106
  - spec/gcoder/resolver_spec.rb
117
107
  - spec/gcoder/storage_spec.rb