bzhipchat 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mojo Tech
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,67 @@
1
+ h1. HipChat Wrapper
2
+
3
+ A very basic wrapper for the HipChat HTTP API.
4
+
5
+ h2. Usage
6
+
7
+ bc.. client = HipChat::Client.new(api_token)
8
+ client['my room'].send('username', 'I talk')
9
+
10
+ # Send notifications to users (default false)
11
+ client['my room'].send('username', 'I quit!', :notify => true)
12
+
13
+ # Color it red. or "yellow", "green", "purple", "random" (default "yellow")
14
+ client['my room'].send('username', 'Build failed!', :color => 'red')
15
+
16
+ h2. Capistrano
17
+
18
+ bc.. require 'hipchat/capistrano'
19
+
20
+ set :hipchat_token, "<your token>"
21
+ set :hipchat_room_name, "Your room"
22
+ set :hipchat_announce, false # notify users
23
+ set :hipchat_color, 'green' #finished deployment message color
24
+ set :hipchat_failed_color, 'red' #cancelled deployment message color
25
+
26
+ h3. Who did it?
27
+
28
+ To determine the user that is currently running the deploy, the capistrano tasks will look for the following:
29
+
30
+ # The $HIPCHAT_USER environment variable
31
+ # The hipchat_human capistrano var.
32
+ # The git user.name var.
33
+ # The $USER environment variable.
34
+
35
+ h2. Rails 3 Rake Task
36
+
37
+ Send a message using a rake task:
38
+
39
+ bc. rake hipchat:send["hello world"]
40
+
41
+ Options like the room, API token, user name and notification flag can be set in YAML.
42
+
43
+ RAILS_ROOT/config/hipchat.yml:
44
+
45
+ bc.. token: "<your token>"
46
+ room: "Your room"
47
+ user: "Your name" # Default to `whoami`
48
+ notify: true # Defaults to false
49
+
50
+ h2. Engine Yard
51
+
52
+ Use a "deploy hook":http://bit.ly/qnbIkP to send messages from Engine Yard's Cloud platform.
53
+
54
+ RAILS_ROOT/deploy/after_restart.rb:
55
+
56
+ bc.. on_app_master do
57
+ message = "Deploying revision #{revision[0...6]} to #{node[:environment][:name]}"
58
+ message += " (with migrations)" if migrate?
59
+ message += "."
60
+
61
+ # Send a message via rake task assuming a hipchat.yml in your config like above
62
+ run "cd #{release_path} && bundle exec rake hipchat:send MESSAGE='#{message}'"
63
+ end
64
+
65
+ h2. Copyright
66
+
67
+ Copyright (c) 2010 Mojo Tech. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hipchat"
8
+ gem.summary = %Q{Ruby library to interact with HipChat}
9
+ gem.description = %Q{Ruby library to interact with HipChat}
10
+ gem.email = "gems@mojotech.com"
11
+ gem.homepage = "http://github.com/mojotech/hipchat"
12
+ gem.authors = ["MojoTech"]
13
+ gem.add_dependency "httparty"
14
+ gem.add_development_dependency "rspec", "~> 2.0"
15
+ gem.add_development_dependency "rr", "~> 1.0"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ task :default => :spec
23
+
24
+ require 'rake/rdoctask'
25
+ Rake::RDocTask.new do |rdoc|
26
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
27
+
28
+ rdoc.rdoc_dir = 'rdoc'
29
+ rdoc.title = "hipchat #{version}"
30
+ rdoc.rdoc_files.include('README*')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.1
data/hipchat.gemspec ADDED
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bzhipchat"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Betazeta"]
12
+ s.date = "2013-08-27"
13
+ s.description = "Ruby library to interact with HipChat, forked by boris.quiroz@betazeta.com"
14
+ s.email = "boris.quiroz@betazeta.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.textile",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "hipchat.gemspec",
26
+ "lib/hipchat.rb",
27
+ "lib/hipchat/capistrano.rb",
28
+ "lib/hipchat/chef.rb",
29
+ "lib/hipchat/rails3_tasks.rb",
30
+ "lib/hipchat/railtie.rb",
31
+ "spec/hipchat_spec.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/boris/hipchat"
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.10"
38
+ s.summary = "Ruby library to interact with HipChat, forked by boris.quiroz@betazeta.com"
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.0"])
46
+ s.add_development_dependency(%q<rr>, ["~> 1.0"])
47
+ else
48
+ s.add_dependency(%q<httparty>, [">= 0"])
49
+ s.add_dependency(%q<rspec>, ["~> 2.0"])
50
+ s.add_dependency(%q<rr>, ["~> 1.0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<httparty>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, ["~> 2.0"])
55
+ s.add_dependency(%q<rr>, ["~> 1.0"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,97 @@
1
+ require 'hipchat'
2
+
3
+ Capistrano::Configuration.instance(:must_exist).load do
4
+ set :hipchat_send_notification, false
5
+ set :hipchat_with_migrations, false
6
+
7
+ namespace :hipchat do
8
+ task :set_client do
9
+ set :hipchat_client, HipChat::Client.new(hipchat_token)
10
+ end
11
+
12
+ task :trigger_notification do
13
+ set :hipchat_send_notification, true
14
+ end
15
+
16
+ task :configure_for_migrations do
17
+ set :hipchat_with_migrations, true
18
+ end
19
+
20
+ task :notify_deploy_started do
21
+ if hipchat_send_notification
22
+ on_rollback do
23
+ send_options.merge!(:color => failed_message_color)
24
+ hipchat_client[hipchat_room_name].
25
+ send(deploy_user, "#{human} cancelled deployment of #{deployment_name} to #{env}.", send_options)
26
+ end
27
+
28
+ message = "#{human} esta haciendo deploy #{deployment_name} a #{stage}"
29
+ message << " (with migrations)" if hipchat_with_migrations
30
+ message << "."
31
+
32
+ hipchat_client[hipchat_room_name].send(deploy_user, message, send_options)
33
+ end
34
+ end
35
+
36
+ task :notify_deploy_finished do
37
+ hipchat_client[hipchat_room_name].
38
+ send(deploy_user, "#{human} termino deploy #{deployment_name} a #{stage}.", send_options)
39
+ end
40
+
41
+ def send_options
42
+ return @send_options if defined?(@send_options)
43
+ @send_options = message_color ? {:color => message_color} : {}
44
+ @send_options.merge!(:notify => message_notification)
45
+ @send_options
46
+ end
47
+
48
+ def deployment_name
49
+ if branch
50
+ name = "#{application}/#{branch}"
51
+ name += " (revision #{real_revision[0..7]})" if real_revision
52
+ name
53
+ else
54
+ application
55
+ end
56
+ end
57
+
58
+ def message_color
59
+ fetch(:hipchat_color, nil)
60
+ end
61
+
62
+ def failed_message_color
63
+ fetch(:hipchat_failed_color, "red")
64
+ end
65
+
66
+ def message_notification
67
+ fetch(:hipchat_announce, false)
68
+ end
69
+
70
+ def deploy_user
71
+ fetch(:hipchat_deploy_user, "Deploy")
72
+ end
73
+
74
+ def human
75
+ ENV['HIPCHAT_USER'] ||
76
+ fetch(:hipchat_human,
77
+ if (u = %x{git config user.name}.strip) != ""
78
+ u
79
+ elsif (u = ENV['USER']) != ""
80
+ u
81
+ else
82
+ "Someone"
83
+ end)
84
+ end
85
+
86
+ def env
87
+ fetch(:hipchat_env, fetch(:rack_env, fetch(:rails_env, "production")))
88
+ end
89
+ end
90
+
91
+ before "hipchat:notify_deploy_started", "hipchat:set_client"
92
+ before "deploy", "hipchat:trigger_notification"
93
+ before "deploy:migrations", "hipchat:trigger_notification", "hipchat:configure_for_migrations"
94
+ before "deploy:update_code", "hipchat:notify_deploy_started"
95
+ after "deploy", "hipchat:notify_deploy_finished"
96
+ after "deploy:migrations", "hipchat:notify_deploy_finished"
97
+ end
@@ -0,0 +1,31 @@
1
+ require 'hipchat'
2
+
3
+ #
4
+ # Provides a Chef exception handler so you can send information about
5
+ # chef-client failures to a HipChat room.
6
+ #
7
+ # Docs: http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers
8
+ #
9
+ # Install - add the following to your client.rb:
10
+ # require 'hipchat/chef'
11
+ # hipchat_handler = HipChat::NotifyRoom.new("<api token>", "<room name>")
12
+ # exception_handlers << hipchat_handler
13
+ #
14
+
15
+ module HipChat
16
+ class NotifyRoom < Chef::Handler
17
+
18
+ def initialize(api_token, room_name, notify_users=false)
19
+ @api_token = api_token
20
+ @room_name = room_name
21
+ @notify_users = notify_users
22
+ end
23
+
24
+ def report
25
+ msg = "Failure on #{node.name}: #{run_status.formatted_exception}"
26
+
27
+ client = HipChat::Client.new(@api_token)
28
+ client[@room_name].send('Chef', msg, :notify => @notify_users)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'hipchat'
2
+
3
+ namespace :hipchat do
4
+ desc "Sends a HipChat message as a particular user"
5
+ task :send do
6
+ required_options = [:message, :room, :token, :user]
7
+ config_file = Rails.root.join 'config', 'hipchat.yml'
8
+
9
+ options = {
10
+ :message => ENV['MESSAGE'],
11
+ :user => ENV['HIPCHAT_USER'],
12
+ :notify => ENV['NOTIFY'],
13
+ :room => ENV['ROOM'],
14
+ :token => ENV['TOKEN']
15
+ }.reject { |k, v| v.blank? }
16
+
17
+ system_options = {
18
+ :user => ENV['USER']
19
+ }.reject { |k, v| v.blank? }
20
+
21
+ if File.exists? config_file
22
+ options.reverse_merge! YAML.load_file(config_file).symbolize_keys
23
+ end
24
+
25
+ options.reverse_merge! system_options
26
+
27
+ options[:notify] = options[:notify].to_s != 'false'
28
+
29
+ if (missing_options = required_options - options.keys).size > 0
30
+ puts "HipChat needs #{missing_options.to_sentence} to send!"
31
+ exit
32
+ end
33
+
34
+ client = HipChat::Client.new(options[:token])
35
+
36
+ client[options[:room]].send(options[:user], options[:message], :notify => options[:notify])
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ require 'hipchat'
2
+
3
+ module HipChat
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ require 'hipchat/rails3_tasks'
7
+ end
8
+ end
9
+ end
data/lib/hipchat.rb ADDED
@@ -0,0 +1,93 @@
1
+ require 'httparty'
2
+ require 'ostruct'
3
+
4
+ require 'hipchat/railtie' if defined?(Rails::Railtie)
5
+
6
+ module HipChat
7
+ class UnknownRoom < StandardError; end
8
+ class Unauthorized < StandardError; end
9
+ class UnknownResponseCode < StandardError; end
10
+
11
+ class Client
12
+ include HTTParty
13
+
14
+ base_uri 'https://api.hipchat.com/v1/rooms'
15
+ format :json
16
+
17
+ def initialize(token)
18
+ @token = token
19
+ end
20
+
21
+ def rooms
22
+ @rooms ||= self.class.get("/list", :query => {:auth_token => @token})['rooms'].
23
+ map { |r| Room.new(@token, r) }
24
+ end
25
+
26
+ def [](name)
27
+ Room.new(@token, :room_id => name)
28
+ end
29
+ end
30
+
31
+ class Room < OpenStruct
32
+ include HTTParty
33
+
34
+ base_uri 'https://api.hipchat.com/v1/rooms'
35
+
36
+ def initialize(token, params)
37
+ @token = token
38
+
39
+ super(params)
40
+ end
41
+
42
+ # Send a message to this room.
43
+ #
44
+ # Usage:
45
+ #
46
+ # # Default
47
+ # send 'nickname', 'some message'
48
+ #
49
+ # # Notify users and color the message red
50
+ # send 'nickname', 'some message', :notify => true, :color => 'red'
51
+ #
52
+ # # Notify users (deprecated)
53
+ # send 'nickname', 'some message', true
54
+ #
55
+ # Options:
56
+ #
57
+ # +color+:: "yellow", "red", "green", "purple", or "random"
58
+ # (default "yellow")
59
+ # +notify+:: true or false
60
+ # (default false)
61
+ def send(from, message, options_or_notify = {})
62
+ options = if options_or_notify == true or options_or_notify == false
63
+ warn "DEPRECATED: Specify notify flag as an option (e.g., :notify => true)"
64
+ { :notify => options_or_notify }
65
+ else
66
+ options_or_notify || {}
67
+ end
68
+
69
+ options = { :color => 'yellow', :notify => false }.merge options
70
+
71
+ response = self.class.post('/message',
72
+ :query => { :auth_token => @token },
73
+ :body => {
74
+ :room_id => room_id,
75
+ :from => from,
76
+ :message => message,
77
+ :color => options[:color],
78
+ :notify => options[:notify] ? 1 : 0
79
+ }
80
+ )
81
+
82
+ case response.code
83
+ when 200; true
84
+ when 404
85
+ raise UnknownRoom, "Unknown room: `#{room_id}'"
86
+ when 401
87
+ raise Unauthorized, "Access denied to room `#{room_id}'"
88
+ else
89
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe HipChat do
4
+ subject { HipChat::Client.new("blah") }
5
+
6
+ let(:room) { subject["Hipchat"] }
7
+
8
+ # Helper for mocking room message post requests
9
+ def mock_successful_send(from, message, options={})
10
+ options = {:color => 'yellow', :notify => 0}.merge(options)
11
+ mock(HipChat::Room).post("/message",
12
+ :query => {:auth_token => "blah"},
13
+ :body => {:room_id => "Hipchat",
14
+ :from => "Dude",
15
+ :message => "Hello world",
16
+ :color => options[:color],
17
+ :notify => options[:notify]}) {
18
+ OpenStruct.new(:code => 200)
19
+ }
20
+ end
21
+
22
+ describe "sends a message to a room" do
23
+ it "successfully without custom options" do
24
+ mock_successful_send 'Dude', 'Hello world'
25
+
26
+ room.send("Dude", "Hello world").should be_true
27
+ end
28
+
29
+ it "successfully with notifications on as option" do
30
+ mock_successful_send 'Dude', 'Hello world', :notify => 1
31
+
32
+ room.send("Dude", "Hello world", :notify => true).should be_true
33
+ end
34
+
35
+ it "successfully with custom color" do
36
+ mock_successful_send 'Dude', 'Hello world', :color => 'red'
37
+
38
+ room.send("Dude", "Hello world", :color => 'red').should be_true
39
+ end
40
+
41
+ it "but fails when the room doesn't exist" do
42
+ mock(HipChat::Room).post(anything, anything) {
43
+ OpenStruct.new(:code => 404)
44
+ }
45
+
46
+ lambda { room.send "", "" }.should raise_error(HipChat::UnknownRoom)
47
+ end
48
+
49
+ it "but fails when we're not allowed to do so" do
50
+ mock(HipChat::Room).post(anything, anything) {
51
+ OpenStruct.new(:code => 401)
52
+ }
53
+
54
+ lambda { room.send "", "" }.should raise_error(HipChat::Unauthorized)
55
+ end
56
+
57
+ it "but fails if we get an unknown response code" do
58
+ mock(HipChat::Room).post(anything, anything) {
59
+ OpenStruct.new(:code => 403)
60
+ }
61
+
62
+ lambda { room.send "", "" }.
63
+ should raise_error(HipChat::UnknownResponseCode)
64
+ end
65
+ end
66
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'hipchat'
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rr
9
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bzhipchat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Betazeta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rr
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ description: Ruby library to interact with HipChat, forked by boris.quiroz@betazeta.com
63
+ email: boris.quiroz@betazeta.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.textile
69
+ files:
70
+ - .document
71
+ - LICENSE
72
+ - README.textile
73
+ - Rakefile
74
+ - VERSION
75
+ - hipchat.gemspec
76
+ - lib/hipchat.rb
77
+ - lib/hipchat/capistrano.rb
78
+ - lib/hipchat/chef.rb
79
+ - lib/hipchat/rails3_tasks.rb
80
+ - lib/hipchat/railtie.rb
81
+ - spec/hipchat_spec.rb
82
+ - spec/spec.opts
83
+ - spec/spec_helper.rb
84
+ homepage: http://github.com/boris/hipchat
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.23
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Ruby library to interact with HipChat, forked by boris.quiroz@betazeta.com
108
+ test_files: []