Skip to content

Instantly share code, notes, and snippets.

View ambantis's full-sized avatar

Alexandros Bantis ambantis

View GitHub Profile
@ambantis
ambantis / GFolder.scala
Created October 1, 2013 19:40
A Scala representation of a Google Drive Folder
case class GFolder(id: String,
eTag: String,
url: String,
iconUrl: String,
title: String,
owner: String,
parents: Set[String],
children: Set[String],
scions: Set[String],
created: LocalDateTime,
@ambantis
ambantis / MagicNotebook.scala
Last active December 24, 2015 10:28
A Scala object that manages the tree of Google Drive files in MagicNotebook.io
case class MagicNotebook(tree: GFolder,
implicit val folders: Map[String, GFolder],
implicit val docs: Map[String, GDoc])
object MagicNotebook {
def create(userId: String)(implicit s: Session) = {
DbHomes.findById(userId).flatMap { fileId =>
DbGFolders.findById(fileId).map { tree =>
val folders = DbGFolders.findAllFor(fileId)
@ambantis
ambantis / scion_view.sql
Created October 1, 2013 19:45
a Postgres view that recursively finds all children, grand-children, great-grand-children, etc. of a Google Drive folder
CREATE VIEW scion_view AS
WITH RECURSIVE scions(id, scion) AS (
SELECT c.id, c.child
FROM children AS c
UNION ALL
SELECT s.id, c.child
FROM children AS c, scions AS s
WHERE c.id = s.scion)
SELECT * FROM scions ORDER BY id, scion;
@ambantis
ambantis / child_string_agg.sql
Created October 1, 2013 19:48
A Postgres function to flatten a one-to-many relationship to a one-to-one relationship of comma-delimited strings
SELECT DISTINCT
id, string_agg(child, ',' ORDER BY child) AS child_str
FROM children GROUP BY id;
@ambantis
ambantis / gfolder_view.sql
Created October 1, 2013 19:50
A Postgres view that denormalizes to a Scala representation of a Google Drive folder
CREATE VIEW gfolder_view AS
SELECT
f.id, f.e_tag, f.url, f.icon_url, f.title, m.name, f.file_owner,
p.parent_str, c.child_str, s.scion_str, f.created, f.modified
FROM
gfiles AS f
JOIN mimes AS m ON (f.mime_type = m.name)
LEFT JOIN (SELECT DISTINCT id, string_agg(parent, ',' ORDER BY parent) AS parent_str
FROM parents GROUP BY id) AS p ON (f.id = p.id)
LEFT JOIN (SELECT DISTINCT id, string_agg(child, ',' ORDER BY child) AS child_str
@ambantis
ambantis / DbGFolder.scala
Last active December 24, 2015 10:29
Mapping a Scala Slick Table object to a Postgres View
case class DbGFolder(id: String,
eTag: String,
url: String,
iconUrl: String,
title: String,
owner: String,
parents: Option[String],
children: Option[String],
scions: Option[String],
created: LocalDateTime,
class DbUserSpec extends FeatureSpec with GivenWhenThen {
feature("A prospective user can log into the system and create a new account") {
info("As a prospective user")
info("I want to be able to create an account")
info("So that I can use the MagicNotebook app")
scenario("I login for the first time") {
Given("I click 'Accept' on the Google OAuth Consent Page")
And("I allow the app to have access to my Google Drive and personal info")
pending
@ambantis
ambantis / GoogleOauth.scala
Created October 7, 2013 21:41
A function that takes a json response of access_token and user_info from Google OAuth2, validates the json, and information to Cache and the Database if json is valid.
trait GoogleOAuth extends AuthConstants with CacheConstants {
def persistPacket(rUser: Response, rToken: Response)(implicit s: Session): Future[Try[DbUser]] = future {
(rUser.json.as[JsObject] ++ rToken.json.as[JsObject]).validate[GoogleOAuthPacket] match {
case JsError(errors) =>
Logger.error(s"failed to validate user, with errors: $errors")
Failure(new Throwable("didn't work"))
case JsSuccess(packet, _) =>
Logger.debug(s"successfully parsed user and token: $packet")
@ambantis
ambantis / IntegrationSuites.scala
Created October 7, 2013 22:08
A ScalaTest test suite for integration testing in Play! 2.2.0
import java.io.File
import play.api.{Mode, Play, DefaultApplication}
import org.scalatest.{SequentialNestedSuiteExecution, BeforeAndAfterAll, Suites}
import app.models.DbUserSpec
class IntegrationSuites extends Suites(new PlayConfigSpec, new DbUserSpec) with BeforeAndAfterAll
with SequentialNestedSuiteExecution with DbSetup {
implicit val app: DefaultApplication =
new DefaultApplication(new File("."), this.getClass.getClassLoader, None, Mode.Test)
@ambantis
ambantis / DbUserTest.scala
Created October 12, 2013 18:59
Using ScalaTest, a unit test of a lazy val json to object validator
@DoNotDiscover
class DbUserTest extends FunSpec with Matchers with EitherValues with DbUserJson {
describe("A DbUser") {
describe("Should be instantiated from valid json") {
validUserJson.validate[DbUser].left.get.email should be("bart@simpsons.com")
}
}
}