Class: Grubber::Restaurant

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

Constant Summary collapse

ATTRIBUTES =
[:name, :description, :categories, :rating, :url, :display_phone, :location, :reviews].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Restaurant

Returns a new instance of Restaurant.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
# File 'lib/grubber/restaurant.rb', line 9

def initialize(opts={})
  opts.each_pair do |k,v|
    self.instance_variable_set("@#{k}", v)
  end
  yield self
end

Class Method Details

.all(opts = {}) ⇒ Object



16
17
18
19
# File 'lib/grubber/restaurant.rb', line 16

def self.all(opts={})
  q = opts[:term] || 'lunch'
  Client.new.search(q)
end

.parse(body) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grubber/restaurant.rb', line 34

def self.parse(body)
  JSON.parse(body)['businesses'].collect do |r|
    Restaurant.new do |n|
      ATTRIBUTES.map(&:to_s).each do |a|
        value = r[a]
        value = value.to_i if a == 'rating'
        n.send("#{a}=", value)
      end
    end
  end
end

.random(opts = {}) ⇒ Object



21
22
23
# File 'lib/grubber/restaurant.rb', line 21

def self.random(opts={})
  all(opts).sample
end

Instance Method Details

#addressObject



25
26
27
# File 'lib/grubber/restaurant.rb', line 25

def address
  location['display_address'].first
end

#categoryObject



29
30
31
32
# File 'lib/grubber/restaurant.rb', line 29

def category
  cat = categories.first
  cat.is_a?(Array) ? cat.first : cat
end