Class: PostCommit::Hooks::Campfire

Inherits:
Base
  • Object
show all
Defined in:
lib/post_commit/hooks/campfire.rb

Overview

To send a Campfire post commit, you have to setup your subdomain, API token and room.

post_commit :campfire do
  authorize :subdomain => "mycompany", :token => "TVfD8rB0x1sze8nZ4P1vaO5wOWM", :room => 666, :ssl => true
  post "Some message"
end

You can specify the message type. See PostCommit::Hooks::Campfire#post for usage.

Constant Summary

MESSAGE_TYPE =
{
  :text    => "TextMessage",
  :paste   => "PasteMessage",
  :twitter => "TwitterMessage"
}

Instance Attribute Summary

Attributes inherited from Base

#credentials, #options, #request, #response, #uri

Instance Method Summary collapse

Methods inherited from Base

#authorize, #convert_to_params, #convert_to_xml, inherited, #initialize, #set

Constructor Details

This class inherits a constructor from PostCommit::Hooks::Base

Instance Method Details

#post(message, options = {}) ⇒ Object

Post message to Campfire.

post "Hi Campfire!"
#=> send text message

post "Pasted code", :type => :paste
#=> formatted paste message with more link and scroll bar

post "http://twitter.com/dhh/status/9688523285", :type => :twitter
#=> display the specified Twitter status


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/post_commit/hooks/campfire.rb', line 28

def post(message, options = {})
  options = {:type => :text}.merge(options)
  protocol = credentials[:ssl] ? "https" : "http"
  @uri = URI.parse("#{protocol}://#{credentials[:subdomain]}.campfirenow.com/room/#{credentials[:room]}/speak.json")
  http = Net::HTTP.new(uri.host, uri.port)

  @request = Net::HTTP::Post.new(uri.path)
  @request.basic_auth credentials[:token], "x"
  @request.content_type = "application/json"
  @request.body = {:message => {
    :type => MESSAGE_TYPE[options[:type].to_sym],
    :body => message.to_s
  }}.to_json

  @response = http.request(@request)

  if @response.code == "201"
    JSON.parse response.body
  else
    false
  end
rescue Exception
  false
end