Class: MealSelector::Backend
- Inherits:
-
Object
- Object
- MealSelector::Backend
- Defined in:
- lib/meal_selector/backend.rb
Overview
Management layer between ApiInterface and Meal Object
Constant Summary collapse
- DEFAULT_FAVORITES_PATH =
Default location for favorite meals to be saved
"#{Dir.home}/favorite_meals.json"
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#favorites ⇒ Object
readonly
Returns the value of attribute favorites.
Instance Method Summary collapse
-
#add_to_favorites(meal) ⇒ boolean
Favorites Adds meal to favorites.
-
#api_can_save? ⇒ Boolean
Can api key/version be saved? return [Boolean].
-
#clear_favorites ⇒ void
Clears favorites.
-
#favorites_changed? ⇒ Boolean
True if favorites change since `#favorite_init` was called.
-
#favorites_init ⇒ void
Sets the favorites state to watch for changes.
-
#find_meal_by_ingredient(primary_ingredient) ⇒ Hash
Finds meals based on primary ingredient.
-
#find_meals_by_categories(category) ⇒ Hash
find meals based on a category.
-
#find_meals_by_name(name) ⇒ Hash
Find meals by name.
-
#find_random_meal ⇒ Meal
Finds random meal object.
-
#initialize(api) ⇒ Backend
constructor
Sets up favorites and categories via api.
-
#load_favorites(path = DEFAULT_FAVORITES_PATH) ⇒ Boolean
Loads saved favorite meals from path and adds to favorites.
-
#resolve_meal(meal) ⇒ Meal
Creates a whole meal if a meal is not.
-
#save_api_info ⇒ boolean
Saves keys and version to default file.
-
#save_favorites(path = DEFAULT_FAVORITES_PATH) ⇒ void
Saves favorites to a file.
Constructor Details
#initialize(api) ⇒ Backend
Sets up favorites and categories via api
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/meal_selector/backend.rb', line 15 def initialize(api) raise 'api must be a api_interface' unless api.is_a?(ApiInterface) @favorites = {} @categories = {} @favorite_state = :unset @api = api @categories = populate_categories(@api.meal_categories) load_favorites favorites_init end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories
11 12 13 |
# File 'lib/meal_selector/backend.rb', line 11 def categories @categories end |
#favorites ⇒ Object (readonly)
Returns the value of attribute favorites
11 12 13 |
# File 'lib/meal_selector/backend.rb', line 11 def favorites @favorites end |
Instance Method Details
#add_to_favorites(meal) ⇒ boolean
Favorites Adds meal to favorites
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/meal_selector/backend.rb', line 48 def add_to_favorites(meal) raise 'Not a meal' unless meal.is_a?(Meal) if @favorites[meal.id].nil? @favorites[meal.id] = meal true else false end end |
#api_can_save? ⇒ Boolean
Can api key/version be saved? return [Boolean]
29 30 31 |
# File 'lib/meal_selector/backend.rb', line 29 def api_can_save? @api.can_save? end |
#clear_favorites ⇒ void
This method returns an undefined value.
Clears favorites
100 101 102 |
# File 'lib/meal_selector/backend.rb', line 100 def clear_favorites @favorites.clear end |
#favorites_changed? ⇒ Boolean
True if favorites change since `#favorite_init` was called
92 93 94 95 96 |
# File 'lib/meal_selector/backend.rb', line 92 def favorites_changed? raise 'favorite_init has not yet been called' if @favorite_state == :unset @favorite_state != @favorites.keys.sort end |
#favorites_init ⇒ void
This method returns an undefined value.
Sets the favorites state to watch for changes
85 86 87 |
# File 'lib/meal_selector/backend.rb', line 85 def favorites_init @favorite_state = @favorites.keys.sort end |
#find_meal_by_ingredient(primary_ingredient) ⇒ Hash
Finds meals based on primary ingredient
129 130 131 132 |
# File 'lib/meal_selector/backend.rb', line 129 def find_meal_by_ingredient(primary_ingredient) meals_hsh = @api.search_by_ingredient(primary_ingredient.downcase) Meal.create_from_array(meals_hsh) end |
#find_meals_by_categories(category) ⇒ Hash
find meals based on a category
117 118 119 120 121 122 123 124 |
# File 'lib/meal_selector/backend.rb', line 117 def find_meals_by_categories(category) raise '@categories is empty' if @categories.empty? raise 'category must be a string' unless category.is_a?(String) raise 'Provided category is not valid' unless @categories.include?(category.capitalize) meals = @api.meals_by_category(category) Meal.create_from_array(meals) end |
#find_meals_by_name(name) ⇒ Hash
Find meals by name
109 110 111 |
# File 'lib/meal_selector/backend.rb', line 109 def find_meals_by_name(name) Meal.create_from_array(@api.search_meals_name(name)) end |
#find_random_meal ⇒ Meal
Finds random meal object
136 137 138 |
# File 'lib/meal_selector/backend.rb', line 136 def find_random_meal Meal.new(@api.random_meal) end |
#load_favorites(path = DEFAULT_FAVORITES_PATH) ⇒ Boolean
Loads saved favorite meals from path and adds to favorites
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/meal_selector/backend.rb', line 72 def load_favorites(path = DEFAULT_FAVORITES_PATH) return false unless File.exist?(path) raise "#{path} is not a file" unless File.file?(path) raw_data = File.read(path).chomp parsed = JSON.parse(raw_data, symbolize_names: true) meals_hsh = Meal.create_from_array(parsed) meals_hsh.each { |_key, meal| add_to_favorites(meal) } true end |
#resolve_meal(meal) ⇒ Meal
Creates a whole meal if a meal is not.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/meal_selector/backend.rb', line 143 def resolve_meal(meal) raise 'Must be a meal object' unless meal.is_a?(Meal) resolved = if meal.whole_meal? meal else # returns meal by id Meal.new(@api.meal_by_id(meal.id.to_i)) end resolved end |
#save_api_info ⇒ boolean
will overwrite existing file
Saves keys and version to default file
36 37 38 39 40 41 42 |
# File 'lib/meal_selector/backend.rb', line 36 def save_api_info if api_can_save? @api.save return true end false end |
#save_favorites(path = DEFAULT_FAVORITES_PATH) ⇒ void
Will overwrite existing file
This method returns an undefined value.
Saves favorites to a file
62 63 64 65 66 |
# File 'lib/meal_selector/backend.rb', line 62 def save_favorites(path = DEFAULT_FAVORITES_PATH) converted = @favorites.collect { |_id, meal| meal.to_meal_hash } meal_hash = { meals: converted } File.open(path, 'w') { |file| file.write(meal_hash.to_json) } end |