Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/parser.rb
Overview
Provides parsing methods of all kind
Class Method Summary collapse
-
.extract_food_id(response) ⇒ String
Extracts the food id from response.
-
.extract_user_token(response) ⇒ String
Extracts the user token from response.
-
.extract_xp(response) ⇒ String
Extract value of XP points.
-
.food_id(food_json) ⇒ String
Extracts the food id from json.
-
.get_search_value(json, description) ⇒ String
Extract the search value from json according to the description.
-
.jsonp_to_json(jsonp, key) ⇒ JSON
Parse the jsonp to json.
-
.login_object_id(login_response) ⇒ Strng
Extracts object ID from lofin response.
-
.slice_metadata_key(key) ⇒ String
Extracts the metadata key from search parameter.
Class Method Details
.extract_food_id(response) ⇒ String
Extracts the 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
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
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
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
8 9 10 11 12 13 14 15 |
# File 'lib/parser.rb', line 8 def self.get_search_value(json, description) json.each do || ext_hash = .select { |k, _| k.downcase.include? 'description' } extern = ext_hash.values.first.downcase local = description.downcase return ['searchValue'] if "\b#{local}\b".match(extern) end end |
.jsonp_to_json(jsonp, key) ⇒ JSON
Parse the jsonp to json
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
54 55 56 57 |
# 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) ⇒ String
Extracts the metadata key from search parameter
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 |