Class: User
- Inherits:
-
Object
- Object
- User
- Defined in:
- lib/user.rb
Overview
Represents the user of this application
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
has email, password, token and xp points during the runtime.
-
#password ⇒ Object
readonly
has email, password, token and xp points during the runtime.
-
#token ⇒ Object
readonly
has email, password, token and xp points during the runtime.
-
#xp ⇒ Object
readonly
has email, password, token and xp points during the runtime.
Class Method Summary collapse
-
.initialize ⇒ Object
Constructor creates the new user with unset email and password.
Instance Method Summary collapse
-
#check_rating(rating) ⇒ String
Checks the boundary on food rating.
-
#last ⇒ String
Provides the last recipe of user.
-
#level(xp) ⇒ String
Computes the level from XP.
-
#login(email = @email, password = @password) ⇒ String
Login the user using Backendless.
-
#process_login_response(email, password, response) ⇒ Object
Process the login response and set the attributes of user.
-
#read_creddentials ⇒ Object
Reads the credentials from the ‘creddentials.csv’ and set them.
-
#register(email, password) ⇒ String
Register the user using Backendless.
-
#review(rating) ⇒ true?
Reviews the last food of the user.
-
#store_creddentials ⇒ Object
Stores the credentials to ‘creddentials.csv’ to achieve persistence.
-
#to_str ⇒ String
Provides the user stats in human-readable form.
-
#update_last_food_id(food_id) ⇒ true?
Updates the last food id in Backendless database.
Instance Attribute Details
#email ⇒ Object (readonly)
has email, password, token and xp points during the runtime
4 5 6 |
# File 'lib/user.rb', line 4 def email @email end |
#password ⇒ Object (readonly)
has email, password, token and xp points during the runtime
4 5 6 |
# File 'lib/user.rb', line 4 def password @password end |
#token ⇒ Object (readonly)
has email, password, token and xp points during the runtime
4 5 6 |
# File 'lib/user.rb', line 4 def token @token end |
#xp ⇒ Object (readonly)
has email, password, token and xp points during the runtime
4 5 6 |
# File 'lib/user.rb', line 4 def xp @xp end |
Class Method Details
.initialize ⇒ Object
Constructor creates the new user with unset email and password
7 8 9 10 |
# File 'lib/user.rb', line 7 def self.initialize @email = nil @password = nil end |
Instance Method Details
#check_rating(rating) ⇒ String
Checks the boundary on food rating
111 112 113 114 115 116 117 |
# File 'lib/user.rb', line 111 def () abort('Rating points missing!') if .nil? = Integer() = 3 if > 3 = 0 if < 0 end |
#last ⇒ String
Provides the last recipe of user
58 59 60 61 62 |
# File 'lib/user.rb', line 58 def last login_response = Backendless.login @email, @password object_id = Parser.login_object_id login_response Backendless.last(object_id) end |
#level(xp) ⇒ String
Computes the level from XP
134 135 136 |
# File 'lib/user.rb', line 134 def level(xp) xp / 10 end |
#login(email = @email, password = @password) ⇒ String
Login the user using Backendless
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/user.rb', line 31 def login(email = @email, password = @password) response = Backendless.login email, password if response process_login_response email, password, response store_creddentials 'Login success' else abort('Login failed') end end |
#process_login_response(email, password, response) ⇒ Object
Process the login response and set the attributes of user
47 48 49 50 51 52 53 |
# File 'lib/user.rb', line 47 def process_login_response(email, password, response) @email = email @password = password @token = Parser.extract_user_token response @id = Parser.login_object_id response @xp = Parser.extract_xp response end |
#read_creddentials ⇒ Object
Reads the credentials from the ‘creddentials.csv’ and set them
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/user.rb', line 73 def read_creddentials creddentials = [] CSV.foreach ENV['HOME'] + '/creddentials.csv' do |record| creddentials << record end @email = creddentials[0][0] @password = creddentials[0][1] rescue StandardError => e abort("You are not logged in!\nERROR:\n#{e}") end |
#register(email, password) ⇒ String
Register the user using Backendless
17 18 19 20 21 22 23 24 |
# File 'lib/user.rb', line 17 def register(email, password) response = Backendless.register email, password if response 'Registration sucessful' else 'Registration failed' end end |
#review(rating) ⇒ true?
Reviews the last food of the user
97 98 99 100 101 102 103 104 105 |
# File 'lib/user.rb', line 97 def review() = @xp += last_food = Backendless.last @id return false if last_food.eql? 'Empty plate' response = Backendless.update(@id, @token, 'xp', @xp) update_last_food_id 'Empty plate' if response true if response end |
#store_creddentials ⇒ Object
Stores the credentials to ‘creddentials.csv’ to achieve persistence
65 66 67 68 69 70 |
# File 'lib/user.rb', line 65 def store_creddentials CSV.open(ENV['HOME'] + '/creddentials.csv', 'w') do |csv| csv << [@email, @password] end read_creddentials end |
#to_str ⇒ String
Provides the user stats in human-readable form
122 123 124 125 126 127 128 |
# File 'lib/user.rb', line 122 def to_str result = "Email: #{@email}\n" lvl = level xp result += "Level: #{lvl}\n" result += "XP: #{@xp}\n" result end |
#update_last_food_id(food_id) ⇒ true?
Updates the last food id in Backendless database
88 89 90 91 |
# File 'lib/user.rb', line 88 def update_last_food_id(food_id) response = Backendless.update(@id, @token, 'food_id', food_id) true if response end |