class HangoutsChat::Sender

Provide methods to send messages to Hangouts Chat rooms using webhooks API

Attributes

url[R]

@return [String] Webhook URL, given on initialization

Public Class Methods

new(webhook_url) click to toggle source

Creates Sender object @param webhook_url [String] URL for incoming webhook

# File lib/hangouts_chat.rb, line 14
def initialize(webhook_url)
  @url = webhook_url
  @http = HTTP.new(@url)
end

Public Instance Methods

card(header, sections, thread: nil) click to toggle source

Sends Card Message @since 0.0.4 @param header [Hash] card header content @param sections [Array<Hash>] card widgets array @param thread [String] it will be sent as a reply (`nil` is a new thread will be created) @return [Net::HTTPResponse] response object

# File lib/hangouts_chat.rb, line 34
def card(header, sections, thread: nil)
  payload = { cards: [header: header, sections: sections] }
  send_request(payload, thread: thread)
end
simple(text, thread: nil) click to toggle source

Sends Simple Text Message @param text [String] text to send to room @param thread [String] it will be sent as a reply (`nil` is a new thread will be created) @return [Net::HTTPResponse] response object

# File lib/hangouts_chat.rb, line 23
def simple(text, thread: nil)
  payload = { text: text }
  send_request(payload, thread: thread)
end

Private Instance Methods

send_request(payload, thread: nil) click to toggle source

Sends payload and check response @param payload [Hash] data to send by POST @param thread [String] it will be sent as a reply (`nil` is a new thread will be created) @return [Net::HTTPResponse] response object @raise [APIError] if got unsuccessful response

# File lib/hangouts_chat.rb, line 46
def send_request(payload, thread: nil)
  payload[:thread] = { name: thread } if thread

  response = @http.post payload
  raise APIError, response unless response.is_a?(Net::HTTPSuccess)
  response
end