Class: Messenger::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/messenger/slack.rb

Class Method Summary collapse

Class Method Details

.deliver(url, body, options = {}) ⇒ Object

URL format:

slack://[email protected]/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX/#room

Slack supports the notion of attachments, so the messages can be a little fancier. If body is a string, that string will simply be sent to the room as text. ‘body` can also be an array of attachments, or a single attachment. Attachment reference: api.slack.com/docs/attachments



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/messenger/slack.rb', line 15

def self.deliver(url, body, options={})
  raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
  parsed_url = matcher(url)

  options[:headers] ||= {}
  response = HTTParty.post(
    "https://#{parsed_url[:base_url]}/#{parsed_url[:key_one]}/#{parsed_url[:key_two]}/#{parsed_url[:secret]}",
    :headers => { "Content-Type" => "application/json"}.merge(options[:headers]),
    :body => build_message(parsed_url[:channel], parsed_url[:display_name], body, options)
  )
  Messenger::Result.new(success?(response), response)
end

.obfuscate(url) ⇒ Object



28
29
30
31
32
# File 'lib/messenger/slack.rb', line 28

def self.obfuscate(url)
  raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
  parsed_url = matcher(url)
  "slack://#{parsed_url[:display_name]}@#{parsed_url[:base_url]}/T********/B********/************************/#{parsed_url[:channel]}"
end

.valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/messenger/slack.rb', line 6

def self.valid_url?(url)
  !!matcher(url)
end