Class: Confluence::Session

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

Overview

Wraps a Confluence::Client and manages the lifetime of a session.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Session

Initializes a new session with the given arguments and sets it for other classes like Confluence::Page.

If a block is given to initialize, initialize yields with the Confluence::Client and automatically logs out of the session afterwards. Otherwise Session#destroy should be called after finished.

Parameters

arguments<Hash>

Described below.

Arguments

:url - The url of the Confluence instance. :username - The username. :password - The password.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/confluence/session.rb', line 20

def initialize(arguments = {})
  raise ArgumentError, "Required argument 'url' is missing." unless arguments.key? :url

  @client = Confluence::Client.new(arguments)

  unless @client.has_token?
    raise ArgumentError, "Required argument 'username' is missing." unless arguments.key? :username
    raise ArgumentError, "Required argument 'password' is missing." unless arguments.key? :password

    @client.(arguments[:username], arguments[:password])
  end

  # set client for records
  Confluence::Record.client = @client

  # yield if block was given and destroy afterwards
  if block_given?
    yield @client

    self.destroy
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/confluence/session.rb', line 5

def client
  @client
end

Instance Method Details

#destroyObject

Destroys the session by logging out and resets other classes like Confluence::Page.



77
78
79
80
81
82
83
84
85
86
# File 'lib/confluence/session.rb', line 77

def destroy
  # invalidate the token
  client.logout

  # client is not valid anymore
  @client = nil

  # reset client for records
  Confluence::Record.client = nil
end

#login(arguments) ⇒ Object

Logs in and acquire a new token after destroying the previous session.

Parameters

arguments<Hash>

Described below.

Arguments

:username - The username. :password - The password.

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
# File 'lib/confluence/session.rb', line 64

def (arguments)
  raise ArgumentError, "Required argument 'username' is missing." unless arguments.key? :username
  raise ArgumentError, "Required argument 'password' is missing." unless arguments.key? :password

  # log out first
  client.logout if client

  # log in with credentials
  @client.(arguments[:username], arguments[:password])
end

#tokenObject

Returns the current session token.



45
46
47
# File 'lib/confluence/session.rb', line 45

def token
  client.token if client
end

#usernameObject

Returns the currently logged in username.



51
52
53
# File 'lib/confluence/session.rb', line 51

def username
  client.username if client
end