class Yelp::Business

Constants

VERSION

Attributes

api_url[RW]
business_id[RW]
business_url[RW]
data[RW]
reviews_api_url[RW]

Public Class Methods

business_id_from(url) click to toggle source
# File lib/yelp/business.rb, line 17
def business_id_from(url)
  url.gsub %r[.*/], ''
end
configure() { |self| ... } click to toggle source
# File lib/yelp/business.rb, line 11
def configure
  Yelp.instance_eval do
    yield(self)
  end
end
new(business_id = nil) click to toggle source
# File lib/yelp/business.rb, line 28
def initialize(business_id = nil)
  raise ::Yelp::InvalidIdentifierError, 'business ID can not be nil' if business_id.nil?
  self.business_id  = business_id
  self.business_url = PUBLIC_URL_PREFIX + business_id
  self.api_url      = API_HOST + BUSINESS_PATH + self.business_id if self.business_id
  self.reviews_api_url = API_HOST + BUSINESS_PATH + self.business_id + REVIEWS_SUFFIX
end

Public Instance Methods

access_token() click to toggle source
# File lib/yelp/business.rb, line 92
def access_token
  Yelp.access_token
end
api_fetch() click to toggle source
# File lib/yelp/business.rb, line 76
def api_fetch
  HTTP.
    auth(bearer_token).
    get(api_url).parse
end
bearer_token() click to toggle source
# File lib/yelp/business.rb, line 88
def bearer_token
  "Bearer #{access_token}"
end
error(*args) click to toggle source
# File lib/yelp/business.rb, line 72
def error(*args)
  STDERR.puts args.join("\n")
end
exec_api_call() click to toggle source
# File lib/yelp/business.rb, line 56
def exec_api_call
  raise ::Yelp::MissingAccessTokenError unless access_token
  response = api_fetch
  if response['error']
    raise ::Yelp::AuthenticationError, 'Invalid access token' if response['error']['code'] == 'TOKEN_INVALID'
    raise ::Yelp::BusinessNotFoundError, "Business #{business_id} was not found" if response['error']['description'] =~ /could not be found/
    raise ::Yelp::YelpError, "Yelp returned error: #{response['error']['description']}"
  end
  response
rescue Exception => e
  error "ERROR:   while fetching data for business #{business_id.bold.green} from #{api_url.yellow}"
  error "DETAILS: #{e.message.bold.red}"
  error(*e.backtrace) if ENV['DEBUG']
  raise e
end
fetch() { |data| ... } click to toggle source

usage: @business.fetch do |data|

data.name #=> 'Gary Danko'

end

# File lib/yelp/business.rb, line 40
def fetch
  unless self.data
    raise ::Yelp::MissingAccessTokenError, 'please specify authentication token as class variable' unless access_token
    self.data = OpenStruct.new(exec_api_call)
    raise ::Yelp::BusinessNotFoundError "Can't find business #{business_id}" unless data
  end
  yield(self.data) if block_given?
  self
end
Also aliased as: get
get()
Alias for: fetch
get_reviews() click to toggle source
# File lib/yelp/business.rb, line 52
def get_reviews
  self.data = OpenStruct.new(reviews_api_fetch)
end
method_missing(method, *args, &block) click to toggle source
# File lib/yelp/business.rb, line 96
def method_missing(method, *args, &block)
  data.send(method, *args, &block)
end
reviews_api_fetch() click to toggle source
# File lib/yelp/business.rb, line 82
def reviews_api_fetch
  HTTP.
    auth(bearer_token).
    get(reviews_api_url).parse
end