Skip to content

Commit

Permalink
Merge pull request hugorodgerbrown#6 from moserware/master
Browse files Browse the repository at this point in the history
Added support for room names instead of id's
  • Loading branch information
hugorodgerbrown committed May 23, 2012
2 parents 34dd412 + 304945c commit 2dbe071
Showing 1 changed file with 129 additions and 19 deletions.
148 changes: 129 additions & 19 deletions HipChatClient/HipChatClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Serialization;

namespace HipChat
Expand All @@ -15,7 +15,7 @@ namespace HipChat
/// </remarks>
public class HipChatClient
{
private int room = int.MinValue;
private string roomId = string.Empty;
private string sender = string.Empty;
private ApiResponseFormat format = ApiResponseFormat.JSON;
private bool notify = false;
Expand Down Expand Up @@ -60,7 +60,39 @@ public enum ApiResponseFormat { JSON = 0, XML = 1 }
/// <summary>
/// The numeric id of the room to which to send a message
/// </summary>
public int RoomId { get { return room; } set { room = value; } }
public int RoomId
{
// Convert to and from an integer to preserve API backward compatibility
get
{
int id;
if(int.TryParse(roomId, out id))
{
return id;
}

return int.MinValue;
}
set
{
roomId = value.ToString();
}
}

/// <summary>
/// The name of the room to which to send a message.
/// </summary>
public string RoomName
{
get
{
return roomId;
}
set
{
roomId = value;
}
}

/// <summary>
/// Name the message will appear be sent from. Must be less than 15 characters long. May contain letters, numbers, -, _, and spaces.
Expand Down Expand Up @@ -113,16 +145,34 @@ public HipChatClient(string token, int room)
this.RoomId = room;
}

public HipChatClient(string token, string room)
: this(token)
{
this.RoomName = room;
}

public HipChatClient(string token, int room, string from)
: this(token, room)
{
this.From = from;
}

public HipChatClient(string token, string room, string from)
: this(token, room)
{
this.From = from;
}

public HipChatClient(string token, int room, ApiResponseFormat format)
: this(token, room)
{
this.Format = format;
}

public HipChatClient(string token, string room, ApiResponseFormat format)
: this(token, room)
{
this.Format = format;
}

