Class: UserSessionsPrinter

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/ft_42.rb

Constant Summary

Constants included from Constants

Constants::HOURS_NEEDED, Constants::SECRET_42, Constants::UID_42, Constants::URL_42

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_sessions) ⇒ UserSessionsPrinter

Returns a new instance of UserSessionsPrinter.



232
233
234
235
# File 'lib/ft_42.rb', line 232

def initialize(user_sessions)
  @pastel = Pastel.new
  @user_sessions = user_sessions
end

Instance Attribute Details

#pastelObject (readonly)

Returns the value of attribute pastel.



230
231
232
# File 'lib/ft_42.rb', line 230

def pastel
  @pastel
end

#user_sessionsObject (readonly)

Returns the value of attribute user_sessions.



230
231
232
# File 'lib/ft_42.rb', line 230

def user_sessions
  @user_sessions
end

Instance Method Details

#allObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/ft_42.rb', line 237

def all
  unless user_sessions.sessions.empty?
    active = false
    user_sessions.sessions.each do |session|
      if session.end_at - session.begin_at == 600.0
        unless active
          puts "Is #{highlight('active')} at " + highlight("#{cluster(session.host)}") + " computer #{session.host}."
        end
        unless session.primary?
          puts pastel.red.bold("Warning: Logged in on more than one computer. Please logout from #{session.host} ASAP.")
        end
        active = true
      end
    end

    unless active
      puts "Was last active " + highlight("#{ActionView::Base.new.time_ago_in_words(user_sessions.sessions.first.end_at)} ago") + " at #{highlight(cluster(user_sessions.sessions.first.host))}."
    end
  end

  puts "Has " + highlight("#{hours} #{hours == 1 ? 'hour' : 'hours'}") + " in the clusters this week, starting #{Time.current.beginning_of_week.strftime("%A, %B #{Time.current.beginning_of_week.day.ordinalize}")}. #{'Go to sleep.' if hours > 60}"

  percent_complete = ((hours.to_f / HOURS_NEEDED.to_f) * 100).round
  if (percent_complete <= 100)
    progressbar_needed = ProgressBar.create(progress_mark: "█", length: 60, format: "%t: |" + pastel.red("%B") + "| #{hours}/38 hours")
    percent_complete.times { progressbar_needed.increment }
    puts progressbar_needed
  end
end

#sessions_this_weekObject



267
268
269
270
271
272
273
# File 'lib/ft_42.rb', line 267

def sessions_this_week
  unless user_sessions.sessions.empty?
    user_sessions.sessions.each do |session|
      puts "#{session.host} from #{session.begin_at} to #{session.end_at}"
    end
  end
end