diff options
Diffstat (limited to 'lib/mattermost')
-rw-r--r-- | lib/mattermost/client.rb | 39 | ||||
-rw-r--r-- | lib/mattermost/command.rb | 10 | ||||
-rw-r--r-- | lib/mattermost/session.rb | 9 | ||||
-rw-r--r-- | lib/mattermost/team.rb | 7 |
4 files changed, 64 insertions, 1 deletions
diff --git a/lib/mattermost/client.rb b/lib/mattermost/client.rb new file mode 100644 index 00000000000..2a0e4b400d3 --- /dev/null +++ b/lib/mattermost/client.rb @@ -0,0 +1,39 @@ +module Mattermost + class Client + attr_reader :user + + def initialize(user) + @user = user + end + + private + + def with_session(&blk) + Session.new(user).with_session(&blk) + end + + def json_get(path, options = {}) + with_session do |session| + json_response session.get(path, options) + end + end + + def json_post(path, options = {}) + with_session do |session| + json_response session.post(path, options) + end + end + + def json_response(response) + json_response = JSON.parse(response.body) + + if response.success? + json_response + elsif json_response['message'] + raise json_response['message'] + else + raise 'Undefined error' + end + end + end +end diff --git a/lib/mattermost/command.rb b/lib/mattermost/command.rb new file mode 100644 index 00000000000..d1e4bb0eccf --- /dev/null +++ b/lib/mattermost/command.rb @@ -0,0 +1,10 @@ +module Mattermost + class Command < Client + def create(params) + response = json_post("/api/v3/teams/#{params[:team_id]}/commands/create", + body: params.to_json) + + response['token'] + end + end +end diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb index fb8d7d97f8a..f0ce51d6a71 100644 --- a/lib/mattermost/session.rb +++ b/lib/mattermost/session.rb @@ -1,5 +1,10 @@ module Mattermost - class NoSessionError < StandardError; end + class NoSessionError < StandardError + def message + 'No session could be set up, is Mattermost configured with Single Sign on?' + end + end + # This class' prime objective is to obtain a session token on a Mattermost # instance with SSO configured where this GitLab instance is the provider. # @@ -30,6 +35,8 @@ module Mattermost begin yield self + rescue Errno::ECONNREFUSED + raise NoSessionError ensure destroy end diff --git a/lib/mattermost/team.rb b/lib/mattermost/team.rb new file mode 100644 index 00000000000..784eca6ab5a --- /dev/null +++ b/lib/mattermost/team.rb @@ -0,0 +1,7 @@ +module Mattermost + class Team < Client + def all + json_get('/api/v3/teams/all') + end + end +end |