Skip to content

Instantly share code, notes, and snippets.

@gordysc
Created February 4, 2019 02:45
Show Gist options
  • Save gordysc/ff50bd8d8554feaaa05fc29f2b0ee8ea to your computer and use it in GitHub Desktop.
Save gordysc/ff50bd8d8554feaaa05fc29f2b0ee8ea to your computer and use it in GitHub Desktop.
class SlackWebhook
attr_accessor :webhook
def initialize(webhook)
@webhook = webhook
end
def send_message(text)
uri = URI(webhook)
connection.post do |req|
req.url uri.path
req.headers['Content-Type'] = 'application/json'
req.body = { text: text }.to_json
end
end
private
def connection
Faraday.new(url: "https://hooks.slack.com") do |faraday|
faraday.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true }
faraday.adapter Faraday.default_adapter
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment