Class: KazeClient::Client

Inherits:
Object show all
Defined in:
lib/kaze_client/client.rb

Overview

Execute a KazeClient::Request, stores the server’s base URL, the request and the response.

See Also:

Author:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, token: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    The server’s base URL (e.g. app.kaze.so)

  • token (String) (defaults to: nil)

    The authentication token

Since:

  • 0.1.0



20
21
22
23
24
25
# File 'lib/kaze_client/client.rb', line 20

def initialize(base_url, token: nil)
  @base_url = base_url
  @token    = token
  @login    = nil
  @password = nil
end

Instance Attribute Details

#base_urlString (readonly)

Returns The server’s base URL (e.g. app.kaze.so).

Returns:

Since:

  • 0.1.0



13
14
15
# File 'lib/kaze_client/client.rb', line 13

def base_url
  @base_url
end

#tokenString? (readonly)

Returns The last authentication token.

Returns:

  • (String, nil)

    The last authentication token

See Also:

Since:

  • 0.1.0



16
17
18
# File 'lib/kaze_client/client.rb', line 16

def token
  @token
end

Instance Method Details

#execute(request) ⇒ KazeClient::Response

Execute a request

If the request needs authentication (meaning it includes KazeClient::Utils::AuthentifiedRequest) it sets the authentication token.

Parameters:

Returns:

Raises:

See Also:

Since:

  • 0.1.0



37
38
39
40
41
42
43
44
45
# File 'lib/kaze_client/client.rb', line 37

def execute(request)
  if request.is_a?(Utils::AuthentifiedRequest) && request.token.nil?
     if @token.nil?

    request.with_token(@token)
  end

  do_execute(request)
end

#login(login = @login, password = @password) ⇒ KazeClient::Response

Stores the given login and password, stores the token received from authentication.

Parameters:

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

    The user login.

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

    The user password.

Returns:

Raises:

See Also:

  • #do_execute

Since:

  • 0.1.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kaze_client/client.rb', line 55

def (=@login, password=@password)
  # Impossible to login using nil login or password.
  # The first call to #login must be given a login and a password.
  raise KazeClient::Error::InvalidCredentials, 'Please set login and password' if .nil? || password.nil?

  request = KazeClient::LoginRequest.new(login: , password: password)

  response = do_execute(request)

  # Store the token for next request and the login/password for next call
  @token    = response['token']
  @login    = 
  @password = password

  response
end

#with_token(token) ⇒ KazeClient::Client

Set the authentication token

Parameters:

  • token (String)

    The new authentication token

Returns:

Since:

  • 0.1.0



76
77
78
79
80
# File 'lib/kaze_client/client.rb', line 76

def with_token(token)
  @token = token

  self
end