Module: SlackNotify::Connection

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

Instance Method Summary collapse

Instance Method Details

#base_urlObject



32
33
34
# File 'lib/slack-notify/connection.rb', line 32

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

#handle_response(response) ⇒ Object



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

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



28
29
30
# File 'lib/slack-notify/connection.rb', line 28

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
15
16
# File 'lib/slack-notify/connection.rb', line 3

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

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

  handle_response(response)
end