Sha256: f46b7e339a56edbf335127881ba0e5ba190d97b6423410ec44ca756faa3a6e6c

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require 'slackistrano/version'
require 'net/http'
require 'json'

load File.expand_path("../slackistrano/tasks/slack.rake", __FILE__)

module Slackistrano

  #
  #
  #
  def self.post(team: nil, token: nil, webhook: nil, via_slackbot: false, payload: {})
    if via_slackbot
      post_as_slackbot(team: team, token: token, webhook: webhook, payload: payload)
    else
      post_as_webhook(team: team, token: token, webhook: webhook, payload: payload)
    end
  rescue => e
    puts "There was an error notifying Slack."
    puts e.inspect
  end

  #
  #
  #
  def self.post_as_slackbot(team: nil, token: nil, webhook: nil, payload: {})
    uri = URI(URI.encode("https://#{team}.slack.com/services/hooks/slackbot?token=#{token}&channel=#{payload[:channel]}"))

    text = payload[:attachments].collect { |a| a[:text] }.join("\n")

    Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      http.request_post uri.request_uri, text
    end
  end

  #
  #
  #
  def self.post_as_webhook(team: nil, token: nil, webhook: nil, payload: {})
    params = {'payload' => payload.to_json}

    if webhook.nil?
      webhook = "https://#{team}.slack.com/services/hooks/incoming-webhook"
      params.merge!('token' => token)
    end

    uri = URI(webhook)
    Net::HTTP.post_form(uri, params)
  end


end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
slackistrano-1.0.0 lib/slackistrano.rb
slackistrano-0.1.12 lib/slackistrano.rb
slackistrano-0.1.11 lib/slackistrano.rb
slackistrano-0.1.10 lib/slackistrano.rb
slackistrano-0.1.9 lib/slackistrano.rb
slackistrano-0.1.8 lib/slackistrano.rb
slackistrano-0.1.7 lib/slackistrano.rb
slackistrano-0.1.6 lib/slackistrano.rb