Skip to content

Instantly share code, notes, and snippets.

@cdmckay
cdmckay / uuid_to_bytea.sql
Last active December 23, 2022 02:13
How to convert a PostgreSQL UUID to bytea
select decode(replace('45774962-e6f7-41f6-b940-72ef63fa1943'::text, '-', ''), 'hex');
-- And here's how to convert it to the ShortUUID format used in Process Street
select replace(encode(substring(decode(replace('45774962-e6f7-41f6-b940-72ef63fa1943'::text, '-', ''), 'hex') from 9 for 8) ||
substring(decode(replace('45774962-e6f7-41f6-b940-72ef63fa1943'::text, '-', ''), 'hex') from 1 for 8), 'base64'), '=', '');
@cdmckay
cdmckay / local-storage-size.js
Last active May 8, 2022 11:03
Detects the size of the browser's localStorage
if (localStorage && !localStorage.getItem('size')) {
var i = 0;
try {
// Test up to 10 MB
for (i = 250; i <= 10000; i += 250) {
localStorage.setItem('test', new Array((i * 1024) + 1).join('a'));
}
} catch (e) {
localStorage.removeItem('test');
localStorage.setItem('size', i - 250);
create or replace function uuid_to_muid(id varchar)
returns varchar as
$$
trim(base64_encode(
substring(hex_decode_binary(replace(id, '-', '')), 9, 8) ||
substring(hex_decode_binary(replace(id, '-', '')), 1, 8),
0,
'-_ '
))
$$;
@cdmckay
cdmckay / elvis.js
Created July 15, 2015 03:46
A simple "elvis" operator in JavaScript
function elvis(object, path) {
return path ? path.split('.').reduce(function (value, key) {
return value && value[key];
}, object) : object;
}
// Example
var o = { a: { b: 1, c: 2 }, d: 3 };
elvis(o, 'a');
// = { b: 1, c: 2 }
@cdmckay
cdmckay / MultipartFormDataWriteableExample.scala
Created June 24, 2015 05:04
An example of how to use MultipartFormDataWriteable
import play.api.libs.ws.WS
import play.api.mvc.MultipartFormData.FilePart
import play.api.mvc.MultipartFormData
import utilities.MultipartFormDataWriteable._
...
val url = "https://example.com"
val dataParts = Map(
templateService.getById(id) match {
case Found(template) => ...
case NotFound(message) => // message contains a nice (consistent message)
}
for {
checklist <- checklistService.getById(checklistId).toRight("checklist").right
template <- templateService.getById(checklist.template.id).toRight
} yield {
...
@cdmckay
cdmckay / elvis.php
Last active April 20, 2016 17:35
A simple "elvis" operator in PHP
<?php
function elvis($object, $path = null) {
return array_reduce(explode('.', $path), function ($subObject, $segment) {
return isset($subObject[$segment]) ? $subObject[$segment] : null;
}, $object);
}
// Example
$o = [ 'a' => [ 'b' => 1, 'c' => 2 ], 'd' => 3 ];
@cdmckay
cdmckay / MultipartFormDataWriteable.scala
Created June 24, 2015 04:39
Add support for MultipartFormData to the Play Scala WS library
package utilities
import java.io.{ByteArrayOutputStream, File}
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.multipart.{MultipartRequestEntity, FilePart, StringPart}
import play.api.http.HeaderNames._
import play.api.http.{ContentTypeOf, Writeable}
import play.api.mvc.{Codec, MultipartFormData}
@cdmckay
cdmckay / gist:7530967
Created November 18, 2013 16:37
A temporary fix for a bug in the Rhinofly s3-play library
def uploadPart(bucket: Bucket, uploadTicket: BucketFileUploadTicket, bucketFilePart: BucketFilePart): Future[BucketFilePartUploadTicket] = {
require(bucketFilePart.partNumber > 0, "The partNumber must be greater than 0")
require(bucketFilePart.partNumber < 10001, "The partNumber must be lesser than 10001")
// A hack way to get access to private httpUrl method
val url = bucket.s3.url(bucket.name, uploadTicket.name, 0).split('?')(0)
val uploadPart = bucket.s3.awsWithSigner
.url(url)
.withQueryString(
"partNumber" -> bucketFilePart.partNumber.toString,
@cdmckay
cdmckay / gist:3fb0007f6fee50fbbda1
Created December 21, 2015 17:42
Codecov error
==> Detecting CI provider
Circle CI Detected
==> Preparing upload
==> Processing gcov (disable by -X gcov)
Executing gcov (find /home/ubuntu/process-street -type f -name '*.gcno' -exec gcov -pb {} +)
==> Collecting reports
+ /home/ubuntu/process-street/target/scala-2.11/scoverage-data/scoverage.coverage.xml bytes=23057648
+ /home/ubuntu/process-street/target/scala-2.11/coverage-report/cobertura.xml bytes=4840129
+ /home/ubuntu/process-street/target/scala-2.11/scoverage-report/scoverage.xml bytes=11752247
==> Appending adjustments (http://bit.ly/1O4eBpt)