Class: YelpApi

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_keys) ⇒ YelpApi

Returns a new instance of YelpApi.



22
23
24
25
26
27
28
29
30
# File 'lib/yelp4rails.rb', line 22

def initialize(auth_keys)
  @consumer_key = auth_keys[:consumer_key]
  @consumer_secret = auth_keys[:consumer_secret]
  @token = auth_keys[:token]
  @token_secret = auth_keys[:token_secret]
  @base_url = "http://api.yelp.com"
  setup_consumer
  setup_access_token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Make it model like ;)



79
80
81
82
83
84
85
86
87
# File 'lib/yelp4rails.rb', line 79

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^search_by_(.+)$/
    run_search_by_method($1, *args, &block)
  else
    super # You *must* call super if you don't handle the
          # method, otherwise you'll mess up Ruby's method
          # lookup.
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def access_token
  @access_token
end

#auth_keysObject

Returns the value of attribute auth_keys.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def auth_keys
  @auth_keys
end

#base_urlObject

Returns the value of attribute base_url.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def base_url
  @base_url
end

#consumerObject (readonly)

Returns the value of attribute consumer.



20
21
22
# File 'lib/yelp4rails.rb', line 20

def consumer
  @consumer
end

#consumer_keyObject

Returns the value of attribute consumer_key.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def consumer_secret
  @consumer_secret
end

#tokenObject

Returns the value of attribute token.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret.



19
20
21
# File 'lib/yelp4rails.rb', line 19

def token_secret
  @token_secret
end

#yelp_clientObject (readonly)

Returns the value of attribute yelp_client.



20
21
22
# File 'lib/yelp4rails.rb', line 20

def yelp_client
  @yelp_client
end

Instance Method Details

#fetch_uri(uri) ⇒ Object



67
68
69
70
71
# File 'lib/yelp4rails.rb', line 67

def fetch_uri(uri)
  log "Fetching from Yelp: #{uri}"
  result = @access_token.get(uri)
  return JSON.parse(result.body)
end

#find_by(params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yelp4rails.rb', line 54

def find_by(params)
  if params.include? :business_id
    search_by_business_id(params[:business_id])
  else
    queryparams = []
    params.each do |key, value|
      queryparams << "#{key}=#{value.gsub(" ", "+")}"
    end
    uri = "/v2/search?#{queryparams.join("&")}"
    fetch_uri(uri)
  end
end

#log(message, level = :info) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yelp4rails.rb', line 32

def log(message, level=:info)
  if defined? Rails.logger
    if level == :info
      Rails.logger.info message
    elsif level == :error
      Rails.logger.error message
    end
  else
    puts "#{level}: #{message}"
  end
end

#run_search_by_method(attrs, *args, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/yelp4rails.rb', line 89

def run_search_by_method(attrs, *args, &block)
  # Make an array of attribute names
  attrs = attrs.split('_and_')
  # #transpose will zip the two arrays together like so:
  #   [[:a, :b, :c], [1, 2, 3]].transpose
  #   # => [[:a, 1], [:b, 2], [:c, 3]]
  attrs_with_args = [attrs, args].transpose       
  queryparams=[]
  attrs_with_args.each do |key, value|
    queryparams << "#{key}=#{value.gsub(" ", "+")}"
  end       
  #{OAuth::Helper.escape(query)}
  uri = "/v2/search?#{queryparams.join("&")}"
  #puts uri       
  return fetch_uri(uri)
end

#search_by_business_id(bid) ⇒ Object



73
74
75
# File 'lib/yelp4rails.rb', line 73

def search_by_business_id(bid)
  return fetch_uri("/v2/business/#{bid}")
end

#setup_access_tokenObject



48
49
50
51
52
# File 'lib/yelp4rails.rb', line 48

def setup_access_token
  @access_token = OAuth::AccessToken.new(@consumer)
  @access_token.token = @token
  @access_token.secret = @token_secret
end

#setup_consumerObject



44
45
46
# File 'lib/yelp4rails.rb', line 44

def setup_consumer
  @consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, :site=>@base_url)
end