Class: HammerCLIForeman::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_path) ⇒ Session

Returns a new instance of Session.



60
61
62
63
64
65
66
67
68
69
# File 'lib/hammer_cli_foreman/sessions.rb', line 60

def initialize(session_path)
  @session_path = File.expand_path(session_path)
  if File.exist?(@session_path) && !File.zero?(@session_path)
    session_data = JSON.parse(File.read(@session_path))
    @id = session_data['id']
    @user_name = session_data['user_name']
  end
rescue JSON::ParserError
  warn _('Invalid session data. Resetting the session.')
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



58
59
60
# File 'lib/hammer_cli_foreman/sessions.rb', line 58

def id
  @id
end

#user_nameObject

Returns the value of attribute user_name.



58
59
60
# File 'lib/hammer_cli_foreman/sessions.rb', line 58

def user_name
  @user_name
end

Instance Method Details

#destroyObject



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

def destroy
  @id = nil
  store
end

#storeObject



71
72
73
74
75
76
77
78
79
# File 'lib/hammer_cli_foreman/sessions.rb', line 71

def store
  File.open(@session_path,"w") do |f|
    f.write({
      id: id,
      user_name: user_name
    }.to_json)
  end
  File.chmod(0600, @session_path)
end

#valid?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/hammer_cli_foreman/sessions.rb', line 86

def valid?
  !id.nil? && !id.empty?
end