Completed
Push — master ( 304251...d0aa14 )
by John
46s
created

Poster   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %
Metric Value
dl 0
loc 37
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 1
A set_webhook_url() 0 9 3
A send_message() 0 6 1
1
require 'faraday'
2
require 'faraday_middleware'
3
4
module Glip
5
  class Poster
6
    VERSION = '0.0.2'
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  # make requests with 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
        raise 'must include webhook URL or id argument'
32
      end
33
    end
34
35
    def send_message(message, opts={})
36
      return @http.post do |req|
37
        req.url @webhook_url
38
        req.body = @options.merge(opts).merge({body: message})
39
      end
40
    end
41
  end
42
end
43