#endregion constructors
Expand All @@ -135,6 +185,16 @@ public static void SendMessage(string token, int room, string from, string messa
// create a local instance of HipChatClient, as then we get the validation
var client = new HipChatClient(token, room);
client.SendMessage(message, from);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
public static void SendMessage(string token, string room, string from, string message)
{
// create a local instance of HipChatClient, as then we get the validation
var client = new HipChatClient(token, room);
client.SendMessage(message, from);
}

/// <summary>
Expand All @@ -147,6 +207,15 @@ public static void SendMessage(string token, int room, string from, string messa
client.SendMessage(message, from, notify);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
public static void SendMessage(string token, string room, string from, string message, bool notify)
{
// create a local instance of HipChatClient, as then we get the validation
var client = new HipChatClient(token, room);
client.SendMessage(message, from, notify);
}

/// <summary>
/// Sends a message to a chat room.
Expand All @@ -158,6 +227,15 @@ public static void SendMessage(string token, int room, string from, string messa
client.SendMessage(message, from, color);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
public static void SendMessage(string token, string room, string from, string message, BackgroundColor color)
{
// create a local instance of HipChatClient, as then we get the validation
var client = new HipChatClient(token, room);
client.SendMessage(message, from, color);
}

/// <summary>
/// Sends a message to a chat room.
Expand All @@ -169,6 +247,16 @@ public static void SendMessage(string token, int room, string from, string messa
client.SendMessage(message, from, notify, color);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
public static void SendMessage(string token, string room, string from, string message, bool notify, BackgroundColor color)
{
// create a local instance of HipChatClient, as then we get the validation
var client = new HipChatClient(token, room);
client.SendMessage(message, from, notify, color);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
Expand All @@ -180,6 +268,17 @@ public void SendMessage(string message, int room, string from)
SendMessage(message);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
/// <param name="message">The message to send - can contain some HTML and must be valid XHTML.</param>
public void SendMessage(string message, string room, string from)
{
this.RoomName = room;
this.From = from;
SendMessage(message);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
Expand All @@ -191,6 +290,17 @@ public void SendMessage(string message, int room, string from, bool notify)
SendMessage(message, room, from);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
/// <param name="message">The message to send - can contain some HTML and must be valid XHTML.</param>
/// <param name="notify">If true, the message triggers a "ping" sound when it hits the room.</param>
public void SendMessage(string message, string room, string from, bool notify)
{
this.Notify = notify;
SendMessage(message, room, from);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
Expand All @@ -200,8 +310,8 @@ public void SendMessage(string message, int room)
{
this.RoomId = room;
SendMessage(message);
}

}
/// <summary>
/// Sends a message to a chat room.
/// </summary>
Expand All @@ -213,7 +323,7 @@ public void SendMessage(string message, int room, bool notify)
this.Notify = notify;
SendMessage(message, room);
}

/// <summary>
/// Sends a message to a chat room.
/// </summary>
Expand Down Expand Up @@ -295,9 +405,9 @@ public void SendMessage(string message)
{
#region validation
if (string.IsNullOrEmpty(Token))
throw new InvalidOperationException("You must set the Token property before calling the SendMessage method.");
if (RoomId == int.MinValue)
throw new InvalidOperationException("You must set the RoomId property before calling the SendMessage method.");
throw new InvalidOperationException("You must set the Token property before calling the SendMessage method.");
if (string.IsNullOrEmpty(RoomName))
throw new InvalidOperationException("You must set the RoomId property before calling the SendMessage method.");
if (string.IsNullOrEmpty(From))
throw new InvalidOperationException("You must set the From property before calling the SendMessage method.");
if (string.IsNullOrEmpty(message))
Expand Down Expand Up @@ -410,7 +520,7 @@ public string RoomHistory(DateTime date)
#region validation
if (string.IsNullOrEmpty(Token))
throw new InvalidOperationException("You must set the Token property before calling the API.");
if (RoomId == int.MinValue)
if (string.IsNullOrEmpty(RoomName))
throw new InvalidOperationException("You must set the RoomId property before calling the SendMessage method.");
if (date == null)
throw new ArgumentNullException("date", "You must pass in a valid date for the history API.");
Expand All @@ -424,8 +534,8 @@ public string RoomHistory()
{
#region validation
if (string.IsNullOrEmpty(Token))
throw new InvalidOperationException("You must set the Token property before calling the API.");
if (RoomId == int.MinValue)
throw new InvalidOperationException("You must set the Token property before calling the API.");
if (string.IsNullOrEmpty(RoomName))
throw new InvalidOperationException("You must set the RoomId property before calling the SendMessage method.");

#endregion validation
Expand All @@ -442,7 +552,7 @@ private string FormatMessageUri(string message)
{
var url = string.Format(@"https://api.hipchat.com/v1/rooms/message?auth_token={0}&room_id={1}&format={2}&notify={3}&from={4}&message={5}&color={6}",
Uri.EscapeDataString(this.Token),
this.RoomId,
Uri.EscapeDataString(this.RoomName),
this.Format.ToString().ToLower(),
this.NotifyAsChar,
Uri.EscapeDataString(this.From),
Expand Down Expand Up @@ -471,7 +581,7 @@ private string FormatRoomsHistoryUri(DateTime date)
return string.Format("https://api.hipchat.com/v1/rooms/history?format={0}&auth_token={1}&room_id={2}&date={3}",
this.Format.ToString().ToLower(),
this.Token,
this.RoomId,
Uri.EscapeDataString(this.RoomName),
date.ToString("yyyy-MM-dd"));
}

Expand All @@ -480,7 +590,7 @@ private string FormatRoomsHistoryUri()
return string.Format("https://api.hipchat.com/v1/rooms/history?format={0}&auth_token={1}&room_id={2}&date={3}",
this.Format.ToString().ToLower(),
this.Token,
this.RoomId,
Uri.EscapeDataString(this.RoomName),
"recent");
}

Expand Down

0 comments on commit 2dbe071

Please sign in to comment.