Skip to content

Instantly share code, notes, and snippets.

View kamranayub's full-sized avatar

Kamran Ayub kamranayub

View GitHub Profile
@kamranayub
kamranayub / DataContext.js
Created April 24, 2013 05:12
A working ASP.NET Web API MVC 4 Anti-Forgery approach that also works on cloud hosts like AppHarbor. See: http://kamranicus.com/Blog/Posts/70/protip-using-anti-forgery-token-with-aspnet-web-ap
var options = {};
// jQuery options
// options.url = foo;
// CSRF Token
var csrfToken = $("input[name='__RequestVerificationToken']").val();
if (csrfToken) {
options.headers = {
@kamranayub
kamranayub / removeRoot.js
Created August 29, 2013 14:54
Remove the root element of a JSON object, see: http://jsfiddle.net/kamranayub/72dm2/
var rootElementJson = {
root: {
name: "Foo",
person: true
}
};
var removeRootElement = function (obj) {
var numKeys = 0,
rootKey;
@kamranayub
kamranayub / deploy.sh
Created February 26, 2015 16:29
Deploy site from Travis
#!/bin/bash
echo "Running deployment script..."
CURRENT_COMMIT=`git rev-parse HEAD`
# Change the branch used if applicable (e.g. gh-pages)
echo "Cloning master branch..."
# Hide output since we use an access token here
git clone -b master "https://${GH_TOKEN}@${GH_REF}" _deploy > /dev/null 2>&1 || exit 1
@kamranayub
kamranayub / trigger-build.js
Created March 29, 2015 22:59
Manually trigger Travis CI build for repository
var Travis = require('travis-ci');
// change this
var repo = "kamranayub/kamranayub.github.io";
var travis = new Travis({
version: '2.0.0'
});
travis.authenticate({
@kamranayub
kamranayub / ko.extendes.urlSync.js
Created May 11, 2015 16:19
Knockout Extender: URLSync - syncs observable value with URL hash
/**
* An extender that syncs the observable with the address
* bar (using hash fragment). Will load from URL hash or
* querystring (at time of creation) and will
* update hash when observable changes.
*
* Options: string|object
* String: Query parameter key to get/set
* Object:
* - param - Query parameter key to get/set
@kamranayub
kamranayub / ko.bindingReport.js
Last active June 14, 2023 22:34
Knockout.js Binding Performance Reporter
/**
* Performance reporting for Knockout binding handlers
*
* Usage: Include after all bindings are declared, view console for results.
*/
(function () {
var report = [];
var lastReport = 0;
var debounceWait = 500;
@kamranayub
kamranayub / Generate-OfflineManifest.ps1
Last active September 21, 2016 20:24
PowerShell script to build an HTML5 application manifest file
<#
.SYNOPSIS
Generates an HTML5 offline app cache manifest file
.DESCRIPTION
Generates an offline app cache manifest file according to file paths specified
and outputs with MD5 checksums so manifest only changes when dependent files change.
@kamranayub
kamranayub / CassetteConfiguration.cs
Last active March 5, 2016 16:47
Register a CDN URL generator for Cassette. See blog post: http://kamranicus.com/blog/2015/10/10/azure-cdn-cassette/
namespace Website {
/// <summary>
/// Configures Cassette IoC
/// </summary>
public class CassetteContainerConfiguration : IConfiguration<TinyIoCContainer>
{
public void Configure(TinyIoCContainer container)
{
@kamranayub
kamranayub / App.config
Created November 5, 2015 05:06
Example self-hosting a Web API endpoint in a Windows Service using TopShelf (https://topshelf.readthedocs.org) and TopShelf.Owin package.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Port" value="1337"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@kamranayub
kamranayub / 1_TestingContext.cs
Last active October 23, 2020 15:37
Example using Ninject and Moq to create a TestingImpersonationContext. All it does is replace the bound dependency with a new impersonated mock and then replaces it once disposed. Relies on a testing context that is available to any test class (could also be a test base class). `UserRepository` is just an example of code that retrieves a user fr…
public class TestingContext {
private IKernel _kernel;
public TestingContext() {
// create initial Strict mock that can always be setup outside impersonation context
UserContext = new Mock<IUserContext>(MockBehavior.Strict);
// register dependencies
_kernel = CreateKernel();