Class: Yelp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yelpster/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

#initialize(attributes = {}) ⇒ Client

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


48
49
50
51
52
53
54
55
# File 'lib/yelpster/client.rb', line 48

def initialize(attributes = {})
  @agent = DEFAULT_AGENT
  @debug = false
  @logger = nil
  attributes.each do |attr, value|
    self.send("#{attr}=", value)
  end
end

Instance Attribute Details

#agentObject

allows specifying the user agent string to submit with search requests


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

def agent
  @agent
end

#consumer_keyObject

Returns the value of attribute consumer_key


40
41
42
# File 'lib/yelpster/client.rb', line 40

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret


40
41
42
# File 'lib/yelpster/client.rb', line 40

def consumer_secret
  @consumer_secret
end

#debugObject

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


34
35
36
# File 'lib/yelpster/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/yelpster/client.rb', line 38

def logger
  @logger
end

#tokenObject

Returns the value of attribute token


40
41
42
# File 'lib/yelpster/client.rb', line 40

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret


40
41
42
# File 'lib/yelpster/client.rb', line 40

def token_secret
  @token_secret
end

#yws_idObject

Returns the value of attribute yws_id


40
41
42
# File 'lib/yelpster/client.rb', line 40

def yws_id
  @yws_id
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.


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/yelpster/client.rb', line 60

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[:proxy] = nil
  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