Skip to content

Instantly share code, notes, and snippets.

View michaelbramwell's full-sized avatar

696d656b michaelbramwell

  • Perth Australia
View GitHub Profile
@michaelbramwell
michaelbramwell / pwGenUrandom
Created March 3, 2017 02:29
Password Generation
< /dev/urandom tr -db _A-Z-a-z-0-9 | head -c16
@michaelbramwell
michaelbramwell / handySPGroupHelpers
Created February 17, 2015 09:14
Handy SP Group Helpers
public static void AddUserToGroup(SPWeb web, string userName, string groupName)
{
SPWeb spWeb = web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevSite = new SPSite(spWeb.Url))
using (SPWeb elevWeb = elevSite.OpenWeb())
{
elevWeb.AllowUnsafeUpdates = true;
@michaelbramwell
michaelbramwell / Factory.cs
Last active January 30, 2017 14:08
Factory
public interface IFactory
{
IAbbreviationsAndAcronymsService AbbreviationsAndAcronymsService {get; set;}
IBusinessActivityService BusinessActivityService {get; set;}
Factory Make(ConcreteTypes type);
}
public class Factory
{
public IAbbreviationsAndAcronymsService AbbreviationsAndAcronymsService;
@michaelbramwell
michaelbramwell / SimpleRssAtomFeedSearchForHugo.js
Created January 17, 2017 13:19
Simple Rss Atom Feed Search For Hugo
var search = search || {};
(function(o, win) {
let _client = document.getElementById("client");
let _server = document.getElementById("server");
let _dateFmt = function(dateStr) {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return new Date(dateStr).toLocaleDateString("en-US", options);
};
@michaelbramwell
michaelbramwell / idxOf.js
Last active January 14, 2017 13:40
Find the needle in the haystack
let _idxOf = function(needle){
return function(haystack){
return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
};
};
// e.g
let searchTerm = "fly";
let pets = [{ name: 'George', type: 'fly' }, { name: 'Benson', type: 'dog' }];
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> False<T>()
{
@michaelbramwell
michaelbramwell / siteroot.cs
Created November 30, 2016 01:17
Sitecore get current site root. ref - http://stackoverflow.com/a/33161150/6264690
Item currentItem = Sitecore.Context.Item;
SiteInfo currentSiteRoot = SiteContextFactory.Sites
.Where(s => s.RootPath != "" && currentItem.Paths.Path.ToLower().StartsWith(s.RootPath.ToLower()))
.OrderByDescending(s => s.RootPath.Length)
.FirstOrDefault();
@michaelbramwell
michaelbramwell / SetTimeZoneFromISO8601Str.php
Created September 7, 2016 03:07
Set Time Zone From ISO8601 String
private function SetTimeZone($dtStr)
{
$newDt = DateTime::createFromFormat(DateTime::ISO8601, $dtStr);
$newDt->setTimezone(new DateTimeZone('Australia/Perth'));
$newDtStr = $newDt->format("c");
return $newDtStr;
}
@michaelbramwell
michaelbramwell / SlackHook.cs
Created June 14, 2016 00:30
Simple Slack WebHook Client
// dependancies: Newtonsoft.Json, RestSharp;
public interface IRpcClient<T>
{
string AccessToken { get; }
string BaseUrl { get; }
T DataStore { get; }
string Host { get; }
string Protocal { get; }
string RelativeUrl { get; }
@michaelbramwell
michaelbramwell / GenericRestRequest.cs
Last active May 6, 2016 07:58
Generic Request with RestRequest lib
public static IRestResponse<T> MakeRequest(string relativeUrl, Func<RestClient, RestRequest, IRestResponse<T>> clientFunc)
{
var client = new RestClient("https://someresource.com");
var request = new RestSharp.RestRequest(relativeUrl);
return clientFunc(client, request);
}
// example call
var request = MakeRequest("/api/whatever", (c, r) => c.Execute<SomeType>(r));
var deserializedSomeTypeData = request.Data;