Class: Fluent::GoogleChatClient::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/google_chat_client.rb

Overview

The base framework of google_chat client

Direct Known Subclasses

WebApi

Constant Summary collapse

SCOPE =
'https://www.googleapis.com/auth/chat.bot'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyfile = nil, https_proxy = nil) ⇒ Base

Returns a new instance of Base.

Parameters:



35
36
37
38
39
# File 'lib/fluent/plugin/google_chat_client.rb', line 35

def initialize(keyfile = nil, https_proxy = nil)
  self.keyfile     = keyfile    if keyfile
  self.https_proxy = https_proxy if https_proxy
  @log = Logger.new('/dev/null')
end

Instance Attribute Details

#debug_devObject

Returns the value of attribute debug_dev.



17
18
19
# File 'lib/fluent/plugin/google_chat_client.rb', line 17

def debug_dev
  @debug_dev
end

#https_proxyObject

Returns the value of attribute https_proxy.



18
19
20
# File 'lib/fluent/plugin/google_chat_client.rb', line 18

def https_proxy
  @https_proxy
end

#keyfileObject

Returns the value of attribute keyfile.



18
19
20
# File 'lib/fluent/plugin/google_chat_client.rb', line 18

def keyfile
  @keyfile
end

#logObject

Returns the value of attribute log.



17
18
19
# File 'lib/fluent/plugin/google_chat_client.rb', line 17

def log
  @log
end

Instance Method Details

#authorizeGoogle::Auth::UserRefreshCredentials

Ensure valid credentials, either by restoring from the saved credentials files or intitiating an OAuth2 authorization. If authorization is required, the user’s default browser will be launched to approve the request.

Returns:

  • (Google::Auth::UserRefreshCredentials)

    OAuth2 credentials



47
48
49
50
51
52
53
# File 'lib/fluent/plugin/google_chat_client.rb', line 47

def authorize
  credentials = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(self.keyfile),
      scope: SCOPE
  )
  credentials
end

#post(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/google_chat_client.rb', line 68

def post(params)
  chat = Chat::HangoutsChatService.new
  chat.authorization = authorize
  message = Chat::Message.new
  message.text = params[:text].length > 4096 ? params[:text][0...4096] : params[:text]

  chat.create_space_message(
      'spaces/%s' % params[:space],
      message
  )
end

#proxy_classObject



64
65
66
# File 'lib/fluent/plugin/google_chat_client.rb', line 64

def proxy_class
  @proxy_class ||= Net::HTTP
end