Class: User

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

Overview

Represents the user of this application

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#emailObject (readonly)

has email, password, token and xp points during the runtime



4
5
6
# File 'lib/user.rb', line 4

def email
  @email
end

#passwordObject (readonly)

has email, password, token and xp points during the runtime



4
5
6
# File 'lib/user.rb', line 4

def password
  @password
end

#tokenObject (readonly)

has email, password, token and xp points during the runtime



4
5
6
# File 'lib/user.rb', line 4

def token
  @token
end

#xpObject (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

.initializeObject

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

Parameters:

  • rating (String)

    rating of food

Returns:

  • (String)

    real rating in proper interval



111
112
113
114
115
116
117
# File 'lib/user.rb', line 111

def check_rating(rating)
  abort('Rating points missing!') if rating.nil?
  real_rating = Integer(rating)
  real_rating = 3 if real_rating > 3
  real_rating = 0 if real_rating < 0
  real_rating
end

#lastString

Provides the last recipe of user

Returns:

  • (String)

    the last food id of the user



58
59
60
61
62
# File 'lib/user.rb', line 58

def last
   = Backendless. @email, @password
  object_id = Parser. 
  Backendless.last(object_id)
end

#level(xp) ⇒ String

Computes the level from XP

Parameters:

  • xp (Integer)

    XP points of the user gained from ratings of his food

Returns:

  • (String)

    level of the user



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

Parameters:

  • email (String) (defaults to: @email)

    email of the user

  • password (String) (defaults to: @password)

    password of the user

Returns:

  • (String)

    state of the login in human-readable form



31
32
33
34
35
36
37
38
39
40
# File 'lib/user.rb', line 31

def (email = @email, password = @password)
  response = Backendless. email, password
  if 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

Parameters:

  • email (String)

    email of the user

  • password (String)

    password of the user

  • response (JSON)

    response from login to Backendless



47
48
49
50
51
52
53
# File 'lib/user.rb', line 47

def (email, password, response)
  @email = email
  @password = password
  @token = Parser.extract_user_token response
  @id = Parser. response
  @xp = Parser.extract_xp response
end

#read_creddentialsObject

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

Parameters:

  • email (String)

    email of the user

  • password (String)

    password of the user

Returns:

  • (String)

    state of the registration in human-readable form



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

Parameters:

  • rating (String)

    the rating of the last food from 0 to 3

Returns:

  • (true, nil)

    the status of the operation



97
98
99
100
101
102
103
104
105
# File 'lib/user.rb', line 97

def review(rating)
  real_rating = check_rating rating
  @xp += real_rating
  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_creddentialsObject

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_strString

Provides the user stats in human-readable form

Returns:

  • (String)

    user stats



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

Parameters:

  • food_id (String)

    the id of food from Yummly

Returns:

  • (true, nil)

    the state of the operation



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