Skip to content

Instantly share code, notes, and snippets.

@jmhdez
jmhdez / analytics.cs
Created March 19, 2012 09:38 — forked from gregoryyoung/analytics.cs
Usage Tracking with Google Analytics
public class TrackingService
{
// Sample usage:
// service.TrackEvent("customers/add");
// service.TrackEvent("order/discount-applied");
public void TrackEvent(string path)
{
ThreadPool.QueueUserWorkItem(x => TrackPageView("/events/" + path));
}
@jmhdez
jmhdez / Post_tweet.cs
Created June 10, 2012 11:31
Post tweet
public void UpdateStatus(string message)
{
// Application tokens
const string CONSUMER_KEY = "YOUR_CONSUMER_KEY";
const string CONSUMER_SECRET = "YOUR_CONSUMER_SECRET";
// Access tokens
const string ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const string ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";
// Common parameters
@jmhdez
jmhdez / GetTweets.cs
Created June 13, 2012 19:09
Get twitter's home timeline
// Related code: https://gist.github.com/2905028
public IEnumerable<string> GetTimeline(int count)
{
// Application tokens
const string CONSUMER_KEY = "YOUR_CONSUMER_KEY";
const string CONSUMER_SECRET = "YOUR_CONSUMER_SECRET";
// Access tokens
const string ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const string ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";
@jmhdez
jmhdez / GoToPlugin.java
Created June 22, 2012 07:03
Sample Cordova Plugin for Android
package koalite.cordova;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import android.content.Intent;
import android.net.Uri;
public class GoToPlugin extends Plugin {
@jmhdez
jmhdez / DuckType.cs
Created December 8, 2012 10:46
Duck Typing con C#
// Required libs
// - NUnit
// - Castle.DynamicProxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using NUnit.Framework;
@jmhdez
jmhdez / DuckType.cs
Created December 16, 2012 16:32
Duck typing con C# preparado para realizar implementaciones de interfaces con objetos anónimos
// Required libs
// - NUnit
// - Castle.DynamicProxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using NUnit.Framework;
@jmhdez
jmhdez / Id.cs
Created April 14, 2013 08:48
Id implementation using phantom types
public struct Id<T>
{
public readonly int Value;
public Id(int value)
{
this.value = value;
}
public static implicit operator Id<T>(int value)
@jmhdez
jmhdez / Sample.cs
Last active December 19, 2015 19:29
Unas cuantas opciones para refactorizar un método a la hora de testearlo
// Situación inicial
public class OrderStatsCalculator
{
// Inyectado por constructor. En todos los casos lo hago igual.
private IOrderRepository repository;
// Esto se puede testear con un mock/stub/fake/etc. que inyectes por
// el constructor
public int Calculate()
{
@jmhdez
jmhdez / Elements.cs
Last active December 20, 2015 11:19
Compartiendo elementos en posiciones dependientes del contenedor
public interface IElement
{
IEnumerable<ChildElement> Children { get; }
}
public class ChildElement
{
public readonly IElement Value;
public readonly Point Point;
@jmhdez
jmhdez / AsDict.cs
Last active December 22, 2015 00:28
Extension Method para convertir un objeto anónimo en un IDictinary
public static class Extensions
{
public IDictionary<string, object> AsDict(this object obj)
{
return obj.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(obj, null))
}
}
var obj = new { Name = "Lucas", Age = 14 }.AsDict();