Skip to content

Instantly share code, notes, and snippets.

@arunthampi
Created September 7, 2016 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunthampi/71f848df098c871dc855c603f9792617 to your computer and use it in GitHub Desktop.
Save arunthampi/71f848df098c871dc855c603f9792617 to your computer and use it in GitHub Desktop.
require 'uri'
class Slack
API_URL = ENV['SLACK_API_URL']
def initialize(token)
@token = token
end
def call(slack_api, method, params = {}, &block)
params = params.select { |k,v| v.present? }
params.merge!(token: @token)
encoded_params = URI.encode_www_form(params)
opts = {
omit_default_port: true,
idempotent: true,
retry_limit: 6,
read_timeout: 360,
connect_timeout: 360
}
url = "#{API_URL}/#{slack_api}"
if method.to_s.downcase == 'get'
url = "#{url}?#{encoded_params}"
else
opts[:body] = URI.encode_www_form(params)
opts[:headers] = { "Content-Type" => "application/x-www-form-urlencoded" }
end
if !block_given?
connection = Excon.new(url, opts)
response = connection.request(method: method)
return JSON.parse(response.body)
else
opts[:response_block] = block
connection = Excon.new(url, opts)
connection.request(method: method)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment