Skip to content

Instantly share code, notes, and snippets.

@khamiltonuk
khamiltonuk / AbstractModelAndViewBuilder.java
Created November 29, 2015 07:31
Code of our abstract parent class follows:
public abstract class AbstractModelAndViewBuilder<B extends AbstractModelAndViewBuilder<B>> {
private static final String REDIRECT = "redirect:";
private B thisObj;
protected abstract B self();
protected ModelAndView mav;
protected AbstractModelAndViewBuilder() {
thisObj = self();
}
protected B setPage(Page page) {
mav = new ModelAndView(page.getTemplateName());
@khamiltonuk
khamiltonuk / lengthStatusAndRecording.java
Last active November 29, 2015 07:37
Code that manages request for information about the call, including the length and status, and a MP3 recording
def callDone = Action {
implicit request => Logger.info(s"A call from a browser has ended, storing details")
Logger.debug(s"Request: ${request.queryString.toList}")
val status = request.queryString.get("DialCallStatus").getOrElse(mutable.Buffer()).mkString
val sid = request.queryString.get("DialCallSid").getOrElse(mutable.Buffer()).mkString
val duration = request.queryString.get("DialCallDuration").getOrElse(mutable.Buffer()).mkString
val recording = request.queryString.get("RecordingUrl").getOrElse(mutable.Buffer()).mkString
Logger.info(s"Call $sid terminated with status $status after $duration. Recording available at $recording")
callsReceived = recording :: callsReceived Ok
}
@khamiltonuk
khamiltonuk / getTwiML.java
Created November 29, 2015 07:50
Returns a TwiML file with the action to execute.
def serveCallConfig = Action {
implicit request => Logger.info(s"Establishing a call form a browser")
Logger.debug(s"Request: ${request.queryString.toList}")
val to = request.queryString.get("To").getOrElse(mutable.Buffer()).mkString
Logger.info(s"Found target number $to") val action = routes.Application.callDone.absoluteURL()
Logger.info(s"Action for recording is $action")
val xml = s"<Response><Dial callerId='+441473379566' method='GET' action='$action' record='true'><Number>$to</Number></Dial></Response>"
Ok(xml).as("text/xml")
}
object Application extends Controller with TwilioAccess {
def index = Action {
implicit request => Ok(views.html.index(credentialsForm)).withNewSession
}
def getCredentials = Action {
implicit request => credentialsForm.bindFromRequest.fold(
errors => BadRequest(views.html.index(errors)
),
credentials => Redirect(routes.Application.testTwilio) .withSession(setSessionCredentials(credentials.sid, credentials.token, credentials.appSID)) )
}
def sendSMS = hasCredentials {
(sid, token, appSID) => implicit request => smsForm.bindFromRequest.fold(
errors => BadRequest(views.html.twilio(sid, errors, "", callsReceived)),
details => { val msg = TwilioAPI .sendSMS(sid, token, details.phone, details.msg) match { case Failure(ex) => Flash(Map("danger" -> s"Couldn't send sms, error: ${ex.getMessage}")) case Success(id) => Flash(Map("success" -> s"SMS sent with id $id")) }
Redirect(routes.Application.testTwilio).flashing(msg) }
)
}
@khamiltonuk
khamiltonuk / asyncLoop.js
Last active November 29, 2015 08:52
Optimising JavaScript performances with asynchronous loops
function splitLoop(items, process, context) {
var todo = items.concat();
setTimeout(function () {
do {
process.call(context, todo.shift());
} while (todo.length > 0);
setTimeout(arguments.callee, 25);
}, 25);
}
@khamiltonuk
khamiltonuk / praticalLoop.js
Created November 29, 2015 08:38
Optimising JavaScript performances with asynchronous loops
var items = [];
for (var i = 0; i < 1000; i++)
items[i] = i;
splitLoop(items, function (item) {
console.log(item)
}, this);
@khamiltonuk
khamiltonuk / specificTimeCheck.js
Last active October 30, 2019 15:19
Optimising JavaScript performances with asynchronous loops
function splitLoop(items, process, context, callback) {
var todo = items.concat();
setTimeout(function () {
var start = +new Date();
do {
process.call(context, todo.shift());
} while (todo.length > 0 && (+new Date() - start < 50));
@khamiltonuk
khamiltonuk / endOfTheCollectionCheck.js
Last active November 29, 2015 08:45
Optimising JavaScript performances with asynchronous loops
function todo(item) {
console.log(item);
};
function done() {
console.log('done');
}
var items = [];
for (var i = 0; i < 1000; i++)
@khamiltonuk
khamiltonuk / splitLoop.js
Last active November 29, 2015 08:53
Optimising JavaScript performances with asynchronous loops
function splitLoop(items, process, context, callback) {
var todo = items.concat();
setTimeout(function () {
var start = +new Date();
do {
process.call(context, todo.shift());
} while (todo.length > 0);
if (todo.length > 0) {