Skip to content

Instantly share code, notes, and snippets.

View lukencode's full-sized avatar
🙃

Luke Lowrey lukencode

🙃
View GitHub Profile
@lukencode
lukencode / ExtendedJsonDeserializer.cs
Created July 22, 2010 03:52
Alternate RestSharp Json Deserializer
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Deserializers;
using RestSharp.Extensions;
@lukencode
lukencode / StringExtensions.cs
Created August 18, 2010 12:34
C# String extension methods
public static class StringExtensions
{
/// Like linq take - takes the first x characters
public static string Take(this string theString, int count, bool ellipsis = false)
{
int lengthToTake = Math.Min(count, theString.Length);
var cutDownString = theString.Substring(0, lengthToTake);
if (ellipsis && lengthToTake < theString.Length)
cutDownString += "...";
@lukencode
lukencode / CalendarHelpers.cs
Created August 18, 2010 23:28
Google and Yahoo Calendar MVC HTML Helpers
public static class CalendarHelpers
{
//Google Calender
public static string GoogleCalendar(this HtmlHelper helper, string linkText, string what, DateTime start, DateTime? end, string description, string location, string websiteName, string websiteAddress, string attributes)
{
//parse dates
var dates = start.ToString("yyyyMMddTHHmmssZ");
if (end.HasValue && end > start)
{
@lukencode
lukencode / DateExtensions.cs
Created December 20, 2010 00:20
DateExtensions.cs
public static class DateExtensions
{
public static string TimeAgo(this DateTime date)
{
if (date <= DateTime.Now)
{
var timeSince = DateTime.Now.Subtract(date);
if (timeSince.TotalMilliseconds < 1) return "not yet";
if (timeSince.TotalMinutes < 1) return "just now";
if (timeSince.TotalMinutes < 2) return "1 minute ago";
public static DownloadableFile ToDownloadableXmlFileForExcel2003(this System.Xml.Linq.XDocument file, string fileName)
{
MemoryStream ms = new MemoryStream();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings() { Encoding = Encoding.UTF8 };
XmlWriter xmlWriter = XmlWriter.Create(ms, xmlWriterSettings);
file.Save(xmlWriter); //.Save() adds the <xml /> header tag!
xmlWriter.Close(); //Must close the writer to dump it's content its output (the memory stream)
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IFluentInterface {
[EditorBrowsable(EditorBrowsableState.Never)]
Type GetType();
[EditorBrowsable(EditorBrowsableState.Never)]
int GetHashCode();
[EditorBrowsable(EditorBrowsableState.Never)]
string ToString();
[EditorBrowsable(EditorBrowsableState.Never)]
bool Equals(object obj);
@lukencode
lukencode / CurrencyHelper.cs
Created November 18, 2011 04:29
format currency by code, get exchange rates
public static class CurrencyHelper
{
private static string _currencyRegex = "rhs: \\\"(\\d*.\\d*)";
// slightly modified from: http://www.ashishblog.com/blog/currency-exchange-rate-in-webpage-using-c-asp-net/
// Uses a google api which takes requests in this format: http://www.google.com/ig/calculator?hl=en&q=1AUD%3D%3FUSD
public static decimal Convert(decimal amount, string fromCurrency, string toCurrency)
{
var web = new WebClient();
@lukencode
lukencode / CacheExtensions.cs
Created December 9, 2011 00:46
CacheExtensions
public static class CacheExtensions
{
static object sync = new object();
/// <summary>
/// Executes a method and stores the result in cache using the given cache key. If the data already exists in cache, it returns the data
/// and doesn't execute the method. Thread safe, although the method parametersn't guaranteed to be thread safe.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cache"></param>
@lukencode
lukencode / SparkExtensions.cs
Created December 12, 2011 04:18
Spark-Graphs for .NET. Clone of https://github.com/holman/spark.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NSpark
{
public static class SparkExtensions
{
public static String Spark(this string input)
@lukencode
lukencode / gist:2849237
Created June 1, 2012 05:45
thinkin fillr
public class NameFiller : IPropertyFiller<string>
{
private Random _rand;
private static Regex _combinedRegex = new Regex("name|fullname|firstname|lastname|surname|middlename|maidenname", RegexOptions.IgnoreCase);
private static Regex _fullNameRegex = new Regex("name|fullname", RegexOptions.IgnoreCase);
private static Regex _firstNameRegex = new Regex("firstname|middlename", RegexOptions.IgnoreCase);
private static Regex _surnameRegex = new Regex("lastname|surname|maidenname", RegexOptions.IgnoreCase);
private static List<string> _firstNames = new List<string> { "Luke", "John", "Mary" };