Skip to content
Browse files

Initial Commit

  • Loading branch information...
0 parents commit cd43a07d8d3c9634c68938b571d236e93ad44b69 @jesilz jesilz committed Sep 27, 2012
Showing with 286 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. +3 −0 Gemfile
  3. +14 −0 Gemfile.lock
  4. +1 −0 README.md
  5. +1 −0 VERSION
  6. +7 −0 config/config.yml.example
  7. +41 −0 hipchat-logger.rb
  8. +13 −0 lib/hipchat.rb
  9. +21 −0 lib/hipchat/client.rb
  10. +44 −0 lib/hipchat/message.rb
  11. +132 −0 lib/hipchat/room.rb
  12. +1 −0 views/splunk.erb
8 .gitignore
@@ -0,0 +1,8 @@
+._*
+.*.swp
+.bundle
+.DS_Store
+.htaccess
+config/config.yml
+tmp/*
+*.log
3 Gemfile
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+gem 'httparty'
14 Gemfile.lock
@@ -0,0 +1,14 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ httparty (0.9.0)
+ multi_json (~> 1.0)
+ multi_xml
+ multi_json (1.3.6)
+ multi_xml (0.5.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ httparty
1 README.md
@@ -0,0 +1 @@
+#TODO
1 VERSION
@@ -0,0 +1 @@
+#TODO
7 config/config.yml.example
@@ -0,0 +1,7 @@
+hipchat:
+ api:
+ key: '12345678903sdf87gasdfo8gdf'
+
+user_mappings:
+ firstlast: firstl
+
41 hipchat-logger.rb
@@ -0,0 +1,41 @@
+require 'bundler/setup'
+require 'yaml'
+require File.expand_path('../lib/hipchat', __FILE__)
+require 'erb'
+require 'logger'
+
+# Define log file
+log = Logger.new('log/hipchat_logger.log', 'daily')
+
+# Load config
+config = YAML.load(open(File.expand_path('../config/config.yml', __FILE__)))
+
+# ERB Template
+log_output = ERB.new(File.read('views/splunk.erb'))
+
+# Connect to HipChat
+client = HipChat::Client.new(config["hipchat"]["api"]["key"])
+
+# DATES
+today = Time.now
+yesterday = today - (24*60*60)
+
+log_date = yesterday.strftime('%Y-%m-%d')
+# Loop through HipChat Rooms
+client.rooms.each do |room|
+
+ # Open room log file for writing
+ log_file = File.open("log/hipchat_room_id_#{room.room_id}_#{log_date}.log", 'w')
+
+ begin
+ room.messages(log_date).each do |message|
+ # Look up netid
+ message.author_netid = "jeffs" if config["user_mappings"].has_key? message.author.downcase.gsub(/\s/,'')
+
+ # log output using erb template
+ log_file.write log_output.result(message.get_binding)
+ end
+ rescue Exception => e
+ log.warn e.message
+ end
+end
13 lib/hipchat.rb
@@ -0,0 +1,13 @@
+require 'httparty'
+require 'ostruct'
+
+module HipChat
+ class UnknownRoom < StandardError; end
+ class Unauthorized < StandardError; end
+ class UnknownResponseCode < StandardError; end
+
+ require_relative 'hipchat/client'
+ require_relative 'hipchat/room'
+ require_relative 'hipchat/message'
+
+end
21 lib/hipchat/client.rb
@@ -0,0 +1,21 @@
+module HipChat
+ class Client
+ include HTTParty
+
+ base_uri 'https://api.hipchat.com/v1/rooms'
+ format :json
+
+ def initialize(token)
+ @token = token
+ end
+
+ def rooms
+ @rooms ||= self.class.get("/list", :query => {:auth_token => @token})['rooms'].
+ map { |r| Room.new(@token, r) }
+ end
+
+ def [](name)
+ Room.new(@token, :room_id => name)
+ end
+ end
+end
44 lib/hipchat/message.rb
@@ -0,0 +1,44 @@
+module HipChat
+ class Message < OpenStruct
+ include HTTParty
+
+ base_uri 'https://api.hipchat.com/v1/rooms'
+
+ def initialize(token, params)
+ @token = token
+
+ super(params)
+ end
+
+ # .
+ #
+ # Example Message Methods:
+ #
+ # #<HipChat::Message date="2012-09-27T10:48:23-0700", from={"name"=>"Jeff Silzer", "user_id"=>167249}, message="what if we didn't get emails?">
+ #
+ # Or if there is an attachment
+ #
+ # #<HipChat::Message date="2012-09-27T10:04:02-0700", from={"name"=>"Will Borchardt", "user_id"=>168653}, file={"name"=>"garry.png", "size"=>10757, "url"=>"http://uploads.hipchat.com/27240/168653/qaof47agndcduqs/garry.png"}, message="so nice and compact :)">
+
+ attr_reader :author_netid
+ def author_name
+ @author_name ||= self.from["name"]
+ end
+
+ def author_id
+ @author_id ||= self.from["user_id"]
+ end
+
+ alias :author :author_name
+
+ def author_netid=(netid)
+ @author_netid = netid
+ end
+
+ # REQUIRED FOR ERB
+ def get_binding
+ binding
+ end
+
+ end
+end
132 lib/hipchat/room.rb
@@ -0,0 +1,132 @@
+module HipChat
+ class Room < OpenStruct
+ include HTTParty
+
+ base_uri 'https://api.hipchat.com/v1/rooms'
+
+ def initialize(token, params)
+ @token = token
+
+ super(params)
+ end
+
+ # DYNAMIC ROOM METHODS:
+ # room_id=102471
+ # name="Water Cooler"
+ # topic="Welcome! Send this link to coworkers who need accounts: https://www.hipchat.com/invite/27240/b5898e5fb2cd6a2586c1c53c4cd5eadb"
+ # last_active=1348768772
+ # created=1348528666
+ # owner_user_id=167249
+ # is_archived=false
+ # is_private=false
+ # xmpp_jid="27240_biola_it_application_services@conf.hipchat.com">
+
+ # Send a message to this room.
+ #
+ # Usage:
+ #
+ # # Default
+ # send 'nickname', 'some message'
+ #
+ # # Notify users and color the message red
+ # send 'nickname', 'some message', :notify => true, :color => 'red'
+ #
+ # # Notify users (deprecated)
+ # send 'nickname', 'some message', true
+ #
+ # Options:
+ #
+ # +color+:: "yellow", "red", "green", "purple", or "random"
+ # (default "yellow")
+ # +notify+:: true or false
+ # (default false)
+ def send(from, message, options_or_notify = {})
+ options = if options_or_notify == true or options_or_notify == false
+ log.warn "DEPRECATED: Specify notify flag as an option (e.g., :notify => true)"
+ { :notify => options_or_notify }
+ else
+ options_or_notify || {}
+ end
+
+ options = { :color => 'yellow', :notify => false }.merge options
+
+ response = self.class.post('/message',
+ :query => { :auth_token => @token },
+ :body => {
+ :room_id => room_id,
+ :from => from,
+ :message => message,
+ :message_format => options[:message_format] || 'html',
+ :color => options[:color],
+ :notify => options[:notify] ? 1 : 0
+ }
+ )
+
+ case response.code
+ when 200; true
+ when 404
+ raise UnknownRoom, "Unknown room: `#{room_id}'"
+ when 401
+ raise Unauthorized, "Access denied to room `#{room_id}'"
+ else
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
+ end
+ end
+
+ def history(options_or_notify = {})
+ options = if options_or_notify == true or options_or_notify == false
+ warn "DEPRECATED: Specify notify flag as an option (e.g., :notify => true)"
+ { :notify => options_or_notify }
+ else
+ options_or_notify || {}
+ end
+
+ options = { :color => 'yellow', :notify => false }.merge options
+
+ response = self.class.get('/history',
+ :query => {
+ :auth_token => @token,
+ :room_id => room_id,
+ :date => Time.now.strftime('%Y-%m-%d'),
+ :timezone => Time.now.zone
+ }
+ )
+
+ case response.code
+ when 200; response.parsed_response["messages"]
+ when 404
+ raise UnknownRoom, "Unknown room: `#{room_id}'"
+ when 401
+ raise Unauthorized, "Access denied to room `#{room_id}'"
+ else
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
+ end
+ end
+
+ def messages (year_month_day = Time.now.strftime('%Y-%m-%d'))
+ @year_month_day = year_month_day
+ @messages ||= []
+ if @messages.empty?
+ response = self.class.get('/history',
+ :query => {
+ :auth_token => @token,
+ :room_id => room_id,
+ :date => @year_month_day,
+ :timezone => Time.now.zone
+ }
+ )
+
+ case response.code
+ when 200
+ @messages = response.parsed_response['messages'].map { |m| Message.new(@token, m) }
+ when 404
+ raise UnknownRoom, "Unknown room: `#{room_id}'"
+ when 401
+ raise Unauthorized, "Access denied to room `#{room_id}'"
+ else
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
+ end
+ end
+ end
+ end
+end
1 views/splunk.erb
@@ -0,0 +1 @@
+[<%= date %>] <% if author_netid %>netid=<%= author_netid %>, <% end %>name=<%= author%>, message=<%= message.gsub(/\n/,'\n') %> <% if file %>attachment=<%= file.inspect %><% end %>

0 comments on commit cd43a07

Please sign in to comment.
Something went wrong with that request. Please try again.