Skip to content

Instantly share code, notes, and snippets.

View ctcarrier's full-sized avatar

Chris Carrier ctcarrier

  • Stateless Solutions
  • Detroit, MI
View GitHub Profile
@ctcarrier
ctcarrier / gist:11348157
Created April 27, 2014 15:17
ReactiveMongoConnection
class ReactiveMongoConnection extends Module {
import ExecutionContext.Implicits.global
private val config = ConfigFactory.load()
implicit val context = inject[ActorSystem]
val driver = new MongoDriver
val pattern = "^mongodb:\\/\\/([\\w]*):([\\w]*)@([\\w\\.-]+):([\\d]+)\\/([\\w]+)".r
val envUri = Properties.envOrElse("MONGOLAB_URI", "").toString
@ctcarrier
ctcarrier / gist:9918087
Created April 1, 2014 16:48
Joda DateTime Serializer for ReactiveMongo
import reactivemongo.bson.{BSONHandler, BSONDateTime, Macros}
import org.joda.time.format.ISODateTimeFormat
import org.joda.time.{DateTime, DateTimeZone}
package object myApp {
DateTimeZone.setDefault(DateTimeZone.UTC)
implicit object BSONDateTimeHandler extends BSONHandler[BSONDateTime, DateTime] {
val fmt = ISODateTimeFormat.dateTime()
def read(time: BSONDateTime) = new DateTime(time.value)
trait BlPathMatchers {
val BSONObjectID: PathMatcher1[BSONObjectID] =
PathMatcher("""[\da-fA-F]""".r) flatMap { string ⇒
try Some(new BSONObjectID(string))
catch { case _: IllegalArgumentException ⇒ None }
}
}
private BallHttpClient<Ball> httpClient = new BallHttpClient<Ball>(SCHEME, HOST, PORT){
@Override
public ResponseHandler<Ball> createResponseHandler() {
return new ResponseHandler<Ball>(){
@Override
public Ball handleResponse(HttpResponse resp)
throws ClientProtocolException, IOException, NoResourceFoundException, ErrorAccessingResourceException {
handleErrors(resp);
return gson.fromJson(new InputStreamReader(resp.getEntity().getContent()), Ball.class);
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
BallHttpClient<Ball> httpClient = new GenericHttpClient<Ball>(SCHEME, HOST, PORT);
@ctcarrier
ctcarrier / GenericHttpClient.java
Last active January 1, 2016 17:29
Generic Android Http Client
import android.net.http.AndroidHttpClient;
import android.util.Base64;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
@ctcarrier
ctcarrier / ImageDirectoryDao.scala
Created December 17, 2013 23:51
Basic ReactiveMongo DAO
package com.blrest.dao
import scala.concurrent.Future
import akka.actor.ActorSystem
import reactivemongo.bson.BSONDocument
import reactivemongo.api.{QueryOpts, DB}
import reactivemongo.core.commands.Count
import reactivemongo.api.collections.default._
@ctcarrier
ctcarrier / package.scala
Created December 17, 2013 23:40
User of ReactiveMongo macros
package com
import reactivemongo.bson.{Macros, BSONDocument, BSONDocumentReader}
import com.blrest.model.{FlickrData, ImageMeta}
/**
* Created by ccarrier for bl-rest.
* at 3:19 PM on 12/15/13
*/
package object blrest {
@ctcarrier
ctcarrier / gist:7989957
Created December 16, 2013 16:33
Scala regex pattern to extract MongoDB connection info
scala> "mongodb://dbuser:dbpassword@1111.mongolab.com:22222/heroku_app1111"
res7: String = mongodb://dbuser:dbpassword@1111.mongolab.com:22222/heroku_app1111
scala> val pattern = "^mongodb:\\/\\/([\\w]+):([\\w]+)@([\\w\\.]+):([\\d]+)\\/([\\w]+)".r
pattern: scala.util.matching.Regex = ^mongodb:\/\/([\w]+):([\w]+)@([\w\.]+):([\d]+)\/([\w]+)
scala> val pattern(user, password, host, port, db) = res7
user: String = dbuser
password: String = dbpassword
host: String = 1111.mongolab.com