|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Windows.Threading;
namespace TwitterApi
{
public class TwitterMessage
{
public int? Id
{
get;
set;
}
public static TwitterChainMessage CreateMessages(TwitterMessageContext context, String message)
{
TwitterChainMessage chain = new TwitterChainMessage();
String header = context.Header;
string contentLeft = message;
while(contentLeft != null)
{
var twitterMessage = TwitterMessage.Create(header, contentLeft, context.ContinuationString, context.MaxMessageSize, out contentLeft);
chain.Append(twitterMessage);
}
return chain;
}
public Twit ToTwit()
{
return new Twit(this.FullMessage);
}
public static bool TryParseMessage(TwitterMessageContext context, String message, out TwitterMessage twitterMessage)
{
if(!context.IsConsistentWithContext(message))
{
twitterMessage = null;
return false;
}
var header = context.Header;
var content = message.Remove(0, context.Header.Length);
String continuationString = "";
if(content.EndsWith(context.ContinuationString))
{
content = content.Remove(content.Length - context.ContinuationString.Length);
continuationString = context.ContinuationString;
}
twitterMessage = new TwitterMessage(header, content, continuationString, context.MaxMessageSize);
return true;
}
public static TwitterMessage Create(String header, String content, String continuationString, int maxSize, out String contentLeft)
{
int currentSize = header.Length + content.Length;
if(currentSize <= maxSize)
{
contentLeft = null;
return new TwitterMessage(header, content, String.Empty, maxSize);
}
currentSize = currentSize + continuationString.Length;
int numberCharToTrunc = currentSize - maxSize;
contentLeft = content.Substring(content.Length - numberCharToTrunc);
var truncatedContent = content.Substring(0, content.Length - contentLeft.Length);
Debug.Assert(truncatedContent + contentLeft == content);
return new TwitterMessage(header, truncatedContent, continuationString, maxSize);
}
private TwitterMessage(String header, String content, String continuationString, int maxSize)
{
Header = header;
Content = content;
Bottom = continuationString;
if(FullMessage.Length > maxSize)
{
throw new InvalidOperationException("The twit is too big");
}
}
string _FullMessage;
public String FullMessage
{
get
{
return _FullMessage = _FullMessage ?? Header + Content + Bottom;
}
}
public String Header
{
get;
private set;
}
public String Content
{
get;
private set;
}
public String Bottom
{
get;
private set;
}
public bool IsFinalMessage
{
get
{
return String.IsNullOrEmpty(Bottom);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am currently the CTO of
Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.
I also developed a tool to make IaaS on Azure more easy to use
IaaS Management Studio.
If you want to contact me, go
this way