Class: WhatsForDinner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, users) ⇒ WhatsForDinner

Returns a new instance of WhatsForDinner.



4
5
6
7
8
9
10
11
# File 'lib/whats_for_dinner.rb', line 4

def initialize(location, users)
  @location = location
  @users = users
  @yelp_client = YelpClient.new
  @categories = Category.all
  @preferred_categories = []
  @restaurants = []
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def categories
  @categories
end

#locationObject

Returns the value of attribute location.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def location
  @location
end

#preferred_categoriesObject

Returns the value of attribute preferred_categories.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def preferred_categories
  @preferred_categories
end

#restaurantsObject

Returns the value of attribute restaurants.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def restaurants
  @restaurants
end

#usersObject

Returns the value of attribute users.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def users
  @users
end

#yelp_clientObject

Returns the value of attribute yelp_client.



2
3
4
# File 'lib/whats_for_dinner.rb', line 2

def yelp_client
  @yelp_client
end

Instance Method Details

#askObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/whats_for_dinner.rb', line 13

def ask
  if self.restaurants.empty?
    category_filters = get_category_filters
    self.categories = self.categories - self.preferred_categories
    params = {category_filter: category_filters}
    results = yelp_client.search(location, params)
    self.restaurants = results.map{|result| Restaurant.new(result)}
    if self.restaurants.empty?
      ask
    else
      recommendation = self.restaurants.first
      self.restaurants = self.restaurants[1..-1]
      recommendation
    end
  else
    recommendation = self.restaurants.first
    self.restaurants = self.restaurants[1..-1]
    recommendation
  end
end

#get_category_filtersObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/whats_for_dinner.rb', line 34

def get_category_filters
  categories_with_votes = self.categories.map{|category| [category, 0]}.to_h
  users.each do |user|
    user.likes.each do |category|
      categories_with_votes[category] += 1 if categories_with_votes.has_key?(category)
    end
    user.dislikes.each do |category|
      categories_with_votes[category] -= 100 if categories_with_votes.has_key?(category)
    end
  end
  max_votes = categories_with_votes.values.max
  self.preferred_categories = categories_with_votes
    .select {|category, votes| votes == max_votes}
    .keys
  self.preferred_categories.map {|category| category.filter}.join(",")
end