Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / Trello
Created November 21, 2013 11:04
Trello Deserialise Checklist
void Main()
{
var data = @"{'id':'528c97b12314dcac5c0021b0','name':'Query','idBoard':'5268f13aa63b12236e0051d2','idCard':
'528c97aa3a003a4a0c00339a','pos':16384,'checkItems':[{'state':'incomplete','id':'528c97bf7a14af624e00228d',
'name':'What ares are on or off in what page for pocket hub section','nameData':{'emoji':{}},'pos':16970},
{'state':'incomplete','id':'528c97d3e790ecc25c0035f6',
'name':'Buy now integration','nameData':null,'pos':33467},
{'state':'incomplete','id':'528c97d9205dd7a54c003598',
'name':'Buys now quantitiy integration','nameData':null,'pos':50628},
{'state':'incomplete','id':'528c97f0b52fd8ab2f003227','name':'Vote functionality integration','nameData':null,'pos':67217},
@feanz
feanz / CurrentVersionHeaderAttribute.cs
Created November 21, 2013 12:02
MVC Application Version Filter
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class CurrentVersionHeaderAttribute : ActionFilterAttribute
{
private const string _xVersion = "X-Version";
private static readonly string _version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var headers = filterContext.HttpContext.Response.Headers;
@feanz
feanz / DistinctBy.cs
Created November 26, 2013 11:44
DistinctBy Linq Expression
/// <summary>
/// Select distinct elements in collection on a given property
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source">The collection to filter</param>
/// <param name="keySelector">The property to distinct collection by</param>
/// <returns></returns>
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
@feanz
feanz / DaySuffix.cs
Created November 27, 2013 11:30
DaySuffix
public static string DaySuffix(this DateTime dateTime)
{
return (dateTime.Day == 1)
? "st"
: (dateTime.Day == 2)
? "nd"
: (dateTime.Day == 3)
? "rd"
: "th";
}
@feanz
feanz / PostcodeAttribute.cs
Created December 2, 2013 16:38
Postcode attribute
/// <summary>
/// Postcode validation attribute
/// Must be uppercase with a single optional space
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class PostcodeAttribute : DataTypeAttribute
{
private static readonly Regex _regex = new Regex(@"^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$");
//if you need lowercase and multi space use this one
@feanz
feanz / ValidateObjectAttribute.cs
Created December 9, 2013 23:25
Recursive Validation
public class ValidateObjectAttribute: ValidationAttribute {
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
var results = new List<ValidationResult>();
var context = new ValidationContext(value, null, null);
Validator.TryValidateObject(value, context, results, true);
if (results.Count != 0) {
var compositeResults = new CompositeValidationResult(String.Format("Validation for {0} failed!", validationContext.DisplayName));
results.ForEach(compositeResults.AddResult);
@feanz
feanz / RouteTestingExtensions.cs
Last active December 31, 2015 00:29
Route Testing Extensions
/// <summary>
/// Used to simplify testing routes and restful testing routes
/// <example>
/// This tests that incoming PUT on resource is handled by the update method of the Banner controller
/// "~/banner/1"
/// .WithMethod(HttpVerbs.Put)
/// .ShouldMapTo<BannerController>(action => action.Update(1));
/// </example>
/// </summary>
public static class RouteTestingExtensions
@feanz
feanz / MockHttpContextFixture.cs
Created December 11, 2013 10:16
Mock Http Context Fixture (Builder class for test helping)
public class HttpContextBuilder
{
private Mock<HttpContextBase> _context;
private Mock<HttpRequestBase> _request;
private Mock<HttpResponseBase> _response;
private Mock<HttpSessionStateBase> _session;
private Mock<HttpServerUtilityBase> _server;
private Mock<IPrincipal> _user;
private Mock<IIdentity> _identity;
@feanz
feanz / AttributeExtensions.cs
Created December 11, 2013 13:08
AttributeExtensions
public static class AttributeExtensions
{
/// <summary>
/// Will return true if the attributeTarget is decorated with an attribute of type TAttribute.
/// Will return false if not.
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <param name="attributeTarget"></param>
/// <returns></returns>
public static bool IsDecoratedWith<TAttribute>(this ICustomAttributeProvider attributeTarget) where TAttribute : Attribute
@feanz
feanz / MethodInfoExtensions.cs
Created December 11, 2013 13:09
MethodInfoExtensions
public static class MethodInfoExtensions
{
/// <summary>
/// Will return the name of the action specified in the ActionNameAttribute for a method if it has an ActionNameAttribute.
/// Will return the name of the method otherwise.
/// </summary>
/// <param name="method"></param>
/// <returns></returns>
public static string ActionName(this MethodInfo method)
{