Skip to content

Instantly share code, notes, and snippets.

View tdrozdowski's full-sized avatar

Terry Drozdowski tdrozdowski

View GitHub Profile
@tdrozdowski
tdrozdowski / FileDirective.coffee
Last active December 22, 2015 11:49
Need to get file inputs working with Angular? Here are a couple - basic - examples in CoffeeScript and then again in JavaScript. This is glommed together from viewing various discussion threads/solutions. Its quite confusing that no single one of them is a complete solution. This should handle everything, including taking care of the boundary he…
'use strict';
angular.module('yourApp')
.directive 'file', [ () ->
restrict: "E"
template: "<input type=\"file\" />"
replace: true
require: "ngModel"
link: (scope, element, attr, ctrl) ->
listener = ->
@tdrozdowski
tdrozdowski / repl_example1.scala
Last active December 27, 2015 08:49
Part of the Play Framework with Scala in Action session of the Nov 2013 Desert Code Camp. Scala Basics with the REPL. To use these, please install Scala 2.10.x and then use either type in or use the :paste mode of the REPL to copy/pase the following examples.
val foo = "foo"
foo = "Bar"
var bar = "Bar"
foo + bar
@tdrozdowski
tdrozdowski / json-samples.scala
Last active March 23, 2016 09:15
Various code snippets for working with the Play Framework
import play.api.libs.json._
import play.api.libs.json.Reads._
// construct a json
val user = Json.obj("email" -> "[email protected]", "firstName" -> "Foo", "lastName" -> "Bar")
// > user: play.api.libs.json.JsObject = {"email":"[email protected]","firstName":"Foo","lastName":"Bar"}
println(s"user -> $user")
// > user -> {"email":"[email protected]","firstName":"Foo","lastName":"Bar"}
@tdrozdowski
tdrozdowski / CORS_step1.scala
Last active December 27, 2015 19:49
Adding CORS Support via Filter to PlayFramework 2.2.1
// Create the Global class in your /app folder root package:
import play.api.{GlobalSettings, Play}
import play.api.Play.current
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
/**
// This lists out the initial steps you need to take in your fresh Play project to install the ReactiveMongo plugin
// add play.plugins
400:play.modules.reactivemongo.ReactiveMongoPlugin
// update build.sbt
resolvers += "Sonatype Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
// Below is optional if you wish to build custom modules and include them into your project
//resolvers += Resolver.file("Local repo", file("/Users/<user>/.ivy2/local"))(Resolver.ivyStylePatterns)
@tdrozdowski
tdrozdowski / basic_auth_step1.scala
Last active December 27, 2015 19:49
Adding Basic Auth to your Scala Play App. Requires Play 2.2.x
// step one - build your ActionBuilder
/**
* Created by terry on 10/1/13.
*/
object BasicAuthSecured extends ActionBuilder[AuthorizedRequest] with HeaderNames with Results {
protected def invokeBlock[A](request: Request[A], block: (AuthorizedRequest[A]) => Future[SimpleResult]): Future[SimpleResult] = {
BasicAuthService.authorize(request.headers.get(AUTHORIZATION)).map {
identity =>
block(AuthorizedRequest(identity, request))
} getOrElse (Future.successful(onUnauthorized))
@tdrozdowski
tdrozdowski / UsageProcessor.scala
Last active August 29, 2015 13:57
ReactiveMongo 0.10 with JSONCollection - Upsert
private def buildUsageUpsert(usage : Usage) = {
Json.obj(
"$setOnInsert" ->
Json.obj(
"cpCode" -> usage.customer.cpCode,
"zuora.id" -> usage.customer.zuoraId,
"zuora.subscriptionId" -> usage.customer.subscriptionId,
"month" -> usage.month,
"year" -> usage.year,
"uom" -> "GB"
@tdrozdowski
tdrozdowski / CustomDirective.scala
Last active August 27, 2015 03:54
Custom Directive using existing Directive
val filterClientIp : Directive1[InetAddress] = {
extractClientIP {
ip =>
ip.getAddress().fold(reject){
address =>
if (address.isAnyLocalAddress) {
provide(address)
} else {
reject(SuspectIpRejection(address, "IP is suspect!"))
}