Created
September 7, 2016 00:45
-
-
Save arunthampi/71f848df098c871dc855c603f9792617 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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