Class: MealSelector::ApiInterface
- Inherits:
-
Object
- Object
- MealSelector::ApiInterface
- Defined in:
- lib/meal_selector/api_interface.rb
Overview
Communicates with Mealdb database
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
-
.load(path = DEFAULT_KEY_PATH) ⇒ Object
Creates ApiInterface from a file.
Instance Method Summary collapse
-
#can_save? ⇒ Boolean
Can key/version be saved? return [Boolean].
-
#initialize(key, version) ⇒ ApiInterface
constructor
Api interface for mealdb.
-
#meal_by_id(id) ⇒ Hash
Lookup full meal details by id.
-
#meal_categories ⇒ Hash
Gets List of Categories for meals.
-
#meals_by_category(category) ⇒ Hash
Return meals by Category.
-
#random_meal ⇒ Hash
Lookup a single random meal.
-
#save(path = DEFAULT_KEY_PATH) ⇒ Object
Saves ApiInterface to a file.
-
#search_by_ingredient(primary_ingredient) ⇒ Hash
Search by primary main ingredient.
-
#search_meals_name(name) ⇒ Hash
Search by name.
Constructor Details
#initialize(key, version) ⇒ ApiInterface
Api interface for mealdb
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
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]
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
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_categories ⇒ Hash
Gets List of Categories for meals
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
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_meal ⇒ Hash
Lookup a single random 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
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
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
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 |