Class: KazeClient::Client
Overview
Execute a KazeClient::Request, stores the server’s base URL, the request and the response.
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
The server’s base URL (e.g. app.kaze.so).
-
#token ⇒ String?
readonly
The last authentication token.
Instance Method Summary collapse
-
#execute(request) ⇒ KazeClient::Response
Execute a request.
-
#initialize(base_url, token: nil) ⇒ Client
constructor
A new instance of Client.
-
#login(login = @login, password = @password) ⇒ KazeClient::Response
Stores the given login and password, stores the token received from authentication.
-
#with_token(token) ⇒ KazeClient::Client
Set the authentication token.
Constructor Details
#initialize(base_url, token: nil) ⇒ Client
Returns a new instance of Client.
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_url ⇒ String (readonly)
Returns The server’s base URL (e.g. app.kaze.so).
13 14 15 |
# File 'lib/kaze_client/client.rb', line 13 def base_url @base_url end |
#token ⇒ String? (readonly)
Returns The last authentication token.
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.
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? login 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.
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(login=@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 login.nil? || password.nil? request = KazeClient::LoginRequest.new(login: login, password: password) response = do_execute(request) # Store the token for next request and the login/password for next call @token = response['token'] @login = login @password = password response end |
#with_token(token) ⇒ KazeClient::Client
Set the authentication token
76 77 78 79 80 |
# File 'lib/kaze_client/client.rb', line 76 def with_token(token) @token = token self end |