Class: Fluxerrb::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/fluxerrb/webhooks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Webhook

Returns a new instance of Webhook.



8
9
10
11
12
# File 'lib/fluxerrb/webhooks.rb', line 8

def initialize(config = {})
	@base_url = config[:base_url] || 'https://api.fluxer.app/v1'
	@webhook_id = config[:id]
	@webhook_token = config[:token]
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



7
8
9
# File 'lib/fluxerrb/webhooks.rb', line 7

def base_url
  @base_url
end

#webhook_idObject

Returns the value of attribute webhook_id.



7
8
9
# File 'lib/fluxerrb/webhooks.rb', line 7

def webhook_id
  @webhook_id
end

#webhook_tokenObject

Returns the value of attribute webhook_token.



7
8
9
# File 'lib/fluxerrb/webhooks.rb', line 7

def webhook_token
  @webhook_token
end

Class Method Details

.from_url(url, base_override = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fluxerrb/webhooks.rb', line 14

def self.from_url(url, base_override = nil)
	uri = URI(url)
	parts = uri.path.split('/')

	token = parts.pop
	id = parts.pop 

	base = base_override || "#{uri.scheme}://#{uri.host}/v1"
	new(base_url: base, id: id, token: token)
end

Instance Method Details

#send_message(content, extra_params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluxerrb/webhooks.rb', line 25

def send_message(content, extra_params = {})
	raise "[fluxerrb]: Webhook ID/Token not set" unless @webhook_id && @webhook_token

	path = "/webhooks/#{@webhook_id}/#{@webhook_token}"
	uri = URI("#{@base_url}#{path}")
	payload = { content: content }.merge(extra_params)
	http = Net::HTTP.new(uri.host, uri.port)
	if uri.scheme == 'https'
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
	end
	req = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
	req.body = payload.to_json
	response = http.request(req)

	if response.code.to_i == 429
		puts "[fluxerrb]: AN ERROR HAS OCCURED: 429 Ratelimit."
		return false
	end

	response.code.to_i == 200 || response.code.to_i == 204
end