Class: WhatsForDinner
- Inherits:
-
Object
- Object
- WhatsForDinner
- Defined in:
- lib/whats_for_dinner.rb
Instance Attribute Summary collapse
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#location ⇒ Object
Returns the value of attribute location.
-
#preferred_categories ⇒ Object
Returns the value of attribute preferred_categories.
-
#restaurants ⇒ Object
Returns the value of attribute restaurants.
-
#users ⇒ Object
Returns the value of attribute users.
-
#yelp_client ⇒ Object
Returns the value of attribute yelp_client.
Instance Method Summary collapse
- #ask ⇒ Object
- #get_category_filters ⇒ Object
-
#initialize(location, users) ⇒ WhatsForDinner
constructor
A new instance of WhatsForDinner.
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
#categories ⇒ Object
Returns the value of attribute categories.
2 3 4 |
# File 'lib/whats_for_dinner.rb', line 2 def categories @categories end |
#location ⇒ Object
Returns the value of attribute location.
2 3 4 |
# File 'lib/whats_for_dinner.rb', line 2 def location @location end |
#preferred_categories ⇒ Object
Returns the value of attribute preferred_categories.
2 3 4 |
# File 'lib/whats_for_dinner.rb', line 2 def preferred_categories @preferred_categories end |
#restaurants ⇒ Object
Returns the value of attribute restaurants.
2 3 4 |
# File 'lib/whats_for_dinner.rb', line 2 def restaurants @restaurants end |
#users ⇒ Object
Returns the value of attribute users.
2 3 4 |
# File 'lib/whats_for_dinner.rb', line 2 def users @users end |
#yelp_client ⇒ Object
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
#ask ⇒ Object
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_filters ⇒ Object
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 |