Class: MealSelector::Backend

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

Overview

Management layer between ApiInterface and Meal Object

Raises:

  • network issue can raise exceptions

Author:

  • Anthony Tilelli

Constant Summary collapse

DEFAULT_FAVORITES_PATH =

Default location for favorite meals to be saved

"#{Dir.home}/favorite_meals.json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Backend

Sets up favorites and categories via api

Parameters:



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

#categoriesObject (readonly)

Returns the value of attribute categories



11
12
13
# File 'lib/meal_selector/backend.rb', line 11

def categories
  @categories
end

#favoritesObject (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

Parameters:

Returns:

  • (boolean)

    If meal was added



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]

Returns:

  • (Boolean)


29
30
31
# File 'lib/meal_selector/backend.rb', line 29

def api_can_save?
  @api.can_save?
end

#clear_favoritesvoid

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

Returns:

  • (Boolean)

Raises:

  • If `#favorites_changed?` is called before `#favorite_init`



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_initvoid

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

Parameters:

  • primary_ingredient (String)

Returns:

  • (Hash)

    hash of zero (`{}`) or more meals



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

Parameters:

  • category (String)

Returns:

  • (Hash)

    zero (`{}`) or more meals

Raises:

  • if category is not in @categories



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

Parameters:

  • name (#to_s)

Returns:

  • (Hash)

    zero (`{}`) or more meals



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_mealMeal

Finds random meal object

Returns:

  • (Meal)

    single meal



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

Parameters:

  • path (String) (defaults to: DEFAULT_FAVORITES_PATH)

Returns:

  • (Boolean)

    If file was loaded

Raises:

  • If path is not a file



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.

Parameters:

Returns:

  • (Meal)

    whole meal



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_infoboolean

Note:

will overwrite existing file

Saves keys and version to default file

Returns:

  • (boolean)

    If was save to 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

Note:

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