Skip to content
This repository was archived by the owner on Oct 12, 2019. It is now read-only.

Commit b6d7870

Browse files
committed
Version one of the API, untested but mother approved
1 parent 73ab264 commit b6d7870

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

README

Whitespace-only changes.

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Hipchat Ruby Library
2+
=====================
3+
4+
This is just the first version of my copy of the HipChat API. I'm not sure I'm entirely in love with the way it calls Hipchat's API but it assumes a pretty auto-magical format. The future version may cut that back.
5+
6+
The simple way to use this is as follows:
7+
8+
hipchat = Hipchat::Base.new("yourAPIcode")
9+
10+
rooms = hipchat.rooms
11+
12+
main_room = rooms.first
13+
14+
main_room.speak("Bot Name", "This is a simple message")
15+
16+
users = main_room.users # => Returns a the users in the room
17+
18+
users.first.profile # => Returns the special information about the user

lib/hipchat.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'crack'
2+
require 'httparty'
3+
require 'uri'
4+
5+
require File.dirname(__FILE__) + '/hipchat/base'
6+
require File.dirname(__FILE__) + '/hipchat/room'
7+
require File.dirname(__FILE__) + '/hipchat/user'

lib/hipchat/base.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Hipchat
2+
3+
class Base
4+
include HTTParty
5+
base_uri 'api.hipchat.com/v1'
6+
7+
attr_accessor :auth_token
8+
9+
def initialize(auth_token)
10+
self.auth_token = auth_token
11+
end
12+
13+
def rooms
14+
rooms_json = self.class.get("/rooms/list", :query => {:auth_token => @auth_token})
15+
Room.instantiate_rooms(auth_token, rooms_json)
16+
end
17+
18+
def users
19+
users_json = self.class.get("/users/list", :query => {:auth_token => @auth_token})
20+
User.instantiate_users(auth_token, users_json)
21+
end
22+
23+
end
24+
25+
end

lib/hipchat/room.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Hipchat
2+
3+
class Room < Hipchat::Base
4+
5+
attr_accessor :auth_token, :room_hash
6+
7+
def self.instantiate_rooms(auth_token, rooms_json)
8+
rooms_json['rooms'].inject([]) do |rooms, room_json|
9+
rooms << Room.new(auth_token, room_json)
10+
end
11+
end
12+
13+
def initialize(auth_token, room_hash)
14+
self.auth_token = auth_token
15+
self.room_hash = room_hash
16+
end
17+
18+
def method_missing(method_name)
19+
super unless [:name, :room_id, :topic, :last_active, :owner_user_id].include?(method_name)
20+
room_hash[method_name.to_s]
21+
end
22+
23+
def users
24+
users_hash = self.class.get("/rooms/show", :query => {:room_id => self.room_id, :auth_token => self.auth_token})
25+
User.instantiate_users(auth_token, users_hash)
26+
end
27+
28+
def speak(from_name, message)
29+
self.class.post("/rooms/message", :body => {:from => URI.escape(from_name), :message => URI.escape(message), :room_id => self.room_id}, :query => {:auth_token => self.auth_token})
30+
end
31+
32+
end
33+
34+
end

lib/hipchat/user.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Hipchat
2+
3+
class User < Hipchat::Base
4+
5+
attr_accessor :auth_token, :user_hash
6+
7+
def self.instantiate_users(auth_token, users_json)
8+
users_json = users_json.keys.include?("users") ? users_json["users"] : users_json['room']['participants']
9+
users_json.inject([]) do |users, user_json|
10+
users << User.new(auth_token, user_json)
11+
end
12+
end
13+
14+
def initialize(auth_token, user_hash)
15+
self.auth_token = auth_token
16+
self.user_hash = user_hash
17+
end
18+
19+
def profile
20+
user_hash = self.class.get("/users/show", :query => {:user_id => self.user_id, :auth_token => self.auth_token})
21+
self.user_hash.merge!(user_hash["user"])
22+
self
23+
end
24+
25+
def method_missing(method_name)
26+
super unless [:user_id, :name, :email, :title, :photo_url, :status, :is_group_admin].include?(method_name)
27+
user_hash[method_name.to_s]
28+
end
29+
30+
end
31+
32+
end

0 commit comments

Comments
 (0)