Skip to content

Instantly share code, notes, and snippets.

View martinabrahams's full-sized avatar

Martin Abrahams martinabrahams

View GitHub Profile
@martinabrahams
martinabrahams / Gravatar.cs
Last active August 16, 2021 12:15
Simple C# method to create the hash of the email address and return Gravatar image URL for a supplied email address. As per official documentation - https://en.gravatar.com/site/implement/images/
public static string GetGravatarImageUrl(string emailAddress)
{
// Create MD5 Hash of email address
var md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(emailAddress.Trim().ToLower());
byte[] hash = md5.ComputeHash(inputBytes);
// Create lower-case hex string
var sb = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
@martinabrahams
martinabrahams / GenerateUmbracoSitemap.cs
Created March 23, 2016 01:13
Generate Umbraco Sitemap with HREF Lang tags
public static byte[] GenerateSiteMap(IPublishedContent model)
{
var sites = model.Parent.Children.Where(x => x.DocumentTypeAlias == "HomePage" || x.DocumentTypeAlias == "SatelliteHomePage");
var baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.IsSecureConnection ? "https" : "http", HttpContext.Current.Request.Url.Host);
var siteMap = new XmlSiteMap();
foreach (var homePage in sites)
{
@martinabrahams
martinabrahams / CreateElmahAzureTable.sql
Created March 28, 2016 00:45
Creates Microsoft Azure friendly ELMAH Table with clustered index. Credit http://stackoverflow.com/questions/15228112/mvc-elmah-and-sql-azure
--~Changing index [dbo].[ELMAH_Error].PK_ELMAH_Error to a clustered index. You may want to pick a different index to cluster on.
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ELMAH_Error]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ELMAH_Error](
[ErrorId] [uniqueidentifier] NOT NULL,
[Application] [nvarchar](60) NOT NULL,
[Host] [nvarchar](50) NOT NULL,
[Type] [nvarchar](100) NOT NULL,
@martinabrahams
martinabrahams / ElmahCustomSqlErrorLog.cs
Created March 28, 2016 01:49
Create a custom SQL error log for ELMAH. Append data stored in HttpContext.Items to log entries to provide critical debugging info.
using Elmah;
using System;
using System.Collections;
using System.Text;
namespace MyProject
{
public class ElmahCustomSqlErrorLog : SqlErrorLog
{
public ElmahCustomSqlErrorLog(IDictionary config) : base(config) { }
@martinabrahams
martinabrahams / RobotsTxtHandler.cs
Last active July 29, 2016 01:55
Simple robots.txt handler to cater for multiple deploy environments. This example stores the a "build environment" variable in the web.config AppSettings and the handler reads this setting and serves either the production or non-production (dev) physical text file from the file system.
using System.Web;
using System.Web.Routing;
using System.Configuration;
namespace MyWebsite.Infrastructure
{
public class RobotsTxtHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
@martinabrahams
martinabrahams / gist:3646236ed8d978bbfa16a362d95e752d
Created September 12, 2017 06:56 — forked from mediavrog/gist:49c4f809dffea4e00738a7f5e3bbfa59
CORS in Google Cloud Functions for Firebase
const cors = require('cors')({origin: true});
exports.sample = functions.https.onRequest((req, res) => {
cors(req, res, () => {
res.send('Passed.');
});
});
@martinabrahams
martinabrahams / distinct.ts
Created September 27, 2017 02:37
Get distinct items in array in TypeScript - source https://stackoverflow.com/a/45886147/6732882
Array.from(new Set(yourArray.map((item: any) => item.id)))
@martinabrahams
martinabrahams / appveyor-ebs.yml
Last active April 9, 2018 00:23
Build dotnet core 2 project and package as Elastic Beanstalk artifact
image: Visual Studio 2017
before_build:
- dotnet restore
build_script:
# use dotnet CLI to publish to subfolder named site
- dotnet publish -o site
after_build:
@martinabrahams
martinabrahams / datalogger_smoothing.ino
Created January 6, 2019 05:44
Arduino sketch (C) to take x sensors and plot them with smoothing. For home made throttle body balancer.
// number of sensors
const int sensors = 4;
// number of frames to smooth
const int smoothing = 5;
const int minVoltage = 950;
const int maxVoltage = 1050;
// 2d array to store historic data for all sensors
@martinabrahams
martinabrahams / wait-for-cloudformation.sh
Created December 20, 2021 05:54
This script can be used to hold up a deploy pipeline after a CloudFormation deploy has taken place. This will handle both UPDATE and CREATE events, currently the AWS CLI doesn't support this.
#!/bin/bash
STACK_NAME=my-stack-name
UPDATING=1
while [ "$UPDATING" -ne 0 ]
do
stack_status=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} | jq -r --arg STACK "$STACK_NAME" '[.Stacks[]|select(.StackName==$STACK)][0].StackStatus')
if [[ "$stack_status" == "PROCESSING_UPDATE" || "$stack_status" == "PROCESSING_CREATE" ]]; then