Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
@darrelmiller
darrelmiller / gist:1036674
Created June 20, 2011 21:49
Creating the hal spec example using Hal.net
var hal = HalDocument.Create(new Uri("http://example.org"));
hal.AddNamespace("td", new Uri("http://mytodoapp.com/rels/"));
hal.AddLink("td:search", new Uri("/todo-list/search;{searchterm}",UriKind.Relative));
hal.AddLink("td:description", new Uri("/todo-list/description", UriKind.Relative));
hal.AddProperty("created_at", "2010-01-16");
hal.AddProperty("updated_at", "2010-02-21");
hal.AddProperty("summary", "An example list");
hal.CreateResource("td:owner", new Uri("http://range14sux.com/mike"))
@darrelmiller
darrelmiller / gist:1340512
Created November 4, 2011 21:20
Atom feed directly from database
WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2005/Atom')
SELECT 'Customers' AS 'title',
'' AS 'subtitle',
'http://tavis.net/Customers' AS 'id',
REPLACE(CONVERT(varchar,(SELECT MAX(COALESCE(ModifiedDate,CreatedDate)) FROM tblCustomer), 121),' ','T') + 'Z' AS 'updated',
(SELECT cu.Code AS 'title',
REPLACE(CONVERT(varchar,COALESCE(ModifiedDate,CreatedDate) , 121),' ','T') + 'Z' AS 'updated',
'Darrel Miller' AS 'author/name',
'http://tavis.net/Customer/' + LTRIM(STR(cu.ID)) AS 'id',
(SELECT *
@darrelmiller
darrelmiller / LinqApplePropertyList
Created January 7, 2012 15:52
Extremely naive Apple Property List parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Linq;
namespace LinqApplePropertyList {
class Program {
static void Main(string[] args) {
@darrelmiller
darrelmiller / SimpleObjectContent.cs
Created January 21, 2012 03:20
Simple Object Content
public class SimpleObjectContent<T> : HttpContent
{
private readonly T _outboundInstance;
private readonly HttpContent _inboundContent;
private readonly MediaTypeFormatter _formatter;
// Outbound constructor
public SimpleObjectContent(T outboundInstance, MediaTypeFormatter formatter)
{
_outboundInstance = outboundInstance;
public static class FormatterCollectionExtensions
{
public static Tuple<MediaTypeFormatter,MediaTypeHeaderValue> Negotiate<T>(this MediaTypeFormatterCollection formatters, HttpRequestMessage requestMessage)
{
var formatSelector = new FormatterSelector();
MediaTypeHeaderValue mediaType = null;
var response = new HttpResponseMessage() {RequestMessage = requestMessage};
var formatter = formatSelector.SelectWriteFormatter(typeof(T), new FormatterContext(response, false), formatters, out mediaType);
<?xml version="1.0" encoding="utf-8"?>
<resource href="/orders">
<link rel="self" href="/orders"/>
<link rel="relatedResource1" href="/orders/relatedResource1"/>
<link rel="relatedResource2" href="/orders/relatedResource2"/>
public class LoggingFormatterSelector : IFormatterSelector
{
private readonly FormatterSelector _FormatterSelector = new FormatterSelector();
public new MediaTypeFormatter SelectReadFormatter(Type type, FormatterContext formatterContext, IEnumerable<MediaTypeFormatter> formatters)
{
string descriptFormatterContext = GetDescriptFormatterContext(formatterContext);
Console.WriteLine("Selecting Read Formatter for type {0} based on formatter context: {1}", type.Name, descriptFormatterContext);
public class IWantJSONDammit : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return base.SendAsync(request,cancellationToken);
GET /
=>
200 OK
Content-Type: application/hal+json
{
"_links": {
"self": { "href": "/" },
"urn:tekpub:login": { "href": "/login" }
GET /
=>
200 OK
Content-Type: application/hal+json
{
"_links": {
"self": { "href": "/" },
"urn:tekpub:login": { "href": "/login" }
}