| Total Complexity | 5 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
| 1 | require 'faraday' |
||
| 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 |