Sha256: 3e592434b4c5edeccf5307360bf677c97ecb8516d80777bfb044187a7bf0d4f4

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'httparty'
require_relative 'endpoint'
require_relative 'request'
require_relative 'websocket_client'

module Mattermost

	class Client
		include HTTParty
		include Mattermost::Endpoint
		include Mattermost::Request

		attr_accessor :server, :token

		def initialize(server)
			self.server = server
			self.class.base_uri "#{server}/api/v4"
		end

		def login(username, password)
			login_request = post('/users/login', :body => {:login_id => uername, :password => password}.to_json)
			self.token = login_request.headers['token']
			update_token
		end

		def logout
			self.token = nil
			update_token
		end

		def use_access_token(token)
			self.token = token
			update_token
		end

		def connected?
			getMe().success?
		end

		def connect_websocket
			# TODO raise exception then connected? == false
			@ws_client = WebSocketClient.new "#{base_uri}/websocket", token, {:headers => self.class.headers}
			yield @ws_client if block_given?
			@ws_client
		end

		def ws_client
			@ws_client
		end

		def get(path, options = {}, &block)
			self.class.get(path, options, &block)
		end

		def post(path, options = {}, &block)
			self.class.get(path, options, &block)
		end

		def put(path, options = {}, &block)
			self.class.get(path, options, &block)
		end

		def delete(path, options = {}, &block)
			self.class.delete(path, options, &block)
		end

		def base_uri
			self.class.base_uri
		end

		private

		def update_token
			self.class.headers :Authorization => "Bearer #{token}"
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mattermost-api4-ruby-0.0.5 lib/mattermost/client.rb
mattermost-api4-ruby-0.0.4 lib/mattermost/client.rb