diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-20 00:22:10 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-20 00:22:10 +0100 |
commit | 841960f847f04da9c427bcdb19037e2112a90890 (patch) | |
tree | ff485924db7614a01d5f3f52f66dd28056097ff8 /lib/mattermost | |
parent | 34295036e2a9ecf18ca5440a5dd6dbb0c7f05643 (diff) | |
download | gitlab-ce-841960f847f04da9c427bcdb19037e2112a90890.tar.gz |
Fix flow
Diffstat (limited to 'lib/mattermost')
-rw-r--r-- | lib/mattermost/client.rb | 39 | ||||
-rw-r--r-- | lib/mattermost/command.rb | 14 | ||||
-rw-r--r-- | lib/mattermost/team.rb | 14 |
3 files changed, 46 insertions, 21 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 index 5c6f5861a7f..d1e4bb0eccf 100644 --- a/lib/mattermost/command.rb +++ b/lib/mattermost/command.rb @@ -1,16 +1,10 @@ module Mattermost - class Command - def self.create(session, params) - response = session.post("/api/v3/teams/#{params[:team_id]}/commands/create", + class Command < Client + def create(params) + response = json_post("/api/v3/teams/#{params[:team_id]}/commands/create", body: params.to_json) - if response.success? - response.parsed_response['token'] - elsif response.parsed_response.try(:has_key?, 'message') - raise response.parsed_response['message'] - else - raise 'Failed to create a new command' - end + response['token'] end end end diff --git a/lib/mattermost/team.rb b/lib/mattermost/team.rb index 2de01eced0b..784eca6ab5a 100644 --- a/lib/mattermost/team.rb +++ b/lib/mattermost/team.rb @@ -1,15 +1,7 @@ module Mattermost - class Team - def self.all(session) - response = session.get('/api/v3/teams/all') - - if response.success? - response.parsed_response - elsif response.parsed_response.try(:has_key?, 'message') - raise response.parsed_response['message'] - else - raise 'Failed to list teams' - end + class Team < Client + def all + json_get('/api/v3/teams/all') end end end |