Skip to content

Instantly share code, notes, and snippets.

@pauljm
pauljm / asciiify.rb
Created September 17, 2015 23:34
Strategy for handling non-ASCII with Mail
# Escape non-ASCII characters and forward slashes as unicode escape sequences,
# but with forward slashes instead of backslashes, since backslashes already have meaning
def asciiify(str)
str.unpack('U*').map { |i|
(i == 47 || i == 92 || i > 127) ? "/u" + i.to_s(16).rjust(4, '0') : i.chr
}.join
end
# Decode "forward slashed" unicode escape sequences
def deasciiify(str)
@pauljm
pauljm / S3Inspect.scala
Last active June 28, 2019 21:39
Quick and dirty utility to list all the objects in an S3 bucket with a certain prefix and, for any whose key matches a pattern, read the file line by line and print any lines that match a second pattern. Adjust constants as appropriate. Usage: sbt 'run <AWS access key ID> <AWS secret key>'
/**
* Directory structure should be:
* - project_root
* - build.sbt (see below)
* - src
* - main
* - scala
* - S3Inspect.scala
*/
package s3inspect
@pauljm
pauljm / actor_example.scala
Last active December 25, 2015 17:49
Actor system example
/** Reads queue of new users to index */
class NewUserQueue extends Actor with ActorLogging { ... }
object NewUserQueue {
case class Listen(ref: ActorRef)
case class NewUsers(users: Seq[User])
}
/** Indexes documents for a new user */
class UserIndexer extends Actor with ActorLogging { ... }
@pauljm
pauljm / histogram_twirl.scala.html
Last active December 25, 2015 15:39
Histogram Scala/Twirl/HTML
@* Helper to draw <font> tags *@
@font(size: Option[Int] = Some(12), bold: Option[Boolean] = Some(false), color: Option[String] = None) = {<!--
--><font face='@Html(ctx.fontFace)'@{ if (color.isDefined) s" color='${color.get}'" else "" } style='@if(size.isDefined) { font-size: @{size.get}px; } @if(bold.isDefined) { font-weight: @{ if (bold.get) "bold" else "normal"}; }'><!--
-->}
@* Draw a spacer cell with text inside so Outlook doesn't ignore it *@
@spacerCell(width: Option[Any] = None, height: Option[Int] = None, rowspan: Int = 1, colspan: Int = 1, bgcolor: Option[String] = None) = {
<td rowspan=@rowspan colspan=@colspan@{ if (width.isDefined) s" width='${width.get}'" else "" }@{ if (height.isDefined) s" height='${height.get}'" else "" }@{ if (bgcolor.isDefined) s" bgcolor='${bgcolor.get}'" else "" } style='@{ if (height.isDefined) s"font-size: ${height.get}px; line-height: ${height.get}px;" else "font-size: 0; line-height: 0;" }'>&nbsp;</td>
}
@pauljm
pauljm / histogram_simple_4.html
Created October 15, 2013 23:03
Simple histogram, part 4
<tr>
<td height=20></td>
<td align='center' valign='middle'>A</td>
<td></td>
<td align='center' valign='middle'>B</td>
<td></td>
<td align='center' valign='middle'>C</td>
<td></td>
<td align='center' valign='middle'>D</td>
<td></td>
@pauljm
pauljm / histogram_simple_3.html
Last active December 25, 2015 15:39
Simple histogram, part 3
<!-- Row 3 -->
<tr bgcolor='#e4f1f5'>
<td height=15></td>
<!-- Spanning empty -->
<!-- Spanning empty -->
<!-- Spanning filled -->
<!-- Spanning empty -->
</tr>
<!-- Row 4 -->
@pauljm
pauljm / histogram_simple_2.html
Created October 15, 2013 23:00
Simple histogram, part 2
<tr bgcolor='#e4f1f5'>
<td height=15></td>
<td rowspan=3></td>
<td rowspan=2></td>
<td rowspan=5 align='center' valign='top' bgcolor='#6db2c8'>5</td>
<td rowspan=4 align='center' valign='bottom'>1</td>
</tr>
@pauljm
pauljm / histogram_simple_1.html
Last active December 25, 2015 15:39
Simple histogram, part 1
<table cellspacing=1 cellpadding=0 bgcolor='#e68265'>
<tr bgcolor='#e4f1f5'>
<td width=8 height=8></td>
<td width=32 height=8></td>
<td width=8 rowspan=6></td>
<td width=32 height=8></td>
<td width=8 rowspan=6></td>
<td width=32 height=8></td>
<td width=8 rowspan=6></td>
<td width=32 height=8></td>