diff options
author | Thomas Balthazar <thomas@balthazar.info> | 2016-08-10 12:25:01 +0200 |
---|---|---|
committer | Thomas Balthazar <thomas@balthazar.info> | 2016-08-11 08:52:13 +0200 |
commit | fb748daf538e43efcf8884f017391bcbfccf2ea2 (patch) | |
tree | c94ec042e25eaf630008dab7f2f57d6da175a949 /app | |
parent | 551ffc0a4d25a381e9f8f6a8d6f2793bb87f3145 (diff) | |
download | gitlab-ce-fb748daf538e43efcf8884f017391bcbfccf2ea2.tar.gz |
Replace the tinder gem by bare HTTP requests
Diffstat (limited to 'app')
-rw-r--r-- | app/models/project_services/campfire_service.rb | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/app/models/project_services/campfire_service.rb b/app/models/project_services/campfire_service.rb index 511b2eac792..5af93860d09 100644 --- a/app/models/project_services/campfire_service.rb +++ b/app/models/project_services/campfire_service.rb @@ -1,4 +1,6 @@ class CampfireService < Service + include HTTParty + prop_accessor :token, :subdomain, :room validates :token, presence: true, if: :activated? @@ -29,18 +31,53 @@ class CampfireService < Service def execute(data) return unless supported_events.include?(data[:object_kind]) - room = gate.find_room_by_name(self.room) - return true unless room - + self.class.base_uri base_uri message = build_message(data) - - room.speak(message) + speak(self.room, message, auth) end private - def gate - @gate ||= Tinder::Campfire.new(subdomain, token: token) + def base_uri + @base_uri ||= "https://#{subdomain}.campfirenow.com" + end + + def auth + # use a dummy password, as explained in the Campfire API doc: + # https://github.com/basecamp/campfire-api#authentication + @auth ||= { + basic_auth: { + username: token, + password: 'X' + } + } + end + + # Post a message into a room, returns the message Hash in case of success. + # Returns nil otherwise. + # https://github.com/basecamp/campfire-api/blob/master/sections/messages.md#create-message + def speak(room_name, message, auth) + room = rooms(auth).find { |r| r["name"] == room_name } + return nil unless room + + path = "/room/#{room["id"]}/speak.json" + body = { + body: { + message: { + type: 'TextMessage', + body: message + } + } + } + res = self.class.post(path, auth.merge(body)) + res.code == 201 ? res : nil + end + + # Returns a list of rooms, or []. + # https://github.com/basecamp/campfire-api/blob/master/sections/rooms.md#get-rooms + def rooms(auth) + res = self.class.get("/rooms.json", auth) + res.code == 200 ? res["rooms"] : [] end def build_message(push) |