Class: Request

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

Overview

Manages Yummly requests using the Faraday adapter

Class Method Summary collapse

Class Method Details

.add_max_results(parameters) ⇒ Object

Add max results parameter to searching parameters

Parameters:

  • parameters (Hash)

    searching parameters



89
90
91
# File 'lib/request.rb', line 89

def self.add_max_results(parameters)
  parameters['maxResult'] = @max_results
end

.add_max_time(parameters, xp) ⇒ Object

Add maximal time for cooking computed from XP points of user

Parameters:

  • parameters (Hash)

    search parameters for request

  • xp (String)

    XP points of the user



154
155
156
157
158
# 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) ⇒ Object

Add phrase to searching parameters

Parameters:

  • parameters (Hash)

    parameters from command line

  • search_parameters (Hash)

    search parameters in proper form for request



114
115
116
117
118
119
120
# 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) ⇒ Object

Add parameters from command line to searching parameters

Parameters:

  • parameters (Hash)

    parameters from command line

  • search_parameters (Hash)

    search parameters in proper form for request



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/request.rb', line 97

def self.add_search_parameters(parameters, search_parameters)
  parameters.each do |key, value|
     =  key
     = Parser.jsonp_to_json , key
    init_search_parameters search_parameters, key
    multiple_parameters = value.split(',')
    multiple_parameters.each do |single_parameter|
      search_value = Parser.get_search_value , single_parameter
      build_search_parameters search_parameters, key, search_value
    end
  end
end

.build_search_parameters(search_parameters, key, search_value) ⇒ Object

Build the array in hash on key position

Parameters:

  • search_parameters (Hash)

    search parameters for request

  • key (String)

    key from metadata

  • search_value (String)

    value from metadata for searching



146
147
148
# File 'lib/request.rb', line 146

def self.build_search_parameters(search_parameters, key, search_value)
  search_parameters[key].push search_value
end

.generic_connectionFaraday connection

Creates the generic connection for Yummly API

Returns:

  • (Faraday connection)

    connection to Yummly with mandatory headers



18
19
20
21
22
23
# 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) ⇒ String

Provides metadata by key

Parameters:

  • key (String)

    key of metadata

Returns:

  • (String)

    response body of metadata



126
127
128
129
130
131
# File 'lib/request.rb', line 126

def self.(key)
   = Parser. key
  conn = generic_connection
  meta_response = conn.get "metadata/#{}"
  process_response meta_response
end

.get_recipe(food_id) ⇒ String

Provides recipe by ID

Parameters:

  • food_id (String)

    recipe id of food on Yummly

Returns:

  • (String)

    response body containing the recipe data



61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object

Initialize the search parameters array in hash on key position

Parameters:

  • search_parameters (Hash)

    search parameters for request

  • key (String)

    key of metadata



137
138
139
# File 'lib/request.rb', line 137

def self.init_search_parameters(search_parameters, key)
  search_parameters[key] = []
end

.process_response(response) ⇒ String

Processes response of all kind from Yummly

Parameters:

  • response (Faraday response)

    response from Yummly

Returns:

  • (String)

    body of response



76
77
78
79
80
81
82
83
84
# 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

.search(parameters = {}, xp = 0) ⇒ String


Search the recipes filtrated with parameters

Parameters:

  • parameters (Hash) (defaults to: {})

    searching parameters from command line

  • xp (Integer) (defaults to: 0)

    XP points of user for setting max cooking time

Returns:

  • (String)

    Response body containing founded recipes



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/request.rb', line 31

def self.search(parameters = {}, xp = 0)
  # build generic connection
  conn = generic_connection

  # set the search parameters
  search_parameters = {}

  # add phrase if exists
  add_phrase parameters, search_parameters

  # add other search parameters
  add_search_parameters parameters, search_parameters

  # add max searching results
  add_max_results search_parameters

  # calculate max cooking time according to the user's XP
  add_max_time search_parameters, xp

  # execute the request to /recipes with search parameters
  response = conn.get 'recipes', search_parameters

  # return body if OK response
  process_response response
end