123456789101112131415161718192021222324252627282930313233343536373839404142 |
- require 'dotenv/load'
- require 'httparty'
- require 'json'
-
- module SlackMattermostEmoji
- class MattermostClient
- def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
- @username = username
- @password = password
- @domain = domain
- @download_path = File.expand_path(download_path || './slack-emojis')
- @token = nil
-
- if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
- raise 'Mattermost username, password, and domain are all required'
- end
- end
-
- def authenticate
- url = "https://#{@domain}/api/v4/users/login"
- response = HTTParty.post(url, {
- body: JSON.generate({
- login_id: @username,
- password: @password,
- }),
- headers: {
- 'Content-Type' => 'application/json'
- }
- })
-
- if response.code == 401
- raise 'Invalid Mattermost credentials'
- end
-
- @token = response.headers['token']
- end
-
- def authenticated?
- !@token.nil?
- end
- end
- end
|