google_places_autocomplete 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +30 -0
- data/Rakefile +7 -0
- data/google_places_autocomplete.gemspec +27 -0
- data/lib/google_places_autocomplete.rb +16 -0
- data/lib/google_places_autocomplete/client.rb +41 -0
- data/lib/google_places_autocomplete/version.rb +3 -0
- data/spec/google_places_autocomplete/client_spec.rb +21 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/vcr_cassettes/autocomplete.yml +188 -0
- data/spec/vcr_setup.rb +8 -0
- metadata +150 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Google Places Autocomplete
|
2
|
+
|
3
|
+
Ruby wrapper for the [Google Places Autocomplete API](http://code.google.com/apis/maps/documentation/places/autocomplete.html).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Inside your Gemfile:
|
8
|
+
gem 'google_places_autocomplete'
|
9
|
+
|
10
|
+
## Get Google Places API credentials
|
11
|
+
|
12
|
+
Go here and activate it: [https://code.google.com/apis/console](https://code.google.com/apis/console)
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
### Instantiate a client
|
17
|
+
|
18
|
+
>> client = GooglePlacesAutocomplete::Client.new(:api_key => 'your_api_key')
|
19
|
+
|
20
|
+
#### Example
|
21
|
+
|
22
|
+
>> autocomplete = client.autocomplete(:input => "Paris", :types => "geocode")
|
23
|
+
>> autocomplete.predictions.first.description
|
24
|
+
=> "Paris, France"
|
25
|
+
|
26
|
+
## Copyright
|
27
|
+
|
28
|
+
Contact me if you have any suggestions and feel free to fork it!
|
29
|
+
|
30
|
+
Copyright (c) 2009 Johnny Khai Nguyen, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "google_places_autocomplete/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "google_places_autocomplete"
|
7
|
+
s.version = GooglePlacesAutocomplete::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = "Johnny Khai Nguyen"
|
10
|
+
s.email = "johnnyn@gmail.com"
|
11
|
+
s.homepage = "http://github.com/phuphighter/google_places_autocomplete"
|
12
|
+
s.summary = %q{Ruby wrapper for the Google Places Autocomplete API}
|
13
|
+
s.description = %q{Use this gem to access the Google Places Autocomplete API from your Ruby application}
|
14
|
+
|
15
|
+
s.rubyforge_project = "google_places_autocomplete"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'httparty'
|
23
|
+
s.add_dependency 'hashie'
|
24
|
+
s.add_development_dependency 'rspec'
|
25
|
+
s.add_development_dependency 'fakeweb'
|
26
|
+
s.add_development_dependency 'vcr'
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'httparty'
|
3
|
+
require 'hashie'
|
4
|
+
require File.join(File.dirname(__FILE__), 'google_places_autocomplete', 'client')
|
5
|
+
|
6
|
+
module GooglePlacesAutocomplete
|
7
|
+
def self.configure
|
8
|
+
yield self
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :api_key
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module GooglePlacesAutocomplete
|
2
|
+
|
3
|
+
class Client
|
4
|
+
include HTTParty
|
5
|
+
base_uri "https://maps.googleapis.com/maps/api/place/autocomplete"
|
6
|
+
format :json
|
7
|
+
|
8
|
+
attr_reader :api_key
|
9
|
+
|
10
|
+
def initialize(options={})
|
11
|
+
@api_key = options[:api_key] || GooglePlaces.api_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def autocomplete(options={})
|
15
|
+
query = options.merge(self.default_options)
|
16
|
+
mashup(self.class.get("/json", :query => query))
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def default_options
|
22
|
+
{ :sensor => false, :key => @api_key }
|
23
|
+
end
|
24
|
+
|
25
|
+
def mashup(response)
|
26
|
+
case response.code
|
27
|
+
when 200
|
28
|
+
if response.is_a?(Hash)
|
29
|
+
Hashie::Mash.new(response)
|
30
|
+
else
|
31
|
+
if response.first.is_a?(Hash)
|
32
|
+
response.map{|item| Hashie::Mash.new(item)}
|
33
|
+
else
|
34
|
+
response
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GooglePlacesAutocomplete::Client do
|
4
|
+
use_vcr_cassette 'autocomplete'
|
5
|
+
|
6
|
+
it 'should not initialize without an api_key' do
|
7
|
+
lambda { GooglePlacesAutocomplete::Client.new }.should raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should initialize with an api_key' do
|
11
|
+
@client = GooglePlacesAutocomplete::Client.new(:api_key => "foobar")
|
12
|
+
@client.api_key.should == "foobar"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should request autocomplete' do
|
16
|
+
@client = GooglePlacesAutocomplete::Client.new(:api_key => "foobar")
|
17
|
+
@autocomplete = @client.autocomplete(:input => "Paris", :types => "geocode")
|
18
|
+
@autocomplete.predictions.size.should == 5
|
19
|
+
@autocomplete.predictions.first.description.should == 'Paris, France'
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
---
|
2
|
+
- !ruby/struct:VCR::HTTPInteraction
|
3
|
+
request: !ruby/struct:VCR::Request
|
4
|
+
method: :get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Paris&types=geocode&sensor=false&key=foobar
|
6
|
+
body:
|
7
|
+
headers:
|
8
|
+
response: !ruby/struct:VCR::Response
|
9
|
+
status: !ruby/struct:VCR::ResponseStatus
|
10
|
+
code: 200
|
11
|
+
message: OK
|
12
|
+
headers:
|
13
|
+
content-type:
|
14
|
+
- application/json; charset=UTF-8
|
15
|
+
date:
|
16
|
+
- Thu, 02 Jun 2011 08:17:49 GMT
|
17
|
+
server:
|
18
|
+
- mafe
|
19
|
+
x-xss-protection:
|
20
|
+
- 1; mode=block
|
21
|
+
vary:
|
22
|
+
- Accept-Language
|
23
|
+
cache-control:
|
24
|
+
- private
|
25
|
+
body: |
|
26
|
+
{
|
27
|
+
"predictions" : [
|
28
|
+
{
|
29
|
+
"description" : "Paris, France",
|
30
|
+
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
|
31
|
+
"matched_substrings" : [
|
32
|
+
{
|
33
|
+
"length" : 5,
|
34
|
+
"offset" : 0
|
35
|
+
}
|
36
|
+
],
|
37
|
+
"reference" : "CiQRAAAABKKBeaZo_48lzvdI44WY5ub1Yp4FrvekI3Skd_xUqXcSEMmhuofMyZBl7rLil8HgM1caFJYxEEm1X2I9s0aYvDXkoHWp9gPz",
|
38
|
+
"terms" : [
|
39
|
+
{
|
40
|
+
"offset" : 0,
|
41
|
+
"value" : "Paris"
|
42
|
+
},
|
43
|
+
{
|
44
|
+
"offset" : 7,
|
45
|
+
"value" : "France"
|
46
|
+
}
|
47
|
+
],
|
48
|
+
"types" : [ "locality", "political", "geocode" ]
|
49
|
+
},
|
50
|
+
{
|
51
|
+
"description" : "Paris, TX, United States",
|
52
|
+
"id" : "029556239a911839382f42ec36c5ce2b85be9be3",
|
53
|
+
"matched_substrings" : [
|
54
|
+
{
|
55
|
+
"length" : 5,
|
56
|
+
"offset" : 0
|
57
|
+
}
|
58
|
+
],
|
59
|
+
"reference" : "CiQcAAAAFtMJRdTMaY7xtix8NOkmIylHPuv3HjhqBejTmtlFDwESEAvlV6Mr2a9RujKHsr7AitcaFAANLsvbmNDyLOsp7EqBzDm53__-",
|
60
|
+
"terms" : [
|
61
|
+
{
|
62
|
+
"offset" : 0,
|
63
|
+
"value" : "Paris"
|
64
|
+
},
|
65
|
+
{
|
66
|
+
"offset" : 7,
|
67
|
+
"value" : "TX"
|
68
|
+
},
|
69
|
+
{
|
70
|
+
"offset" : 11,
|
71
|
+
"value" : "United States"
|
72
|
+
}
|
73
|
+
],
|
74
|
+
"types" : [ "colloquial_area", "political", "geocode" ]
|
75
|
+
},
|
76
|
+
{
|
77
|
+
"description" : "Paris, Ontario, Canada",
|
78
|
+
"id" : "e7ac9c89d4a590305242b0cb5bf43064027223c9",
|
79
|
+
"matched_substrings" : [
|
80
|
+
{
|
81
|
+
"length" : 5,
|
82
|
+
"offset" : 0
|
83
|
+
}
|
84
|
+
],
|
85
|
+
"reference" : "CiQaAAAAcLp3QE69VyHaR9XT7ODuZqFho4Vk5LZeGjmXEVAHiN8SEAeAx_grOXH5GE31SrFleeUaFLqkH4GmjN2_AvobKzRbAL5ZWzhe",
|
86
|
+
"terms" : [
|
87
|
+
{
|
88
|
+
"offset" : 0,
|
89
|
+
"value" : "Paris"
|
90
|
+
},
|
91
|
+
{
|
92
|
+
"offset" : 7,
|
93
|
+
"value" : "Ontario"
|
94
|
+
},
|
95
|
+
{
|
96
|
+
"offset" : 16,
|
97
|
+
"value" : "Canada"
|
98
|
+
}
|
99
|
+
],
|
100
|
+
"types" : [ "locality", "political", "geocode" ]
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"description" : "Paris, TN, United States",
|
104
|
+
"id" : "08e83eafcf8efd2611bf476f0a1b9a2894c1a813",
|
105
|
+
"matched_substrings" : [
|
106
|
+
{
|
107
|
+
"length" : 5,
|
108
|
+
"offset" : 0
|
109
|
+
}
|
110
|
+
],
|
111
|
+
"reference" : "CiQcAAAA7FOymrF0Q5h7VReIYG7a2hTAOYzD3RnPOfXTDoZvJs0SELdLSpaTFqIh7lKDdkEXBo8aFNE7v4bKA6aXwpJFuZ1vqAY8owL5",
|
112
|
+
"terms" : [
|
113
|
+
{
|
114
|
+
"offset" : 0,
|
115
|
+
"value" : "Paris"
|
116
|
+
},
|
117
|
+
{
|
118
|
+
"offset" : 7,
|
119
|
+
"value" : "TN"
|
120
|
+
},
|
121
|
+
{
|
122
|
+
"offset" : 11,
|
123
|
+
"value" : "United States"
|
124
|
+
}
|
125
|
+
],
|
126
|
+
"types" : [ "colloquial_area", "political", "geocode" ]
|
127
|
+
},
|
128
|
+
{
|
129
|
+
"description" : "Paris, KY, United States",
|
130
|
+
"id" : "e2522e18d49652387de0a8c030b09ed9fb3251b8",
|
131
|
+
"matched_substrings" : [
|
132
|
+
{
|
133
|
+
"length" : 5,
|
134
|
+
"offset" : 0
|
135
|
+
}
|
136
|
+
],
|
137
|
+
"reference" : "CiQcAAAAMjXaIUMs2Oucs8gW_A0XNtdIWVTZzmLfI3_CRP4ni0cSEL2yxLQRKDZLUwNr2Ljzbi0aFEyt6i3PibB7Qvg_1bbR9RvxCdwT",
|
138
|
+
"terms" : [
|
139
|
+
{
|
140
|
+
"offset" : 0,
|
141
|
+
"value" : "Paris"
|
142
|
+
},
|
143
|
+
{
|
144
|
+
"offset" : 7,
|
145
|
+
"value" : "KY"
|
146
|
+
},
|
147
|
+
{
|
148
|
+
"offset" : 11,
|
149
|
+
"value" : "United States"
|
150
|
+
}
|
151
|
+
],
|
152
|
+
"types" : [ "locality", "political", "geocode" ]
|
153
|
+
}
|
154
|
+
],
|
155
|
+
"status" : "OK"
|
156
|
+
}
|
157
|
+
|
158
|
+
http_version: "1.1"
|
159
|
+
- !ruby/struct:VCR::HTTPInteraction
|
160
|
+
request: !ruby/struct:VCR::Request
|
161
|
+
method: :get
|
162
|
+
uri: https://maps.googleapis.com:443/maps/api/place/autocomplete/json?types=geocode&input=Paris&sensor=false&key=foobar
|
163
|
+
body:
|
164
|
+
headers:
|
165
|
+
response: !ruby/struct:VCR::Response
|
166
|
+
status: !ruby/struct:VCR::ResponseStatus
|
167
|
+
code: 200
|
168
|
+
message: OK
|
169
|
+
headers:
|
170
|
+
content-type:
|
171
|
+
- application/json; charset=UTF-8
|
172
|
+
x-xss-protection:
|
173
|
+
- 1; mode=block
|
174
|
+
server:
|
175
|
+
- mafe
|
176
|
+
date:
|
177
|
+
- Fri, 03 Jun 2011 01:41:55 GMT
|
178
|
+
cache-control:
|
179
|
+
- private
|
180
|
+
vary:
|
181
|
+
- Accept-Language
|
182
|
+
body: |
|
183
|
+
{
|
184
|
+
"predictions" : [],
|
185
|
+
"status" : "REQUEST_DENIED"
|
186
|
+
}
|
187
|
+
|
188
|
+
http_version: "1.1"
|
data/spec/vcr_setup.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_places_autocomplete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Johnny Khai Nguyen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-02 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: httparty
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hashie
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: fakeweb
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: vcr
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
type: :development
|
90
|
+
version_requirements: *id005
|
91
|
+
description: Use this gem to access the Google Places Autocomplete API from your Ruby application
|
92
|
+
email: johnnyn@gmail.com
|
93
|
+
executables: []
|
94
|
+
|
95
|
+
extensions: []
|
96
|
+
|
97
|
+
extra_rdoc_files: []
|
98
|
+
|
99
|
+
files:
|
100
|
+
- .gitignore
|
101
|
+
- Gemfile
|
102
|
+
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- google_places_autocomplete.gemspec
|
105
|
+
- lib/google_places_autocomplete.rb
|
106
|
+
- lib/google_places_autocomplete/client.rb
|
107
|
+
- lib/google_places_autocomplete/version.rb
|
108
|
+
- spec/google_places_autocomplete/client_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/vcr_cassettes/autocomplete.yml
|
111
|
+
- spec/vcr_setup.rb
|
112
|
+
has_rdoc: true
|
113
|
+
homepage: http://github.com/phuphighter/google_places_autocomplete
|
114
|
+
licenses: []
|
115
|
+
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 3
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project: google_places_autocomplete
|
142
|
+
rubygems_version: 1.6.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Ruby wrapper for the Google Places Autocomplete API
|
146
|
+
test_files:
|
147
|
+
- spec/google_places_autocomplete/client_spec.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
- spec/vcr_cassettes/autocomplete.yml
|
150
|
+
- spec/vcr_setup.rb
|