Skip to content

Instantly share code, notes, and snippets.

@petrovg
petrovg / Noddy.js
Last active December 19, 2015 00:29
A noddy little REST test server, that allows you to set up a bunch of resources using PUT requests and then serves them back at you. This makes setting up test cases for REST consumers very simple. Run using 'node Noddy.js'. Default port is 9210 - look at the bottom to change that
var http = require('http')
var respond = function(res, s) {
res.writeHead(200);
res.write(s);
res.end();
}
var makeService = function(resources) {
<html>
<head></head>
<body>
<script>
var A = {who: "A"};
A.sine = function(x) { return Math.sin(x); }
A.cube = function(x) { return x*x*x; }
A.compose = function(f, g) {
return function(x) { return f(g(x)); }
@petrovg
petrovg / gist:6018105
Last active December 19, 2015 21:09
Zsh colour theme
PROMPT='%{$fg_bold[green]%}➜ %{$fg_bold[green]%}%p %{$fg[green]%}%c %{$fg_bold[green]%}$(git_prompt_info)%{$fg_bold[green]%} % %{$reset_color%}'
ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[green]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}) %{$fg[green]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[green]%})"
@petrovg
petrovg / gist:6322197
Last active December 21, 2015 14:49
Consuming a web resource in chunks with a Play framework Iteratee
import play.api.libs.ws._
import play.api.libs.iteratee._
import scala.concurrent.ExecutionContext.Implicits.global
WS.url("http://www.bbc.co.uk").get( resp => Iteratee.fold(0) { (i:Int, el:Array[Byte] ) => println(el); i + 1} )
@petrovg
petrovg / gist:6323747
Last active December 21, 2015 14:59
Consuming a twitter feed with OAuth
object twitter {
import play.api.libs.ws._
import play.api.libs.iteratee._
import play.api.libs.oauth._
import scala.concurrent.ExecutionContext.Implicits.global
val key = ConsumerKey("XXXX", "XXXX")
val twitter = OAuth(ServiceInfo(
"https://api.twitter.com/oauth/request_token",
@petrovg
petrovg / gist:dbdaf451901ff0bef2e3
Created May 13, 2014 16:43
Scala future error recovery/handling
object TMP extends App {
import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def b1 = {
val f: Future[Int] = scala.concurrent.future { 100 }
val r = Await.result(f, 1.second)
println(":> " + r)
}
@petrovg
petrovg / gist:7e13a7d2e07564dfdacb
Created March 18, 2015 16:41
Scala - Getting future result optionally and combining it with previous future result.
//
// Is there a better way to do this. The difficulty is coming from the fact that the
// second future result may not be necessary, based on an optinal value in the first.
//
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
case class User(name: String, homepageUrl: Option[String])
case class UserProfile(userName: String, profile: String)
@petrovg
petrovg / MagnetExample.scala
Last active May 1, 2017 20:25
Trying to understand the Magnet pattern (as used in spray)
// Trying to understand the Magnet pattern (as used in spray)
// http://spray.io/blog/2012-12-13-the-magnet-pattern/
/**
* A simple utility that writes things to strings.
* We'd like it to be able to work for multiple different types
*/
object Writer {
/**
* We want this to work for multiple types that we don't know yet,
@petrovg
petrovg / gist:d5e705e710efeb822141
Created August 12, 2015 15:50
How to broadcast your JSON ObjectMapper across a Spark cluster, so that it's only instantiated once on each node. The ObjectMapper is supposed to be reused, but this is not trivial in Spark. Fortunately, Scala's lazy values make this super easy...
class MapperHolder extends Serializable {
lazy val mapper = {
val m = new ObjectMapper with ScalaObjectMapper
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
m.registerModule(DefaultScalaModule)
m
}
}
val holderBcast = sc.broadcast(new MapperHolder())
case class CarMake(name: String)
case class CarModel(name: String)
case class Car(make: CarMake, model: Option[CarModel])
case class StreetName(value: String)
case class Address(number: String, street: StreetName)
case class Person(name: String, address: Option[Address], car: Option[Car])