| Total Complexity | 5 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| 1 | require 'faraday' |
||
| 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 |
||
| 43 |