File: hangouts_chat.rb

package info (click to toggle)
ruby-hangouts-chat 0.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 124 kB
  • sloc: ruby: 132; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require_relative 'hangouts_chat/version'
require_relative 'hangouts_chat/http'
require_relative 'hangouts_chat/exceptions'

# Main namespace
module HangoutsChat
  # Provide methods to send messages to Hangouts Chat rooms using webhooks API
  class Sender
    # @return [String] Webhook URL, given on initialization
    attr_reader :url

    # Creates Sender object
    # @param webhook_url [String] URL for incoming webhook
    def initialize(webhook_url)
      @url = webhook_url
      @http = HTTP.new(@url)
    end

    # Sends Simple Text Message
    # @param text [String] text to send to room
    # @return [Net::HTTPResponse] response object
    def simple(text)
      payload = { text: text }
      send_request(payload)
    end

    # Sends Card Message
    # @since 0.0.4
    # @param header [Hash] card header content
    # @param sections [Array<Hash>] card widgets array
    # @return [Net::HTTPResponse] response object
    def card(header, sections)
      payload = { cards: [header: header, sections: sections] }
      send_request(payload)
    end

    private

    # Sends payload and check response
    # @param payload [Hash] data to send by POST
    # @return [Net::HTTPResponse] response object
    # @raise [APIError] if got unsuccessful response
    def send_request(payload)
      response = @http.post payload
      raise APIError, response unless response.is_a?(Net::HTTPSuccess)
      response
    end
  end
end