Class: Chatrix::Api::Session
- Inherits:
-
ApiComponent
- Object
- ApiComponent
- Chatrix::Api::Session
- Defined in:
- lib/chatrix/api/session.rb
Overview
Contains methods to use session related endpoints in the API.
Instance Method Summary collapse
-
#add_threepid(data) ⇒ Boolean
Adds third-party identification information to the user account.
-
#login(method, options) ⇒ Hash
Performs a login attempt.
-
#logout ⇒ Boolean
Logs out.
-
#refresh(token = nil) ⇒ Hash
Gets a new access token to use for API calls when the current one expires.
-
#register(data, kind = 'user') ⇒ Hash
Registers a new user on the homeserver.
-
#set_password(password, auth = nil) ⇒ Boolean
Set a new password for the current account.
-
#threepids ⇒ Array
Gets third-party IDs associated with the current account.
Methods inherited from ApiComponent
Constructor Details
This class inherits a constructor from Chatrix::Api::ApiComponent
Instance Method Details
#add_threepid(data) ⇒ Boolean
Adds third-party identification information to the user account.
21 22 23 |
# File 'lib/chatrix/api/session.rb', line 21 def add_threepid(data) make_request(:post, '/account/3pid', content: data).code == 200 end |
#login(method, options) ⇒ Hash
A successful login will update the access_token to the new one returned from the login response.
Performs a login attempt.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/chatrix/api/session.rb', line 99 def login(method, ) response = make_request( :post, '/login', content: { type: method }.merge!() ) # Update the local access token @matrix.access_token = response['access_token'] response.parsed_response end |
#logout ⇒ Boolean
This will invalidate the access token. It will no longer be valid for further API calls.
Logs out.
119 120 121 122 123 124 125 126 |
# File 'lib/chatrix/api/session.rb', line 119 def logout response = make_request :post, '/logout' # A successful logout means the access token has been invalidated @matrix.access_token = nil response.code == 200 end |
#refresh(token = nil) ⇒ Hash
On success, the internal access_token and
refresh_token
will be updated automatically for use in
subsequent API calls.
Gets a new access token to use for API calls when the current one expires.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chatrix/api/session.rb', line 141 def refresh(token = nil) refresh_token = token || @refresh_token || @access_token response = make_request( :post, '/tokenrefresh', content: { refresh_token: refresh_token } ) @matrix.access_token = response['access_token'] @refresh_token = response['refresh_token'] response.parsed_response end |
#register(data, kind = 'user') ⇒ Hash
On a successful registration, the
access_token and refresh_token
will be
updated to the values returned by the server.
Registers a new user on the homeserver.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/chatrix/api/session.rb', line 68 def register(data, kind = 'user') response = make_request( :post, '/register', params: { kind: kind }, content: data ) @matrix.access_token = response['access_token'] @refresh_token = response['refresh_token'] response.parsed_response end |
#set_password(password, auth = nil) ⇒ Boolean
The server may request additional authentication as per the official documentation on the "User-Interactive Authentication API".
Set a new password for the current account.
35 36 37 38 39 40 |
# File 'lib/chatrix/api/session.rb', line 35 def set_password(password, auth = nil) data = { new_password: password } data[:auth] = auth if auth make_request(:post, '/account/password', content: data).code == 200 end |
#threepids ⇒ Array
Gets third-party IDs associated with the current account.
12 13 14 |
# File 'lib/chatrix/api/session.rb', line 12 def threepids make_request(:get, '/account/3pid')['threepids'] end |