Module: Discord::Notifier

Defined in:
lib/discord_notifier.rb,
lib/discord_notifier/version.rb

Constant Summary collapse

VERSION =
'1.0.0'.freeze
@@config =
Config.new

Class Method Summary collapse

Class Method Details

.endpoint(config) ⇒ Object



66
67
68
69
70
# File 'lib/discord_notifier.rb', line 66

def self.endpoint(config)
  uri = URI(config[:url])
  uri.query = URI.encode_www_form(wait: true) if config[:wait]
  return uri
end

.message(content, config = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/discord_notifier.rb', line 21

def self.message(content, config = {})
  params = payload(content, config)

  if params[:file]
    send_form(params)
  else
    send_request(params)
  end
end

.payload(content, config) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/discord_notifier.rb', line 31

def self.payload(content, config)
  payload = @@config.to_h.merge(config)

  case content
  when String
    payload[:content] = content
  when Embed
    payload[:embeds] = [content.data]
  when Array
    payload[:embeds] = content.map { |embed| embed.data }
  when File
    payload[:file] = content
  else
    raise ArgumentError, 'Unsupported content type'
  end

  payload.compact
end

.send_form(params) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/discord_notifier.rb', line 56

def self.send_form(params)
  uri = endpoint(params)

  req = Discord.form_data_request(uri, params)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.request(req)
end

.send_request(params) ⇒ Object



50
51
52
53
54
# File 'lib/discord_notifier.rb', line 50

def self.send_request(params)
  Net::HTTP.post endpoint(params),
                 params.to_json,
                 'Content-Type': 'application/json'
end

.setup {|@@config| ... } ⇒ Object

Yields:

  • (@@config)


17
18
19
# File 'lib/discord_notifier.rb', line 17

def self.setup
  yield @@config
end