class Request

Manages Yummly requests using the Faraday adapter

Public Class Methods

add_max_results(parameters) click to toggle source

Add max results parameter to searching parameters

@param parameters [Hash] searching parameters

# File lib/request.rb, line 89
def self.add_max_results(parameters)
  parameters['maxResult'] = @max_results
end
add_max_time(parameters, xp) click to toggle source

Add maximal time for cooking computed from XP points of user

@param parameters [Hash] search parameters for request @param xp [String] XP points of the user

# File lib/request.rb, line 154
def self.add_max_time(parameters, xp)
  parameter = xp * @max_results * 10
  parameter = 80 if (xp * @max_results).eql? 0
  parameters['maxTotalTimeInSeconds'] = parameter
end
add_phrase(parameters, search_parameters) click to toggle source

Add phrase to searching parameters

@param parameters [Hash] parameters from command line @param search_parameters [Hash] search parameters in proper form for request

# File lib/request.rb, line 114
def self.add_phrase(parameters, search_parameters)
  hash_phrase = {}
  hash_phrase[:q] = parameters[:q] if parameters[:q]
  parameters.delete :q
  init_search_parameters search_parameters, :q
  build_search_parameters search_parameters, :q, hash_phrase.values.first
end
add_search_parameters(parameters, search_parameters) click to toggle source

Add parameters from command line to searching parameters

@param parameters [Hash] parameters from command line @param search_parameters [Hash] search parameters in proper form for request

# File lib/request.rb, line 97
def self.add_search_parameters(parameters, search_parameters)
  parameters.each do |key, value|
    metadata = get_metadata key
    metadata_json = Parser.jsonp_to_json metadata, key
    init_search_parameters search_parameters, key
    multiple_parameters = value.split(',')
    multiple_parameters.each do |single_parameter|
      search_value = Parser.get_search_value metadata_json, single_parameter
      build_search_parameters search_parameters, key, search_value
    end
  end
end
build_search_parameters(search_parameters, key, search_value) click to toggle source

Build the array in hash on key position

@param search_parameters [Hash] search parameters for request @param key [String] key from metadata @param search_value [String] value from metadata for searching

# File lib/request.rb, line 146
def self.build_search_parameters(search_parameters, key, search_value)
  search_parameters[key].push search_value
end
generic_connection() click to toggle source

Creates the generic connection for Yummly API

@return [Faraday connection] connection to Yummly with mandatory headers

# File lib/request.rb, line 18
def self.generic_connection
  conn = Faraday.new(url: @uri)
  conn.headers['X-Yummly-App-ID'] = @app_id
  conn.headers['X-Yummly-App-Key'] = @app_key
  conn
end
get_metadata(key) click to toggle source

Provides metadata by key

@param key [String] key of metadata @return [String] response body of metadata

# File lib/request.rb, line 126
def self.get_metadata(key)
  metadata_key = Parser.slice_metadata_key key
  conn = generic_connection
  meta_response = conn.get "metadata/#{metadata_key}"
  process_response meta_response
end
get_recipe(food_id) click to toggle source

Provides recipe by ID

@param food_id [String] recipe id of food on Yummly @return [String] response body containing the recipe data

# File lib/request.rb, line 61
def self.get_recipe(food_id)
  # build generic connection
  conn = generic_connection

  # get food by its ID on yummly
  response = conn.get "recipe/#{food_id}"

  # return body if OK response
  process_response response
end
init_search_parameters(search_parameters, key) click to toggle source

Initialize the search parameters array in hash on key position

@param search_parameters [Hash] search parameters for request @param key [String] key of metadata

# File lib/request.rb, line 137
def self.init_search_parameters(search_parameters, key)
  search_parameters[key] = []
end
process_response(response) click to toggle source

Processes response of all kind from Yummly

@param response [Faraday response] response from Yummly @return [String] body of response

# File lib/request.rb, line 76
def self.process_response(response)
  if response.status.eql? 200
    response.body
  elsif [414].include? response.status
    abort('Sorry, I can not find food like this')
  else
    abort('I can not communicate with Yummly. Set the YUMMLY_KEY')
  end
end