You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mattermost_client.rb 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. require 'dotenv/load'
  2. require 'httparty'
  3. require 'json'
  4. module SlackMattermostEmoji
  5. class MattermostClient
  6. def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
  7. @username = username
  8. @password = password
  9. @domain = domain
  10. @download_path = File.expand_path(download_path || './slack-emojis')
  11. @token = nil
  12. if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
  13. raise 'Mattermost username, password, and domain are all required'
  14. end
  15. end
  16. def authenticate
  17. url = "https://#{@domain}/api/v4/users/login"
  18. response = HTTParty.post(url, {
  19. body: JSON.generate({
  20. login_id: @username,
  21. password: @password,
  22. }),
  23. headers: {
  24. 'Content-Type' => 'application/json'
  25. }
  26. })
  27. if response.code == 401
  28. raise 'Invalid Mattermost credentials'
  29. end
  30. @token = response.headers['token']
  31. end
  32. def authenticated?
  33. !@token.nil?
  34. end
  35. end
  36. end