Skip to content

Instantly share code, notes, and snippets.

View andyczerwonka's full-sized avatar

Andy Czerwonka andyczerwonka

View GitHub Profile
@andyczerwonka
andyczerwonka / euler11.scala
Created April 28, 2011 20:14
The answer to Project Euler #11, using the Scala Actor library
package euler
import scala.actors.Actor
import Actor._
object Euler011 extends Actor {
type Matrix = Seq[Seq[Int]]
case class CalculateMaxRequest(matrix: Matrix, from: Actor)
case class CalculateMaxReply(matrix: Matrix, max: Int)
@andyczerwonka
andyczerwonka / Configuration.scala
Created May 9, 2012 18:05
Specs2 test for parsing JSON and extracting into a case class
package models
case class ProductionType(
key: String,
name: String,
blended: Boolean,
forecast: Boolean,
actual: Boolean,
field: Boolean,
financial: Boolean,
@andyczerwonka
andyczerwonka / Controller.scala
Created May 10, 2012 23:25
Code that is trying illustrate my LinkageError
def dashboard = Action {
val user = User.findByUsername("aczerwonka")
val prefs = UserPreferences.findByUsername("aczerwonka")
val config = Configuration.get();
Ok(views.html.dashboard(user, prefs, config))
}
@andyczerwonka
andyczerwonka / measures.scala
Created May 22, 2012 19:34
I'm trying to understand how to best deal with sending an async request to a web service
def measures(user: User): List[Measure] = {
val prefs = UserPreferences.findByUser(user) // get my prefs
val request = generateRequest(prefs) // build up a WS request
val promise = WS.url(exportEndpoint).post(request)
promise.orTimeout(Nil, 100000).map { responseOrTimeout =>
responseOrTimeout.fold(
response => {
val body = promise.value.get.body
val lines = body.split("\n").map(_.split(","))
lines.map {
@andyczerwonka
andyczerwonka / gist:4447120
Last active December 10, 2015 14:19
I'm trying to serve an image from Play, where the image originates from Gravatar. This code doesn't seem to work, but the URL is correct.
import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
import play.api.libs.concurrent.Execution.Implicits._
import java.io.File
object Application extends Controller {
def index(email: String) = Action {
Async {
@andyczerwonka
andyczerwonka / angular service example.js
Created January 5, 2013 19:44
How would get gain access to successF and errorF?
service.factory('userService', [
'$http',
function($http) {
return {
name : 'User Service',
request : {},
successF : {},
errorF : {},
import akka.actor.{Actor, Stash}
class PausableWorker extends Actor with Stash {
def receive = processing
def processing: Receive = {
case "pause" => context.become(paused)
case msg => // Process task
}
def paused: Receive = {
[merge]
summary = true
tool = "p4"
[mergetool "p4"]
cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge \
"$PWD/$BASE" \
"$PWD/$LOCAL" \
"$PWD/$REMOTE" \
"$PWD/$MERGED"
@andyczerwonka
andyczerwonka / SiteApi.scala
Created October 23, 2014 20:19
I'm trying to create a JSON serializer in Play 2.3.4 and line #7 give a compiler error saying "No Json deserializer found for type Array[models.Sensor]. Try to implement an implicit Reads or Format for this type." Given there is a default serializer for Array, I assume this should work.
case class CreateParams(name: String, interval: Int, camera: Boolean, sensors: IndexedSeq[Sensor])
implicit val createParamsReads = new Reads[CreateParams] {
def reads(js: JsValue): JsResult[CreateParams] = {
val name = (js \ "name").as[String]
val interval = (js \ "interval").as[Int]
val camera = (js \ "camera").as[Boolean]
val sensors = (js \ "sensors").as[IndexedSeq[Sensor]]
JsSuccess(CreateParams(name, interval, camera, sensors))
}
}
var authToken = localStorageService.get('authToken');
$http.defaults.headers.common.Authorization = 'auth-token ' + authToken;
$http.get('/api/whoami').success(function(data) {
self.user = data;
}).error(function() {
self.user = null;
$location.path('/login');
});