Korero – Rev 1
?pathlinks?
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Korero.Serialization;
namespace Korero.Communication
{
public class Callback
{
#region Public Enums, Properties and Fields
public string Command { get; set; }
public DateTime Time =>
DateTime.ParseExact(_time, Constants.DateTimeStamp,
CultureInfo.InvariantCulture, DateTimeStyles.None);
public bool Success => bool.Parse(_success);
public Guid Id => Guid.Parse(_id);
public CSV Data { get; }
public string Error { get; }
#endregion
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private readonly string _id;
private readonly string _success;
private readonly string _time;
#endregion
#region Constructors, Destructors and Finalizers
public Callback(IReadOnlyDictionary<string, string> callback)
{
_success = callback["success"];
_time = callback["time"];
_id = callback["_id"];
switch (callback.ContainsKey("data"))
{
case true:
Data = new CSV(callback["data"]);
break;
default:
Data = new CSV();
break;
}
switch (callback.ContainsKey("error"))
{
case true:
Error = callback["error"];
break;
default:
Error = string.Empty;
break;
}
Command = callback["command"];
}
#endregion
#region Public Indexers
public IEnumerable<string> this[string key] => Get(key);
public string this[int index] => Get(index);
#endregion
#region Public Methods
public bool Contains(string key)
{
return Data.Any(item => item == key);
}
#endregion
#region Private Methods
private string Get(int index)
{
return Data.ElementAtOrDefault(index);
}
private IEnumerable<string> Get(string key)
{
var array = Data.ToArray();
for (var i = 0; i < array.Length; ++i)
{
if (array[i] == key)
{
yield return array[i + 1];
}
}
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.