google-api-customization 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/lib/google_api_customization.rb +22 -5
- data/lib/google_api_customization/place.rb +207 -0
- data/lib/google_api_customization/request.rb +95 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODkxM2MzMjkzMzBlODg1MTg0OTVkMjRlNDY3Yjg1ZWI1YmFhMDhkZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGNmMjAxMWJjYzE0NzU4MTc0MmMyYzYxMDVjZmQzNDMwZTFhNjNjZA==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjgwYjI0YTUzYjRmYTFjZTRlOTViYzk0OTgzNWUwN2RhMGUyYjkzY2Y1ZWRj
|
10
|
+
OTRlM2NkMmY5MWFhZGJkYTE1OTM0N2U0MjRkMDc5ODM4ZWUxMjk2MTc1NjBj
|
11
|
+
NjExODAxZmUzOTIzNjk5ZDMyODU2MTc2M2NlZTNmODg2MzI2MGQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjljOGYwODRlNDhkYzQzMTE5NGU5MWU5M2NiOWVlNGI1ZjYwOGVjYjUxOTQy
|
14
|
+
MmI1YjQyMjI2Y2Q5OWIyOWVhY2M1MTU3ZDkwY2Q1NTRiNWM2NTI4ODZiZTIy
|
15
|
+
YTBkYWMwODMzYmM3ZGEyMzk2NWM2YTM0YTM5OGJlN2ZiYWRhMDk=
|
@@ -1,7 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
require 'debugger'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
%w(request place api_key photo review).each do |file|
|
6
|
+
require File.join(File.dirname(__FILE__), 'google_api_customization', file)
|
6
7
|
end
|
7
8
|
|
9
|
+
|
10
|
+
module GoogleApiCustomization
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
attr_accessor :api_key
|
15
|
+
|
16
|
+
def configuration
|
17
|
+
|
18
|
+
yield self
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
module GoogleApiCustomization
|
2
|
+
|
3
|
+
attr_accessor :lat, :lng, :viewport, :name, :icon, :reference, :vicinity, :types, :id, :formatted_phone_number, :international_phone_number, :formatted_address, :address_components, :street_number, :street, :city, :region, :postal_code, :country, :rating, :url, :cid, :website, :reviews, :aspects, :zagat_selected, :zagat_reviewed, :photos, :review_summary, :nextpagetoken, :price_level, :opening_hours, :events, :utc_offset, :place_id
|
4
|
+
|
5
|
+
class Place
|
6
|
+
|
7
|
+
|
8
|
+
def self.find(place_id, api_key, sensor, options = {})
|
9
|
+
language = options.delete(:language)
|
10
|
+
retry_options = options.delete(:retry_options) || {}
|
11
|
+
extensions = options.delete(:review_summary) ? 'review_summary' : nil
|
12
|
+
|
13
|
+
response = Request.place(
|
14
|
+
{
|
15
|
+
:placeid => place_id,
|
16
|
+
:sensor => sensor,
|
17
|
+
:key => api_key,
|
18
|
+
:language => language,
|
19
|
+
:extensions => extensions,
|
20
|
+
:retry_options => retry_options
|
21
|
+
}
|
22
|
+
)
|
23
|
+
|
24
|
+
self.new(response['result'], api_key, sensor)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# Search for Spots with a pagetoken
|
29
|
+
#
|
30
|
+
# @return [Array<Spot>]
|
31
|
+
# @param [String] pagetoken the token to find next results
|
32
|
+
# @param [String] api_key the provided api key
|
33
|
+
# @param [Boolean] sensor
|
34
|
+
# @param [Hash] options
|
35
|
+
def self.list_by_pagetoken(pagetoken, api_key, sensor, options = {})
|
36
|
+
exclude = options.delete(:exclude) || []
|
37
|
+
exclude = [exclude] unless exclude.is_a?(Array)
|
38
|
+
|
39
|
+
options = {
|
40
|
+
:pagetoken => pagetoken,
|
41
|
+
:sensor => sensor,
|
42
|
+
:key => api_key
|
43
|
+
}
|
44
|
+
|
45
|
+
request(:spots_by_pagetoken, false, exclude, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def self.list_by_query(query, api_key, sensor, options = {})
|
50
|
+
if options.has_key?(:lat) && options.has_key?(:lng)
|
51
|
+
with_location = true
|
52
|
+
else
|
53
|
+
with_location = false
|
54
|
+
end
|
55
|
+
|
56
|
+
if options.has_key?(:radius)
|
57
|
+
with_radius = true
|
58
|
+
else
|
59
|
+
with_radius = false
|
60
|
+
end
|
61
|
+
|
62
|
+
query = query
|
63
|
+
sensor = sensor
|
64
|
+
multipage_request = !!options.delete(:multipage)
|
65
|
+
location = Location.new(options.delete(:lat), options.delete(:lng)) if with_location
|
66
|
+
radius = options.delete(:radius) if with_radius
|
67
|
+
rankby = options.delete(:rankby)
|
68
|
+
language = options.delete(:language)
|
69
|
+
types = options.delete(:types)
|
70
|
+
exclude = options.delete(:exclude) || []
|
71
|
+
retry_options = options.delete(:retry_options) || {}
|
72
|
+
|
73
|
+
exclude = [exclude] unless exclude.is_a?(Array)
|
74
|
+
|
75
|
+
options = {
|
76
|
+
:query => query,
|
77
|
+
:sensor => sensor,
|
78
|
+
:key => api_key,
|
79
|
+
:rankby => rankby,
|
80
|
+
:language => language,
|
81
|
+
:retry_options => retry_options
|
82
|
+
}
|
83
|
+
|
84
|
+
options[:location] = location.format if with_location
|
85
|
+
options[:radius] = radius if with_radius
|
86
|
+
|
87
|
+
# Accept Types as a string or array
|
88
|
+
if types
|
89
|
+
types = (types.is_a?(Array) ? types.join('|') : types)
|
90
|
+
options.merge!(:types => types)
|
91
|
+
end
|
92
|
+
|
93
|
+
request(:spots_by_query, multipage_request, exclude, options)
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.request(method, multipage_request, exclude, options)
|
97
|
+
results = []
|
98
|
+
|
99
|
+
self.multi_pages_request(method, multipage_request, options) do |result|
|
100
|
+
# Some places returned by Google do not have a 'types' property. If the user specified 'types', then
|
101
|
+
# this is a non-issue because those places will not be returned. However, if the user did not specify
|
102
|
+
# 'types', then we do not want to filter out places with a missing 'types' property from the results set.
|
103
|
+
results << self.new(result, options[:key], options[:sensor]) if result['types'].nil? || (result['types'] & exclude) == []
|
104
|
+
end
|
105
|
+
|
106
|
+
results
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def initialize(json_result_object, api_key, sensor)
|
112
|
+
@reference = json_result_object['reference']
|
113
|
+
@place_id = json_result_object['place_id']
|
114
|
+
@vicinity = json_result_object['vicinity']
|
115
|
+
@lat = json_result_object['geometry']['location']['lat']
|
116
|
+
@lng = json_result_object['geometry']['location']['lng']
|
117
|
+
@viewport = json_result_object['geometry']['viewport']
|
118
|
+
@name = json_result_object['name']
|
119
|
+
@icon = json_result_object['icon']
|
120
|
+
@types = json_result_object['types']
|
121
|
+
@id = json_result_object['id']
|
122
|
+
@formatted_phone_number = json_result_object['formatted_phone_number']
|
123
|
+
@international_phone_number = json_result_object['international_phone_number']
|
124
|
+
@formatted_address = json_result_object['formatted_address']
|
125
|
+
@address_components = json_result_object['address_components']
|
126
|
+
@street_number = address_component(:street_number, 'short_name')
|
127
|
+
@street = address_component(:route, 'long_name')
|
128
|
+
@city = address_component(:locality, 'long_name')
|
129
|
+
@region = address_component(:administrative_area_level_1, 'long_name')
|
130
|
+
@postal_code = address_component(:postal_code, 'long_name')
|
131
|
+
@country = address_component(:country, 'long_name')
|
132
|
+
@rating = json_result_object['rating']
|
133
|
+
@price_level = json_result_object['price_level']
|
134
|
+
@opening_hours = json_result_object['opening_hours']
|
135
|
+
@url = json_result_object['url']
|
136
|
+
@cid = json_result_object['url'].to_i
|
137
|
+
@website = json_result_object['website']
|
138
|
+
@zagat_reviewed = json_result_object['zagat_reviewed']
|
139
|
+
@zagat_selected = json_result_object['zagat_selected']
|
140
|
+
@aspects = aspects_component(json_result_object['aspects'])
|
141
|
+
@review_summary = json_result_object['review_summary']
|
142
|
+
@photos = photos_component(json_result_object['photos'], api_key, sensor)
|
143
|
+
@reviews = reviews_component(json_result_object['reviews'])
|
144
|
+
@nextpagetoken = json_result_object['nextpagetoken']
|
145
|
+
@events = events_component(json_result_object['events'])
|
146
|
+
@utc_offset = json_result_object['utc_offset']
|
147
|
+
end
|
148
|
+
|
149
|
+
def [] (key)
|
150
|
+
send(key)
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
def address_component(address_component_type, address_component_length)
|
157
|
+
if component = address_components_of_type(address_component_type)
|
158
|
+
component.first[address_component_length] unless component.first.nil?
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def address_components_of_type(type)
|
163
|
+
@address_components.select{ |c| c['types'].include?(type.to_s) } unless @address_components.nil?
|
164
|
+
end
|
165
|
+
|
166
|
+
def reviews_component(json_reviews)
|
167
|
+
if json_reviews
|
168
|
+
json_reviews.map { |r|
|
169
|
+
Review.new(
|
170
|
+
r['rating'],
|
171
|
+
r['type'],
|
172
|
+
r['author_name'],
|
173
|
+
r['author_url'],
|
174
|
+
r['text'],
|
175
|
+
r['time'].to_i
|
176
|
+
)
|
177
|
+
}
|
178
|
+
else []
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def aspects_component(json_aspects)
|
183
|
+
json_aspects.to_a.map{ |r| { :type => r['type'], :rating => r['rating'] } }
|
184
|
+
end
|
185
|
+
|
186
|
+
def photos_component(json_photos, api_key, sensor)
|
187
|
+
if json_photos
|
188
|
+
json_photos.map{ |p|
|
189
|
+
Photo.new(
|
190
|
+
p['width'],
|
191
|
+
p['height'],
|
192
|
+
p['photo_reference'],
|
193
|
+
p['html_attributions'],
|
194
|
+
api_key,
|
195
|
+
sensor
|
196
|
+
)
|
197
|
+
}
|
198
|
+
else []
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def events_component(json_events)
|
203
|
+
json_events.to_a.map{ |r| {:event_id => r['event_id'], :summary => r['summary'], :url => r['url'], :start_time => r['start_time']} }
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module GoogleApiCustomization
|
2
|
+
class Request
|
3
|
+
attr_accessor :response
|
4
|
+
attr_reader :options
|
5
|
+
|
6
|
+
include ::HTTParty
|
7
|
+
format :json
|
8
|
+
|
9
|
+
|
10
|
+
PLACES_URL = "https://maps.googleapis.com/maps/api/place/details/json"
|
11
|
+
|
12
|
+
RADAR_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/radarsearch/json"
|
13
|
+
|
14
|
+
TEXT_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
15
|
+
|
16
|
+
NEARBY_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
|
17
|
+
|
18
|
+
PHOTO_URL = "https://maps.googleapis.com/maps/api/place/photo"
|
19
|
+
|
20
|
+
PAGETOKEN_URL = "https://maps.googleapis.com/maps/api/place/search/json"
|
21
|
+
|
22
|
+
RADAR_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/radarsearch/json"
|
23
|
+
|
24
|
+
AUTOCOMPLETE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json"
|
25
|
+
|
26
|
+
def self.place(options = {})
|
27
|
+
request = new(PLACES_URL, options)
|
28
|
+
request.parsed_response
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(url, options, follow_redirects = true)
|
32
|
+
retry_options = options.delete(:retry_options) || {}
|
33
|
+
|
34
|
+
retry_options[:status] ||= []
|
35
|
+
retry_options[:max] ||= 0
|
36
|
+
retry_options[:delay] ||= 5
|
37
|
+
|
38
|
+
retry_options[:status] = [retry_options[:status]] unless retry_options[:status].is_a?(Array)
|
39
|
+
@response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
|
40
|
+
|
41
|
+
# puts @response.request.last_uri.to_s
|
42
|
+
|
43
|
+
return unless retry_options[:max] > 0 && retry_options[:status].include?(@response.parsed_response['status'])
|
44
|
+
|
45
|
+
retry_request = proc do
|
46
|
+
for i in (1..retry_options[:max])
|
47
|
+
sleep(retry_options[:delay])
|
48
|
+
|
49
|
+
@response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
|
50
|
+
|
51
|
+
break unless retry_options[:status].include?(@response.parsed_response['status'])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
if retry_options[:timeout]
|
56
|
+
begin
|
57
|
+
Timeout::timeout(retry_options[:timeout]) do
|
58
|
+
retry_request.call
|
59
|
+
end
|
60
|
+
rescue Timeout::Error
|
61
|
+
raise RetryTimeoutError.new(@response)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
retry_request.call
|
65
|
+
|
66
|
+
raise RetryError.new(@response) if retry_options[:status].include?(@response.parsed_response['status'])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def parsed_response
|
72
|
+
|
73
|
+
return @response.headers["location"] if @response.code >= 300 and @response.code < 400
|
74
|
+
case @response.parsed_response['status']
|
75
|
+
when 'OK', 'ZERO_RESULTS'
|
76
|
+
@response.parsed_response
|
77
|
+
when 'OVER_QUERY_LIMIT'
|
78
|
+
raise OverQueryLimitError.new(@response)
|
79
|
+
when 'REQUEST_DENIED'
|
80
|
+
raise RequestDeniedError.new(@response)
|
81
|
+
when 'INVALID_REQUEST'
|
82
|
+
raise InvalidRequestError.new(@response)
|
83
|
+
when 'UNKNOWN_ERROR'
|
84
|
+
raise UnknownError.new(@response)
|
85
|
+
when 'NOT_FOUND'
|
86
|
+
raise NotFoundError.new(@response)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-api-customization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abhishek Sharma
|
@@ -17,6 +17,8 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/google_api_customization.rb
|
20
|
+
- lib/google_api_customization/place.rb
|
21
|
+
- lib/google_api_customization/request.rb
|
20
22
|
homepage: http://rubygems.org/gems/google_api_customization
|
21
23
|
licenses:
|
22
24
|
- NONE
|
@@ -27,17 +29,17 @@ require_paths:
|
|
27
29
|
- lib
|
28
30
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
31
|
requirements:
|
30
|
-
- - '>='
|
32
|
+
- - ! '>='
|
31
33
|
- !ruby/object:Gem::Version
|
32
34
|
version: '0'
|
33
35
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
36
|
requirements:
|
35
|
-
- - '>='
|
37
|
+
- - ! '>='
|
36
38
|
- !ruby/object:Gem::Version
|
37
39
|
version: '0'
|
38
40
|
requirements: []
|
39
41
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
42
|
+
rubygems_version: 2.2.2
|
41
43
|
signing_key:
|
42
44
|
specification_version: 4
|
43
45
|
summary: Hello! This is start up for google API customization for Rails
|