Class: GoogleApiCustomization::Place

Inherits:
Object
  • Object
show all
Defined in:
lib/google_api_customization/place.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_result_object, api_key, sensor) ⇒ Place

Returns a new instance of Place.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/google_api_customization/place.rb', line 140

def initialize(json_result_object, api_key, sensor)
  @reference                  = json_result_object['reference']
  @place_id                   = json_result_object['place_id']
  @vicinity                   = json_result_object['vicinity']
  @lat                        = json_result_object['geometry']['location']['lat']
  @lng                        = json_result_object['geometry']['location']['lng']
  @viewport                   = json_result_object['geometry']['viewport']
  @name                       = json_result_object['name']
  @icon                       = json_result_object['icon']
  @types                      = json_result_object['types']
  @id                         = json_result_object['id']
  @formatted_phone_number     = json_result_object['formatted_phone_number']
  @international_phone_number = json_result_object['international_phone_number']
  @formatted_address          = json_result_object['formatted_address']
  @address_components         = json_result_object['address_components']
  @street_number              = address_component(:street_number, 'short_name')
  @street                     = address_component(:route, 'long_name')
  @city                       = address_component(:locality, 'long_name')
  @region                     = address_component(:administrative_area_level_1, 'long_name')
  @postal_code                = address_component(:postal_code, 'long_name')
  @country                    = address_component(:country, 'long_name')
  @rating                     = json_result_object['rating']
  @price_level                = json_result_object['price_level']
  @opening_hours              = json_result_object['opening_hours']
  @url                        = json_result_object['url']
  @cid                        = json_result_object['url'].to_i
  @website                    = json_result_object['website']
  @zagat_reviewed             = json_result_object['zagat_reviewed']
  @zagat_selected             = json_result_object['zagat_selected']
  @aspects                    = aspects_component(json_result_object['aspects'])
  @review_summary             = json_result_object['review_summary']
  @photos                     = photos_component(json_result_object['photos'], api_key, sensor)
  @reviews                    = reviews_component(json_result_object['reviews'])
  @nextpagetoken              = json_result_object['nextpagetoken']
  @events                     = events_component(json_result_object['events'])
  @utc_offset                 = json_result_object['utc_offset']
end

Class Method Details

.find(place_id, api_key, sensor, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/google_api_customization/place.rb', line 8

def self.find(place_id, api_key, sensor, options = {})
  language  = options.delete(:language)
  retry_options = options.delete(:retry_options) || {}
  extensions = options.delete(:review_summary) ? 'review_summary' : nil
  
  response = Request.place(
                            {
                              :placeid => place_id,
                              :sensor => sensor,
                              :key => api_key,
                              :language => language,
                              :extensions => extensions,
                              :retry_options => retry_options
                            }
  )
  
  self.new(response['result'], api_key, sensor)
end

.list_by_nearby(lat, lng, radius, api_key, sensor, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/google_api_customization/place.rb', line 96

def self.list_by_nearby(lat, lng, radius, api_key, sensor, options = {})
  multipage_request = !!options.delete(:multipage)
  location = Location.new(lat, lng)
  rankby = options.delete(:rankby)
  language = options.delete(:language)
  types = options.delete(:types)
  exclude = options.delete(:exclude) || []
  retry_options = options.delete(:retry_options) || {}

  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
    :location => location.format,
    :radius   => radius,
    :sensor   => sensor,
    :key      => api_key,
    :rankby   => rankby,
    :language => language,
    :retry_options => retry_options
  }

  if types
    types = (types.is_a?(Array) ? types.join('|') : types)
    options.merge!(:types => types)
  end

  request(:spots_by_nearby, multipage_request, exclude, options)
end

.list_by_pagetoken(pagetoken, api_key, sensor, options = {}) ⇒ Array<Spot>

Search for Spots with a pagetoken

Parameters:

  • pagetoken (String)

    the token to find next results

  • api_key (String)

    the provided api key

  • sensor (Boolean)
  • options (Hash) (defaults to: {})

Returns:

  • (Array<Spot>)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/google_api_customization/place.rb', line 35

def self.list_by_pagetoken(pagetoken, api_key, sensor, options = {})
  exclude = options.delete(:exclude) || []
  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
      :pagetoken => pagetoken,
      :sensor => sensor,
      :key => api_key
  }

  request(:spots_by_pagetoken, false, exclude, options)
