Class: Glip::Poster

Inherits:
Object
  • Object
show all
Defined in:
lib/glip-poster/poster.rb

Constant Summary collapse

VERSION =
'0.1.0'
GLIP_WEBHOOK_BASE_URL =
'https://hooks.glip.com/webhook/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url_or_id) ⇒ Poster

Returns a new instance of Poster.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/glip-poster/poster.rb', line 13

def initialize(webhook_url_or_id)
  set_webhook_url(webhook_url_or_id)

  @options = {}

  @http = Faraday.new(url: GLIP_WEBHOOK_BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :logger
    faraday.adapter Faraday.default_adapter # use Net::HTTP
  end
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



11
12
13
# File 'lib/glip-poster/poster.rb', line 11

def http
  @http
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/glip-poster/poster.rb', line 10

def options
  @options
end

#webhook_urlObject (readonly)

Returns the value of attribute webhook_url.



9
10
11
# File 'lib/glip-poster/poster.rb', line 9

def webhook_url
  @webhook_url
end

Instance Method Details

#send_message(message, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/glip-poster/poster.rb', line 35

def send_message(message, opts = {})
  response = @http.post do |req|
    req.url @webhook_url
    req.body = @options.merge(opts).merge(body: message)
  end

  response
end

#set_webhook_url(webhook_url_or_id) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/glip-poster/poster.rb', line 25

def set_webhook_url(webhook_url_or_id)
  if webhook_url_or_id.to_s !~ %r{/}
    @webhook_url = GLIP_WEBHOOK_BASE_URL + webhook_url_or_id
  elsif webhook_url_or_id =~ %r{^https?://}
    @webhook_url = webhook_url_or_id
  else
    fail ArgumentError, 'must include webhook URL or id argument'
  end
end