Module: SlackNotify::Connection

Included in:
Client
Defined in:
lib/slack-notify/connection.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



30
31
32
# File 'lib/slack-notify/connection.rb', line 30

def base_url
  "https://#{@team}.slack.com"
end

#handle_response(response) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/slack-notify/connection.rb', line 16

def handle_response(response)
  unless response.success?
    if response.body.include?("\n")
      raise SlackNotify::Error
    else
      raise SlackNotify::Error.new(response.body)
    end
  end
end

#hook_urlObject



26
27
28
# File 'lib/slack-notify/connection.rb', line 26

def hook_url
  "#{base_url}/services/hooks/incoming-webhook?token=#{@token}"
end

#send_payload(payload) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/slack-notify/connection.rb', line 3

def send_payload(payload)
  conn = Faraday.new(hook_url, { timeout: 5, open_timeout: 5 }) do |c|
    c.use(Faraday::Request::UrlEncoded)
    c.adapter(Faraday.default_adapter)
  end

  response = conn.post do |req|
    req.body = JSON.dump(payload.to_hash)
  end

  handle_response(response)
end