Class: SlackProgress::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-progress/connection.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

NEW_MESSAGE_URL =
'https://slack.com/api/chat.postMessage'
UPDATE_MESSAGE_URL =
'https://slack.com/api/chat.update'

Instance Method Summary collapse

Constructor Details

#initialize(token, channel_id) ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
# File 'lib/slack-progress/connection.rb', line 16

def initialize(token, channel_id)
  @token = token
  @channel_id = channel_id
end

Instance Method Details

#send_request(text, thread_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slack-progress/connection.rb', line 21

def send_request(text, thread_id)
  return unless text

  url = thread_id.nil? ? NEW_MESSAGE_URL : UPDATE_MESSAGE_URL
  data = build_form_data(text, thread_id)

  raw_response = Faraday.post(url) do |req|
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = URI.encode_www_form(data)
    req.options[:timeout] = 2
    req.options[:open_timeout] = 2
  end

  handle_errors(raw_response)
  parsed_response(raw_response)
end