Class: Request
- Inherits:
-
Object
- Object
- Request
- Defined in:
- lib/request.rb
Overview
Manages Yummly requests using the Faraday adapter
Class Method Summary collapse
-
.add_max_results(parameters) ⇒ Object
Add max results parameter to searching parameters.
-
.add_max_time(parameters, xp) ⇒ Object
Add maximal time for cooking computed from XP points of user.
-
.add_phrase(parameters, search_parameters) ⇒ Object
Add phrase to searching parameters.
-
.add_search_parameters(parameters, search_parameters) ⇒ Object
Add parameters from command line to searching parameters.
-
.build_search_parameters(search_parameters, key, search_value) ⇒ Object
Build the array in hash on key position.
-
.generic_connection ⇒ Faraday connection
Creates the generic connection for Yummly API.
-
.get_metadata(key) ⇒ String
Provides metadata by key.
-
.get_recipe(food_id) ⇒ String
Provides recipe by ID.
-
.init_search_parameters(search_parameters, key) ⇒ Object
Initialize the search parameters array in hash on key position.
-
.process_response(response) ⇒ String
Processes response of all kind from Yummly.
-
.search(parameters = {}, xp = 0) ⇒ String
—————————————————————————- Search the recipes filtrated with parameters.
Class Method Details
.add_max_results(parameters) ⇒ Object
Add max results parameter to searching parameters
87 88 89 |
# File 'lib/request.rb', line 87 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
152 153 154 155 156 |
# File 'lib/request.rb', line 152 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
112 113 114 115 116 117 118 |
# File 'lib/request.rb', line 112 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
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/request.rb', line 95 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
144 145 146 |
# File 'lib/request.rb', line 144 def self.build_search_parameters(search_parameters, key, search_value) search_parameters[key].push search_value end |
.generic_connection ⇒ Faraday connection
Creates the generic connection for Yummly API
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
124 125 126 127 128 129 |
# File 'lib/request.rb', line 124 def self.(key) = Parser. key conn = generic_connection = conn.get "metadata/#{metadata_key}" process_response end |
.get_recipe(food_id) ⇒ String
Provides recipe by ID
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
135 136 137 |
# File 'lib/request.rb', line 135 def self.init_search_parameters(search_parameters, key) search_parameters[key] = [] end |
.process_response(response) ⇒ String
Processes response of all kind from Yummly
76 77 78 79 80 81 82 |
# File 'lib/request.rb', line 76 def self.process_response(response) if response.status.eql? 200 response.body 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
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 |