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 / spray_lift_json
Created October 20, 2011 04:32
Spray lift-json support
implicit def liftJsonUnmarshaller[A :Manifest] = new UnmarshallerBase[A] {
val canUnmarshalFrom = ContentTypeRange(`application/json`) :: Nil
def unmarshal(content: HttpContent) = protect {
val jsonSource = DefaultUnmarshallers.StringUnmarshaller.unmarshal(content).right.get
parse(jsonSource).extract[A]
}
}
implicit def liftJsonMarshaller[A <: AnyRef] = new MarshallerBase[A] {
val canMarshalTo = ContentType(`application/json`) :: Nil
@ctcarrier
ctcarrier / serviceTest1
Created April 23, 2012 16:09
Gatling scenario
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import scala.util.Random
import com.excilys.ebi.gatling.core.feeder.Feeder
class ServiceSimulation extends Simulation {
def apply = {
val random = new Random
random.setSeed(System.currentTimeMillis)
@ctcarrier
ctcarrier / ReactiveMongoRandomDocument
Created December 16, 2013 04:22
Select a random document using
import reactivemongo.bson.BSONDocument
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.api.{QueryOpts, DB}
import reactivemongo.core.commands.Count
import reactivemongo.api.collections.default._
def getDocument(collection: BSONCollection): Future[Option[YourClass]] = {
val futureCount = db.command(Count(collection.name))
futureCount.flatMap { count =>
val skip = Random.nextInt(count)
@ctcarrier
ctcarrier / gist:7989957
Created December 16, 2013 16:33
Scala regex pattern to extract MongoDB connection info
scala> "mongodb://dbuser:[email protected]:22222/heroku_app1111"
res7: String = mongodb://dbuser:[email protected]: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
@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 / 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 / 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;
BallHttpClient<Ball> httpClient = new GenericHttpClient<Ball>(SCHEME, HOST, PORT);
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;
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);