Module: Food2Fork::Client

Includes:
HTTParty
Defined in:
lib/food2fork/client.rb

Class Method Summary collapse

Class Method Details

.convert_query_to_url(params) ⇒ Object



8
9
10
11
12
# File 'lib/food2fork/client.rb', line 8

def self.convert_query_to_url(params)
  params.map do |k,v|
    "&#{k}=#{v}"
  end.join
end

.make_request(klass, endpoint, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/food2fork/client.rb', line 14

def self.make_request(klass, endpoint, params = {})
  query_url = convert_query_to_url(params)
  result = get("#{endpoint}?key=#{ENV['FOOD2FORK_API_KEY']}#{query_url}")
  parsed_result = JSON.parse(result.body)
	if parsed_result["recipes"] && parsed_result["recipes"].class == Array
   objects = parsed_result["recipes"].map do |result|
     klass.new(result)
   end
		objects.length == 1 ? objects.first : objects
 elsif parsed_result["recipe"].class == Hash
   klass.new(result)
  else
    parsed_result
 end
end