end

.list_by_query(query, api_key, sensor, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/google_api_customization/place.rb', line 49

def self.list_by_query(query, api_key, sensor, options = {})
  if options.has_key?(:lat) && options.has_key?(:lng)
    with_location = true
  else
    with_location = false
  end

  if options.has_key?(:radius)
    with_radius = true
  else
    with_radius = false
  end

  query = query
  sensor = sensor
  multipage_request = !!options.delete(:multipage)
  location = Location.new(options.delete(:lat), options.delete(:lng)) if with_location
  radius = options.delete(:radius) if with_radius
  rankby = options.delete(:rankby)
  language = options.delete(:language)
  types = options.delete(:types)
  exclude = options.delete(:exclude) || []
  retry_options = options.delete(:retry_options) || {}

  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
    :query => query,
    :sensor => sensor,
    :key => api_key,
    :rankby => rankby,
    :language => language,
    :retry_options => retry_options
  }

  options[:location] = location.format if with_location
  options[:radius] = radius if with_radius

  # Accept Types as a string or array
  if types
    types = (types.is_a?(Array) ? types.join('|') : types)
    options.merge!(:types => types)
  end

  request(:spots_by_query, multipage_request, exclude, options)
end

.request(method, multipage_request, exclude, options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/google_api_customization/place.rb', line 125

def self.request(method, multipage_request, exclude, options)
  results = []

  self.multi_pages_request(method, multipage_request, options) do |result|
    # Some places returned by Google do not have a 'types' property. If the user specified 'types', then
    # this is a non-issue because those places will not be returned. However, if the user did not specify
    # 'types', then we do not want to filter out places with a missing 'types' property from the results set.
    results << self.new(result, options[:key], options[:sensor]) if result['types'].nil? || (result['types'] & exclude) == []
  end

  results
end

Instance Method Details

#[](key) ⇒ Object



178
179
180
# File 'lib/google_api_customization/place.rb', line 178

def [] (key)
  send(key)
end

#address_component(address_component_type, address_component_length) ⇒ Object



185
186
187
188
189
# File 'lib/google_api_customization/place.rb', line 185

def address_component(address_component_type, address_component_length)
  if component = address_components_of_type(address_component_type)
    component.first[address_component_length] unless component.first.nil?
  end
end

#address_components_of_type(type) ⇒ Object



191
192
193
# File 'lib/google_api_customization/place.rb', line 191

def address_components_of_type(type)
  @address_components.select{ |c| c['types'].include?(type.to_s) } unless @address_components.nil?
end

#aspects_component(json_aspects) ⇒ Object



211
212
213
# File 'lib/google_api_customization/place.rb', line 211

def aspects_component(json_aspects)
  json_aspects.to_a.map{ |r| { :type => r['type'], :rating => r['rating'] } }
end

#events_component(json_events) ⇒ Object



231
232
233
# File 'lib/google_api_customization/place.rb', line 231

def events_component(json_events)
  json_events.to_a.map{ |r| {:event_id => r['event_id'], :summary => r['summary'], :url => r['url'], :start_time => r['start_time']} }
end

#photos_component(json_photos, api_key, sensor) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/google_api_customization/place.rb', line 215

def photos_component(json_photos, api_key, sensor)
  if json_photos
    json_photos.map{ |p| 
      Photo.new(
        p['width'], 
        p['height'],
        p['photo_reference'],
        p['html_attributions'], 
        api_key, 
        sensor
      )
    }
  else []
  end
end

#reviews_component(json_reviews) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/google_api_customization/place.rb', line 195

def reviews_component(json_reviews)
  if json_reviews
    json_reviews.map { |r|
      Review.new(
          r['rating'],
          r['type'],
          r['author_name'],
          r['author_url'],
          r['text'],
          r['time'].to_i
      )
    }
  else []
  end
end