Skip to content

Instantly share code, notes, and snippets.

View alexander-williamson's full-sized avatar

Alexander Williamson alexander-williamson

View GitHub Profile
public static class UnixTimestamp
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static double Convert(this DateTime dateTime)
{
return dateTime.Subtract(Epoch).TotalMilliseconds;
}
public static DateTime Convert(this double offset)
@alexander-williamson
alexander-williamson / patiently.rb
Created July 6, 2016 16:38 — forked from avanderberg/patiently.rb
retry wrapper for cucumber steps
def patiently(&block)
cycles = 0
begin
yield
rescue => e
cycles += 1
sleep 0.1
if cycles < 10
retry
else
using System;
using System.Threading;
namespace Alexw.RunningConsoleApp
{
public class Program
{
static readonly ManualResetEvent QuitEvent = new ManualResetEvent(false);
static void Main(string[] args)
@alexander-williamson
alexander-williamson / Recurse.cs
Created September 22, 2016 09:26
Simple LinqPad example to hit an endpoint
// Needs System.Net;
// Prefer HttpClient for real situations other than testing
void Main()
{
while(true) {
try {
var url = "http://example.com/sub/dir/page.aspx?a=1234&b=something";
var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create (url);
var response = (System.Net.HttpWebResponse)request.GetResponse ();
@alexander-williamson
alexander-williamson / pubsub.js
Last active May 28, 2017 19:06
RequireJs jQuery
// tinypubsub:
// https://gist.github.com/cowboy/661855
// code assumes you have shimmed jquery with the correct version in requirejs config:
// http://requirejs.org/docs/jquery.html#modulename
define(["jquery"], function (jQuery) {
var o = jQuery({});
jQuery.subscribe = function () {
@alexander-williamson
alexander-williamson / GetExecutableFromPath.cs
Created December 21, 2016 17:11
Find matching files PATH environment variable folders
private IEnumerable<string> GetExecutableFromPaths(string fileName)
{
var path = Environment.GetEnvironmentVariable("PATH");
var pathFolders = path.Split(';');
foreach (var folder in pathFolders)
{
var fullPath = Path.Combine(folder, fileName);
if (File.Exists(fullPath))
yield return fullPath;
@alexander-williamson
alexander-williamson / Owin.Authentication.Examples.cs
Created December 30, 2016 12:54
Examples for simple owin authentication
# Your use statement can do all the work for you
app.Use(async (context, next) =>
{
if (!IsAuthenticated(context))
{
context.Response.StatusCode = 401;
var bytes = System.Text.Encoding.UTF8.GetBytes("You are not authorised!");
context.Response.Body.Write(bytes, 0, bytes.Length);
await Task.FromResult(context);
/// <summary>
/// Loop until a condition is met and your function returns true
/// </summary>
/// <param name="action">The delegate to run. Return true to exit the loop.</param>
/// <param name="delay">Return a timespan for the delay between loops</param>
/// <param name="whentoStop">Return true when the loop should stop when catching exceptions or looping</param>
public static void DoUntil(Func<bool> action, Func<int, TimeSpan, TimeSpan> delay, Func<int, TimeSpan, bool> whentoStop)
{
var startedUtc = DateTime.UtcNow;
var amountOfRetriesAllowed = 0;
@alexander-williamson
alexander-williamson / TcpPorts.cs
Created May 28, 2017 19:02
Get free TCP Ports
using System.Net;
using System.Net.Sockets;
// taken from https://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net
public static class TcpPorts
{
public static int GetFreeTcpPort()
{
var l = new TcpListener(IPAddress.Loopback, 0);
@alexander-williamson
alexander-williamson / KibanaClient.cs
Created June 14, 2017 16:00
How to send files to logz.io kibana and have properties accepted as fields
using System;
using System.Net;
using RestSharp;
namespace Alexw.ParkingAvailable
{
public class KibanaClient
{
private readonly IRestClient _client;
private readonly string _token;