Class: MealSelector::ApiInterface

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

Overview

Communicates with Mealdb database

Raises:

  • network issue can raise exceptions

Author:

  • Anthony Tilelli

Constant Summary collapse

API_ENDPOINT =

URL to mealdb

'https://www.themealdb.com/api/json'
DEFAULT_KEY_PATH =

Default path for key and version

"#{Dir.home}/.Mealdbkey"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, version) ⇒ ApiInterface

Api interface for mealdb

Parameters:

  • key (#to_s)

    number for your api key

  • version (String)

    choose '1'or '2' to use



16
17
18
19
20
21
22
23
24
25
# File 'lib/meal_selector/api_interface.rb', line 16

def initialize(key, version)
  # Sets API key and Version
  # Set api_key and check its correct format
  raise 'Version must be 1 or 2' unless %w[1 2].include?(version)

  warn('Warning: API key `1` is only for development') if key.to_s == '1'
  @version = version
  @key = key.to_s
  @api_url = API_ENDPOINT + "/v#{@version}/#{@key}/"
end

Class Method Details

.load(path = DEFAULT_KEY_PATH) ⇒ Object

Creates ApiInterface from a file

Parameters:

  • path (string) (defaults to: DEFAULT_KEY_PATH)

Raises:

  • (RuntimeError)

    When file does not exits or malformed file



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/meal_selector/api_interface.rb', line 116

def self.load(path = DEFAULT_KEY_PATH)
  raise "File #{path} does not exist!" unless File.exist?(path)

  raw_data = File.read(path).chomp
  raw_data = raw_data.split
  raise 'Incorrect format for Meal Api Key file' unless raw_data.count == 4
  raise "Error finding version info (#{raw_data[0]})" unless raw_data[0] == 'version:'
  raise "Error finding key info (#{raw_data[2]})" unless raw_data[2] == 'key:'

  new(raw_data[3], raw_data[1])
end

Instance Method Details

#can_save?Boolean

Can key/version be saved? return [Boolean]

Returns:

  • (Boolean)


98
99
100
# File 'lib/meal_selector/api_interface.rb', line 98

def can_save?
  @key != '1'
end

#meal_by_id(id) ⇒ Hash

Lookup full meal details by id

Examples:

meal_by_id(52772)

Parameters:

  • id (Integer)

Returns:

  • (Hash)

    single meal



43
44
45
46
47
48
49
50
# File 'lib/meal_selector/api_interface.rb', line 43

def meal_by_id(id)
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/lookup.php?i=52772
  raise "id is not an Integer (#{id.class})" unless id.is_a?(Integer)

  content = api_call("lookup.php?i=#{id}")
  validate(content)
  content
end

#meal_categoriesHash

Gets List of Categories for meals

Examples:

meal_categories

Returns:

  • (Hash)

    list of meal categories



65
66
67
68
69
70
# File 'lib/meal_selector/api_interface.rb', line 65

def meal_categories
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/list.php?c=list
  content = api_call('list.php?c=list')
  validate(content)
  content
end

#meals_by_category(category) ⇒ Hash

Return meals by Category

Examples:

#meals_by_category(“Seafood”)

Parameters:

  • category (#to_s)

Returns:

  • (Hash)

    one or more meals



89
90
91
92
93
94
# File 'lib/meal_selector/api_interface.rb', line 89

def meals_by_category(category)
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/filter.php?c=Seafood
  content = api_call("filter.php?c=#{category}")
  validate(content)
  content
end

#random_mealHash

Lookup a single random meal

Examples:

random_meal

Returns:

  • (Hash)

    single meal



55
56
57
58
59
60
# File 'lib/meal_selector/api_interface.rb', line 55

def random_meal
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/random.php
  content = api_call('random.php')
  validate(content)
  content
end

#save(path = DEFAULT_KEY_PATH) ⇒ Object

Note:

will overwrite existing file

Saves ApiInterface to a file

Parameters:

  • path (String) (defaults to: DEFAULT_KEY_PATH)

Raises:

  • (RuntimeError)

    When attempting to safe debug key



106
107
108
109
110
111
# File 'lib/meal_selector/api_interface.rb', line 106

def save(path = DEFAULT_KEY_PATH)
  raise 'cannot save debug key' unless can_save?

  File.open(path, 'w') { |file| file.write("version: #{@version}\nkey: #{@key}") }
  File.chmod(0o600, path)
end

#search_by_ingredient(primary_ingredient) ⇒ Hash

Search by primary main ingredient

Examples:

#search_by_ingredient(“chicken_breast”)

#search_by_ingredient(“chicken breast”)

Parameters:

  • primary_ingredient (#to_s)

Returns:

  • (Hash)

    one or more meals



77
78
79
80
81
82
83
# File 'lib/meal_selector/api_interface.rb', line 77

def search_by_ingredient(primary_ingredient)
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/filter.php?i=chicken_breast
  url_end = "filter.php?i=#{primary_ingredient}"
  content = api_call(url_end)
  validate(content)
  content
end

#search_meals_name(name) ⇒ Hash

Search by name

Examples:

#search_meal_name(“Arrabiata”)

Parameters:

  • name (#to_s)

Returns:

  • (Hash)

    sinle or multiple meals



31
32
33
34
35
36
37
# File 'lib/meal_selector/api_interface.rb', line 31

def search_meals_name(name)
  # API EXAMPLE: https://www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata
  url_end = "search.php?s=#{name.to_s.gsub(' ', '%20')}"
  content = api_call(url_end)
  validate(content)
  content
end