Class: Teams

Inherits:
Object
  • Object
show all
Defined in:
lib/teams.rb,
lib/teams/version.rb

Constant Summary collapse

VERSION =
'1.1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url = nil) ⇒ Teams

Returns a new instance of Teams.



2
3
4
# File 'lib/teams.rb', line 2

def initialize(webhook_url = nil)
  @webhook_url = webhook_url || ENV['TEAMS_WEBHOOK_URL'] || ENV['MSTEAMS_RUBY_CLIENT_WEBHOOK_URL']
end

Instance Method Details

#post(text, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/teams.rb', line 6

def post(text, options = {})
  uri = URI.parse(@webhook_url)

  request = Net::HTTP::Post.new(uri.request_uri)
  request['Content-Type'] = 'application/json'
  request.body = {
    title: options[:title],
    text: options[:text2html] ? text2html(text) : text,
    themeColor: options[:themeColor],
    summary: options[:summary] || text
  }.to_json

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.start { |h| h.request(request) }
end

#text2html(text) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/teams.rb', line 24

def text2html(text)
  html = text.clone
  URI::DEFAULT_PARSER.extract(text, %w[http https]).uniq.each do |url|
    html.gsub!(url, %(<a href="#{url}">#{url}</a>))
  end
  html.gsub!("\n", "<br>\n")
  html
end