require 'json' require 'faraday' module ErrandBoy module Gateway class Slack def initialize(request) @request = request end def call send(@request.action) rescue NoMethodError raise NoActionError, "#{@request.action}: action not found." end def invite response = connect.post 'api/users.admin.invite', params result = JSON.parse response.body unless result['ok'] @processed = true if result['error'] == 'already_invited' raise SlackInviteMissingError, result['error'] unless processed? end end def team @request.destination['team'] end def url "https://#{team}.slack.com/" end def processed? @processed end private def connect Faraday.new(url: url) do |faraday| faraday.request :url_encoded # faraday.response :logger faraday.adapter Faraday.default_adapter end end def params { email: @request.register['email'], token: ErrandBoy.slack_access_token, set_active: true } end end end end