Class: Yelp::Business

Inherits:
Object
  • Object
show all
Defined in:
lib/yelp/business.rb,
lib/yelp/business/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(business_id = nil) ⇒ Business

Returns a new instance of Business.



28
29
30
31
32
33
34
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



96
97
98
# File 'lib/yelp/business.rb', line 96

def method_missing(method, *args, &block)
  data.send(method, *args, &block)
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



22
23
24
# File 'lib/yelp/business.rb', line 22

def api_url
  @api_url
end

#business_idObject

Returns the value of attribute business_id.



22
23
24
# File 'lib/yelp/business.rb', line 22

def business_id
  @business_id
end

#business_urlObject

Returns the value of attribute business_url.



22
23
24
# File 'lib/yelp/business.rb', line 22

def business_url
  @business_url
end

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/yelp/business.rb', line 22

def data
  @data
end

#reviews_api_urlObject

Returns the value of attribute reviews_api_url.



22
23
24
# File 'lib/yelp/business.rb', line 22

def reviews_api_url
  @reviews_api_url
end

Class Method Details

.business_id_from(url) ⇒ Object



17
18
19
# File 'lib/yelp/business.rb', line 17

def business_id_from(url)
  url.gsub %r[.*/], ''
end

.configureObject



11
12
13
14
15
# File 'lib/yelp/business.rb', line 11

def configure
  Yelp.instance_eval do
    yield(self)
  end
end

Instance Method Details

#access_tokenObject



92
93
94
# File 'lib/yelp/business.rb', line 92

def access_token
  Yelp.access_token
end

#api_fetchObject



76
77
78
79
80
# File 'lib/yelp/business.rb', line 76

def api_fetch
  HTTP.
    auth(bearer_token).
    get(api_url).parse
end

#bearer_tokenObject



88
89
90
# File 'lib/yelp/business.rb', line 88

def bearer_token
  "Bearer #{access_token}"
end

#error(*args) ⇒ Object



72
73
74
# File 'lib/yelp/business.rb', line 72

def error(*args)
  STDERR.puts args.join("\n")
end

#exec_api_callObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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 {|self.data| ... } ⇒ Object Also known as: get

usage: @business.fetch do |data|

data.name #=> 'Gary Danko'

end

Yields:



40
41
42
43
44
45
46
47
48
# 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

#get_reviewsObject



52
53
54
# File 'lib/yelp/business.rb', line 52

def get_reviews
  self.data = OpenStruct.new(reviews_api_fetch)
end

#reviews_api_fetchObject



82
83
84
85
86
# File 'lib/yelp/business.rb', line 82

def reviews_api_fetch
  HTTP.
    auth(bearer_token).
    get(reviews_api_url).parse
end