/
blaze.rb
68 lines (54 loc) · 1.59 KB
/
blaze.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'net/http'
require 'json'
require 'ostruct'
require 'forwardable'
require 'capistrano/blaze/version'
require 'capistrano/blaze/messages'
require 'capistrano/blaze/recipes' if defined?(Capistrano::Configuration)
module Capistrano
module Blaze
extend self
extend Forwardable
def_delegators :Messages, :start, :failure, :success, :test
def configure
yield configuration
end
def configuration
@config ||= OpenStruct.new
end
def speak(message)
validate_configuration
port = configuration.ssl ? 443 : 80
req = Net::HTTP::Post.new("/room/#{configuration.room_id}/speak.json")
req.basic_auth configuration.token, 'X'
req.body = { :message => { :body => message } }.to_json
req.content_type = "application/json"
req["User-Agent"] = "Capistrano::Blaze"
res = Net::HTTP.start("#{configuration.account}.campfirenow.com", port, :use_ssl => configuration.ssl) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
warn "Campfire message sent!"
else
warn "Campfire communication failed!"
warn res.inspect
warn res.body.inspect
end
end
def validate_configuration
%w(account room_id token).each do |option|
if configuration.send(option).nil?
fail MissingConfigurationOption.new(option)
end
end
end
class MissingConfigurationOption < RuntimeError
def initialize(option)
@option = option
end
def to_s
"Please specify the #{@option} option"
end
end
end
end