class Parser

Provides parsing methods of all kind

Public Class Methods

extract_food_id(response) click to toggle source

Extracts the food id from response

@param response [String] response body containing the recipe @return [String] food id from response

# File lib/parser.rb, line 63
def self.extract_food_id(response)
  j = JSON.parse response
  j['food_id']
end
extract_user_token(response) click to toggle source

Extracts the user token from response

@param response [String] response body containing user token after login @return [String] user token generated when logged in

# File lib/parser.rb, line 72
def self.extract_user_token(response)
  j = JSON.parse response
  j['user-token']
end
extract_xp(response) click to toggle source

Extract value of XP points

@param response [String] response bode containing the XP value @return [String] XP points of the user

# File lib/parser.rb, line 81
def self.extract_xp(response)
  j = JSON.parse response
  j['xp']
end
food_id(food_json) click to toggle source

Extracts the food id from json

@param food_json [JSON] the food json @return [String] id of the food from json

# File lib/parser.rb, line 33
def self.food_id(food_json)
  abort("Sorry, I can't find food for you like that :(") if food_json.nil?
  id = food_json['id']
  id
end
get_search_value(json, description) click to toggle source

Extract the search value from json according to the description

@param json [JSON] metadata from Yummly API @param description [String] local description of data @return [String] search value from metadata for Yummly API

# File lib/parser.rb, line 8
def self.get_search_value(json, description)
  json.each do |metapar|
    ext_hash = metapar.select { |k, _| k.downcase.include? 'description' }
    extern = ext_hash.values.first.downcase
    local = description.downcase
    return metapar['searchValue'] if "\b#{local}\b".match(extern)
  end
end
jsonp_to_json(jsonp, key) click to toggle source

Parse the jsonp to json

@param jsonp [String] jsonp format to parse @param key [String] key used in slicing jsonp @return [JSON] parsed json from jsonp

# File lib/parser.rb, line 22
def self.jsonp_to_json(jsonp, key)
  metadata_key = slice_metadata_key key
  first_split = jsonp.split("set_metadata('#{metadata_key}', ")[1]
  second_split = first_split.split(');')[0]
  JSON.parse second_split
end
login_object_id(login_response) click to toggle source

Extracts object ID from lofin response

@param login_response [JSON] response from login @return [Strng] object ID of the user data

# File lib/parser.rb, line 54
def self.login_object_id(login_response)
  j = JSON.parse login_response
  j['objectId']
end
slice_metadata_key(key) click to toggle source

Extracts the metadata key from search parameter

@param key [String] key to be sliced @return [String] metadata key

# File lib/parser.rb, line 43
def self.slice_metadata_key(key)
  metadata_key = key.to_s
  metadata_key.slice!('allowed') if metadata_key.include? 'allowed'
  metadata_key.slice!('excluded') if metadata_key.include? 'exclude'
  metadata_key.downcase
end