-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe_puppy.rb
51 lines (39 loc) · 1.33 KB
/
recipe_puppy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
class RecipePuppy
include HTTParty
base_uri "http://www.recipepuppy.com/api"
def self.search(query: nil, ingredients: nil, page: nil)
options = {}.tap do |opt|
opt[:i] = ingredients if ingredients.present?
opt[:q] = query if query.present?
opt[:p] = page if page.present?
end
response_body = get("/", query: options).body
results = begin
JSON.parse(response_body).fetch("results")
rescue JSON::ParserError, KeyError => e
Rails.logger.info("RecipePuppy API Error: #{e}")
[]
end
RecipePuppyRecipe.new_collection(results)
end
def self.query_for_n_entries(n:, query:, ingredients: nil)
page = 0
empty_result_sets_encountered = 0
entries = []
overrall_length = 0
loop do
page += 1
results = search(query: query, ingredients: ingredients, page: page)
.reject(&:default_image?)
# end loop when empty result sets are returned repeatedly
empty_result_sets_encountered += 1 if results.empty?
break if empty_result_sets_encountered > 2
entries.push(*results)
overrall_length += results.length
# end loop if the desired list length has been achieved
break if overrall_length >= n
end
entries.first(n)
end
end