Class: Parser

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

Overview

Provides parsing methods of all kind

Class Method Summary collapse

Class Method Details

.extract_food_id(response) ⇒ String

Extracts the food id from response

Parameters:

  • response (String)

    response body containing the recipe

Returns:

  • (String)

    food id from response



63
64
65
66
# File 'lib/parser.rb', line 63

def self.extract_food_id(response)
  j = JSON.parse response
  j['food_id']
end

.extract_user_token(response) ⇒ String

Extracts the user token from response

Parameters:

  • response (String)

    response body containing user token after login

Returns:

  • (String)

    user token generated when logged in



72
73
74
75
# File 'lib/parser.rb', line 72

def self.extract_user_token(response)
  j = JSON.parse response
  j['user-token']
end

.extract_xp(response) ⇒ String

Extract value of XP points

Parameters:

  • response (String)

    response bode containing the XP value

Returns:

  • (String)

    XP points of the user



81
82
83
84
# File 'lib/parser.rb', line 81

def self.extract_xp(response)
  j = JSON.parse response
  j['xp']
end

.food_id(food_json) ⇒ String

Extracts the food id from json

Parameters:

  • food_json (JSON)

    the food json

Returns:

  • (String)

    id of the food from json



33
34
35
36
37
# 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) ⇒ String

Extract the search value from json according to the description

Parameters:

  • json (JSON)

    metadata from Yummly API

  • description (String)

    local description of data

Returns:

  • (String)

    search value from metadata for Yummly API



8
9
10
11
12
13
14
15
# 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) ⇒ JSON

Parse the jsonp to json

Parameters:

  • jsonp (String)

    jsonp format to parse

  • key (String)

    key used in slicing jsonp

Returns:

  • (JSON)

    parsed json from jsonp



22
23
24
25
26
27
# File 'lib/parser.rb', line 22

def self.jsonp_to_json(jsonp, key)
   =  key
  first_split = jsonp.split("set_metadata('#{}', ")[1]
  second_split = first_split.split(');')[0]
  JSON.parse second_split
end

.login_object_id(login_response) ⇒ Strng

Extracts object ID from lofin response

Parameters:

  • login_response (JSON)

    response from login

Returns:

  • (Strng)

    object ID of the user data



54
55
56
57
# File 'lib/parser.rb', line 54

def self.()
  j = JSON.parse 
  j['objectId']
end

.slice_metadata_key(key) ⇒ String

Extracts the metadata key from search parameter

Parameters:

  • key (String)

    key to be sliced

Returns:

  • (String)

    metadata key



43
44
45
46
47
48
# File 'lib/parser.rb', line 43

def self.(key)
   = key.to_s
  .slice!('allowed') if .include? 'allowed'
  .slice!('excluded') if .include? 'exclude'
  .downcase
end