class SlackProgress::Connection
Constants
- NEW_MESSAGE_URL
- UPDATE_MESSAGE_URL
Public Class Methods
new(token, channel_id)
click to toggle source
# File lib/slack-progress/connection.rb, line 16 def initialize(token, channel_id) @token = token @channel_id = channel_id end
Public Instance Methods
send_request(text, thread_id)
click to toggle source
# 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
Private Instance Methods
build_form_data(text, thread_id)
click to toggle source
# File lib/slack-progress/connection.rb, line 40 def build_form_data(text, thread_id) form_data = { token: @token, channel: @channel_id, text: text } form_data.merge!(ts: thread_id) if thread_id form_data.merge!(@app_options.to_h) form_data end
handle_errors(response)
click to toggle source
# File lib/slack-progress/connection.rb, line 47 def handle_errors(response) raise SlackProgress::ConnectionError, "Received #{response.status}" unless response.success? parsed_response = JSON.parse(response.body) if parsed_response['ok'] == false raise SlackProgress::InvalidRequestError, "Error: #{parsed_response['error']}" end end
parsed_response(response)
click to toggle source
# File lib/slack-progress/connection.rb, line 56 def parsed_response(response) body = response.body status = response.status thread_id = JSON.parse(body).fetch('ts', nil) Response.new(status, thread_id, body) end