Class: Yelp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yelped/client.rb

Overview

Provides access to the Yelp search facilities as documented at:

www.yelp.com/developers/documentation

Example usage:

client = Yelp::Client.new
request = Yelp::V1::Review::Request::Location.new(
             :address => '650 Mission St',
             :city => 'San Francisco',
             :state => 'CA',
             :radius => 2,
             :term => 'cream puffs',
             :yws_id => 'YOUR_YWSID_HERE')
response = client.search(request)

By default, response content is formatted as a Ruby hash converted from Yelp’s source JSON response content. Alternate response formats can be specified on request record construction via the Yelp::Request response_format parameter, available in all request record types.

Constant Summary collapse

DEFAULT_AGENT =

the default user agent submitted with search requests

'yelp for Ruby (http://www.rubyforge.org/projects/yelp/)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Constructs a new client that uses the supplied YWSID for submitting search requests.



46
47
48
49
50
# File 'lib/yelped/client.rb', line 46

def initialize
  @agent = DEFAULT_AGENT
  @debug = false
  @logger = nil
end

Instance Attribute Details

#agentObject

allows specifying the user agent string to submit with search requests



31
32
33
# File 'lib/yelped/client.rb', line 31

def agent
  @agent
end

#debugObject

whether debug mode is enabled for logging purposes, defaulting to false



34
35
36
# File 'lib/yelped/client.rb', line 34

def debug
  @debug
end

#loggerObject

the Logger compatible object with which log messages are outputted, defaulting to output to STDOUT



38
39
40
# File 'lib/yelped/client.rb', line 38

def logger
  @logger
end

Instance Method Details

#search(request) ⇒ Object

Submits the supplied search request to Yelp and returns the response in the format specified by the request.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yelped/client.rb', line 55

def search (request)
  # build the full set of hash params with which the url is constructed
  params = request.to_yelp_params

  # construct the url with which we obtain results
  url = build_url(request.base_url, params)
  debug_msg "submitting search [url=#{url}, request=#{request.to_yaml}]."

  # submit the http request for the results
 # http_request_params not used in v2 as OAuth (implemented in v2) only takes response params
 http_params = { 'User-Agent' => @agent }
 http_params['Accept-Encoding'] = 'gzip,deflate' if request.compress_response?
 content = request.pull_results(url, http_params)

  # read the response content
  debug_msg((request.response_format.serialized?) ? "received response [content_length=#{content.length}]." : "received response [content_length=#{content.length}, content=#{content}].")

  # format the output as specified in the request
  format_content(request.response_format, content)
end