Skip to content

Instantly share code, notes, and snippets.

View hamnis's full-sized avatar

Erlend Hamnaberg hamnis

View GitHub Profile
mime = {
MIMEType: function MIMEType(major, minor, attributes) {
this.major = major || "*";
this.minor = minor || "*";
this.attributes = attributes || [];
},
Parameter : function(name, value) {
this.name = name;
this.value = value || "";
}
private static Map<CaseInsensitiveKey, HTTPMethod> defaultMethods = ImmutableMap.<CaseInsensitiveKey, HTTPMethod>builder()
.put(new CaseInsensitiveKey(CONNECT.getMethod()), CONNECT)
.put(new CaseInsensitiveKey(DELETE.getMethod()), DELETE)
.put(new CaseInsensitiveKey(GET.getMethod()), GET)
.put(new CaseInsensitiveKey(HEAD.getMethod()), HEAD)
.put(new CaseInsensitiveKey(OPTIONS.getMethod()), OPTIONS)
.put(new CaseInsensitiveKey(PATCH.getMethod()), PATCH)
.put(new CaseInsensitiveKey(POST.getMethod()), POST)
.put(new CaseInsensitiveKey(PURGE.getMethod()), PURGE)
.put(new CaseInsensitiveKey(PUT.getMethod()), PUT)
@hamnis
hamnis / Message.scala
Created December 6, 2010 19:28
Message in an IRC client
case class Message(origin: Option[Origin], command: NameAndFormat, arguments: List[String]) {
def format = {
val builder = new StringBuilder()
origin.foreach(x => builder.append(":").append(x.name).append(" "))
command match {
case Status(c, n) => builder.append(c)
case Command(n) => builder.append(n)
}
builder.append(" ")
@hamnis
hamnis / gist:820133
Created February 10, 2011 08:25
Atom service document
<?xml version="1.0"?>
<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
<workspace>
<atom:title type="text">spaces</atom:title>
<collection href="http://localhost:1990/confluence/rest/atompub/latest/spaces">
<atom:title type="text">spaces</atom:title>
<accept/>
<categories fixed="yes">
<category xmlns="http://www.w3.org/2005/Atom" scheme="urn:confluence:category" term="page"/>
<category xmlns="http://www.w3.org/2005/Atom" scheme="urn:confluence:category" term="space"/>
@hamnis
hamnis / RichURI.scala
Created August 9, 2011 11:41
Implicit conversion
package com.example.util
import java.net.URI
case class RichURI(uri: URI) {
def getLastPathSegment = {
val uriAsString = uri.toString
val index = uriAsString.lastIndexOf("/")
if (index != -1) {
Some(uriAsString.substring(index + 1))
2. Pandoc Constructs
What do you need to type in Pandoc to get the correct output. Note
this is just basic Pandoc format, so you might be better off reading
the README from Pandoc itself [15].
2.1. section
Just use the normal sectioning commands available in Pandoc, I tend
to use
@hamnis
hamnis / HerokuRedirect.scala
Created March 12, 2012 19:06
unfiltered responsefunction for detecting heroku https
object HerokuRedirect {
def apply[A, B](req: HttpRequest[A], path: String): ResponseFunction[B] = {
val absolutepath = if (path.startsWith("/")) path else "/" + path
req match {
case XForwardProto("https") & Host(host) => Found ~> Location("https://%s%s".format(host, absolutepath))
case _ => Redirect(path)
}
object XForwardProto extends StringHeader("X-Forward-Proto")
}
@hamnis
hamnis / coll.json
Created May 4, 2012 11:03
Sorting in collection+json
{
"collection": {
"version": "1.0",
"href": "http://example.org",
"items": [],
"queries": [
{
"href": "http://example.org",
"rel": "self",
"data": [
package com.example.secure
import unfiltered.request.{UserAgent, HttpRequest}
import unfiltered.response.{ResponseString, ContentType, Forbidden, ResponseFunction}
trait Identification {
type User
type RF = ResponseFunction[Any]
protected def identify(userAgent: ApiKey): Either[SecurityException, User]
val doit = for {
foo <- lookup("foo").toInt.withName("bzz")
bar <- lookup("bar").withName("Brum")
} yield List(foo, bar)
val mapped = doit.apply(map).toMap // skal bli til en Map[String, Array[String]]
//Unfiltered QParams gjør noe nesten slik...