Class: Yelp::Endpoint::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/yelp/endpoint/search.rb

Constant Summary collapse

PATH =
'/v2/search'
BOUNDING_BOX =
[:sw_latitude, :sw_longitude, :ne_latitude, :ne_longitude]
COORDINATES =
[:latitude, :longitude, :accuracy, :altitude, :altitude_accuracy]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Search

Returns a new instance of Search


11
12
13
# File 'lib/yelp/endpoint/search.rb', line 11

def initialize(client)
  @client = client
end

Instance Method Details

#search(location, params = {}, locale = {}) ⇒ BurstStruct::Burst

Take a search_request and return the formatted/structured response from the API

Examples:

Search for business with params and locale

params = { term: 'food',
           limit: 3,
           category: 'discgolf' }

locale = { lang: 'fr' }

response = client.search('San Francisco', params, locale)
response.businesses # [<Business 1>, <Business 2>, <Business 3>]
response.businesses[0].name # 'Yelp'

Search with only location and params

params = { term: 'food' }

response = client.search('San Francisco', params)

Parameters:

Returns:


43
44
45
46
47
48
# File 'lib/yelp/endpoint/search.rb', line 43

def search(location, params = {}, locale = {})
  params.merge!(locale)
  params.merge!({location: location})

  BurstStruct::Burst.new(JSON.parse(search_request(params).body))
end

#search_by_bounding_box(bounding_box, params = {}, locale = {}) ⇒ BurstStruct::Burst

Search by a bounding box: specify a south west lat/long and a ne lat/long along with regular parameters to make a request to the search endpoint More info at: www.yelp.com/developers/documentation/v2/search_api#searchGBB

Examples:

Search for business with params and locale

bounding_box = { sw_latitude: 37.785855, sw_longitude: -122.405780,
                 ne_latitude: 37.780632, ne_longitude: -122.388442 }

params = { term: 'food',
           limit: 3,
           category: 'discgolf' }

locale = { lang: 'fr' }

response = client.search(bounding_box, params, locale)
response.businesses # [<Business 1>, <Business 2>, <Business 3>]
response.businesses[0].name # 'Yelp'

Search with only location and params

bounding_box = { sw_latitude: 37.785855, sw_longitude: -122.405780,
                 ne_latitude: 37.780632, ne_longitude: -122.388442 }

params = { term: 'food' }

response = client.search(bounding_box, params)

Parameters:

Returns:

Raises:


85
86
87
88
89
90
91
92
93
94
# File 'lib/yelp/endpoint/search.rb', line 85

def search_by_bounding_box(bounding_box, params = {}, locale = {})
  raise Error::BoundingBoxNotComplete if BOUNDING_BOX.any? { |corner|
    bounding_box[corner].nil? }

  options = { bounds: build_bounding_box(bounding_box) }
  options.merge!(params)
  options.merge!(locale)

  BurstStruct::Burst.new(JSON.parse(search_request(options).body))
end

#search_by_coordinates(coordinates, params = {}, locale = {}) ⇒ BurstStruct::Burst

Search by coordinates: give it a latitude and longitude along with option accuracy, altitude, and altitude_accuracy to search an area. More info at: www.yelp.com/developers/documentation/v2/search_api#searchGC

Examples:

Search for business with params and locale

coordinates = { latitude: 37.786732,
                longitude: -122.399978 }

params = { term: 'food',
           limit: 3,
           category: 'discgolf' }

locale = { lang: 'fr' }

response = client.search(coordinates, params, locale)
response.businesses # [<Business 1>, <Business 2>, <Business 3>]
response.businesses[0].name # 'Yelp'

Search with only location and params

coordinates = { latitude: 37.786732,
                longitude: -122.399978 }

params = { term: 'food' }

response = client.search(coordinates, params)

Parameters:

Returns:

Raises:


131
132
133
134
135
136
137
138
139
140
# File 'lib/yelp/endpoint/search.rb', line 131

def search_by_coordinates(coordinates, params = {}, locale = {})
  raise Error::MissingLatLng if coordinates[:latitude].nil? ||
      coordinates[:longitude].nil?

  options = { ll: build_coordinates_string(coordinates) }
  options.merge!(params)
  options.merge!(locale)

  BurstStruct::Burst.new(JSON.parse(search_request(options).body))
end