Poster.initialize()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
1
require 'faraday'
2
require 'faraday_middleware'
3
4
module Glip
5
  class Poster
6
    VERSION = '0.1.0'
7
    GLIP_WEBHOOK_BASE_URL = 'https://hooks.glip.com/webhook/'
8
9
    attr_reader :webhook_url
10
    attr_accessor :options
11
    attr_accessor :http
12
13
    def initialize(webhook_url_or_id)
14
      set_webhook_url(webhook_url_or_id)
15
16
      @options = {}
17
18
      @http = Faraday.new(url: GLIP_WEBHOOK_BASE_URL) do |faraday|
19
        faraday.request :json
20
        faraday.response :logger
21
        faraday.adapter Faraday.default_adapter # use Net::HTTP
22
      end
23
    end
24
25
    def set_webhook_url(webhook_url_or_id)
26
      if webhook_url_or_id.to_s !~ %r{/}
27
        @webhook_url = GLIP_WEBHOOK_BASE_URL + webhook_url_or_id
28
      elsif webhook_url_or_id =~ %r{^https?://}
29
        @webhook_url = webhook_url_or_id
30
      else
31
        fail ArgumentError, 'must include webhook URL or id argument'
32
      end
33
    end
34
35
    def send_message(message, opts = {})
36
      response = @http.post do |req|
37
        req.url @webhook_url
38
        req.body = @options.merge(opts).merge(body: message)
39
      end
40
41
      response
42
    end
43
  end
44
end
45