Class: GooglePlaces::Prediction

Inherits:
Object
  • Object
show all
Defined in:
lib/google_places/prediction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_result_object) ⇒ Prediction

Returns a new instance of Prediction.



13
14
15
16
17
18
19
20
# File 'lib/google_places/prediction.rb', line 13

def initialize(json_result_object)
  @description = json_result_object['description']
  @place_id = json_result_object['place_id']
  @terms = json_result_object['terms']
  @types = json_result_object['types']
  @matched_substrings = json_result_object['matched_substrings']
  @structured_formatting = json_result_object['structured_formatting']
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def description
  @description
end

#matched_substringsObject

Returns the value of attribute matched_substrings.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def matched_substrings
  @matched_substrings
end

#place_idObject

Returns the value of attribute place_id.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def place_id
  @place_id
end

#structured_formattingObject

Returns the value of attribute structured_formatting.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def structured_formatting
  @structured_formatting
end

#termsObject

Returns the value of attribute terms.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def terms
  @terms
end

#typesObject

Returns the value of attribute types.



4
5
6
# File 'lib/google_places/prediction.rb', line 4

def types
  @types
end

Class Method Details

.list_by_input(input, api_key, options = {}) ⇒ Object

Query for Predictions (optionally at the provided location)

Parameters:

  • [String,Integer] (Hash)

    a customizable set of options

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

    a customizable set of options

Options Hash (options):

  • :radius (Integer) — default: 1000

    Defines the distance (in meters) within which to return Place results. The maximum allowed radius is 50,000 meters. Note that radius must not be included if :rankby => ‘distance’ (described below) is specified.

  • :types (String, Array)

    Restricts the results to Spots matching at least one of the specified types

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds



39
40
41
42
43
44
45
46
47
48
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
# File 'lib/google_places/prediction.rb', line 39

def self.list_by_input(input, api_key, options = {})
  lat = options.delete(:lat)
  lng = options.delete(:lng)
  language = options.delete(:language)
  radius = options.delete(:radius)
  retry_options = options.delete(:retry_options) || {}
  types  = options.delete(:types)
  components = options.delete(:components)

  options = {
    :input => input,
    :key => api_key,
    :retry_options => retry_options
  }

  if lat && lng
    options[:location] = Location.new(lat, lng).format
    options[:radius] = radius if radius
  end

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

  if language
    options[:language] = language
  end

  if components
    options[:components] = components
  end

  request(:predictions_by_input, options)
end

.request(method, options) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/google_places/prediction.rb', line 76

def self.request(method, options)
  response = Request.send(method, options)

  response['predictions'].map do |result|
    self.new(result)
  end
end