Testing for TLS 1.2

iOS 9 requires all NSURLConnection connections to support TLS 1.2. So I needed to check my servers to see if this was indeed configured. After looking around a bit I was able to find that you could test for this using openssl.

On OS X with homebrew you can test with this command:

/usr/local/Cellar/openssl/1.0.2d_1/bin/openssl s_client -tls1_2 -connect www.codeography.com:443

If you have linux (with a new enough openssl) or have overridden the system openssl on OS X you can just leave off the full path:

openssl s_client -tls1_2 -connect www.codeography.com:443

If it is supported you'll see the Certificate chain as well as some information about the connection. The bit I was looking for was "Protocol: TLSv1.2"

Looked something like this:

New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
    Protocol  : TLSv1.2
    TLS session ticket lifetime hint: 1200 (seconds)

Playing Minecraft when the Port is Blocked

I was at a conference this last weekend and the network blocked the port that minecraft needed to connect to the server. So I spent longer than I would like to admit setting up a ssh tunnel. Here's what I did:

1) Create the tunnel

ssh -L 8080:yourminecraftserver.club:25565 -p 22 -l yoursshuser -N yoursshserver.com

This will stay running in the terminal. Just leave it going while you play.

2) Configure minecraft to use that tunnel

Create a new server by selecting "Multiplayer -> Add Server" and use the following settings:

Server Name:
Server Address:

Now you should be able to connect and build that dirt hobble you've been planning.

How do you write great commit messages?

The best way to learn to write great commit messages is to go back and read them.

When reading code take a moment and go look at the git history every time you mutter "why did they do that." That's the point in time when you want to know the real "why" behind the change. If you don't already know how to to that, take moment to figure out a way quickly to access the history of a line in your editor or in your repo, the less friction the easier it is to go back and read about the changes.

If you make a habit out of this, you will quickly learn what is helpful and what is not. Then, if you are like me, you'll want to go back to slap your past-self for being lazy and giving a few bullet points of "what" was changed and not "why" it was changed.

Sound good? You in? Cool.

To get started just add the word "because" to your commits.

Lets look at an example.

Changed the post title font size

Changed 0.8em 16px.

h2 {
-  font-size: 0.8em;
+  font-size: 16px;

And what if I ever come across this file, I might wonder why this person strayed from the convention of using relative font sizes to pixels. I would would mutter "wtf" to myself and pop open the git blame in my editor for that line and read the commit message. This right here is the point that I want to know why.

Changed the post title font size

Fixed it to 16 pixels because when we increased the overall font size on
the blog the titles were wrapping on mobile views.

h2 {
-  font-size: 0.8em;
+  font-size: 16px;

Now this is helpful. It gives me context. I get clues on what changed in this code and what might break. And I can see that it wasn't some arbitrary change. Me in 6 months will appreciate this.

A commit already contains a diff, so it shows what was changed. No reason to write that up again. But nothing explains why you made that horrible hack or sweeping change.

Communicating the current reasoning to your future self is a super power.

Further Reading

There have been many posts about commit message from formatting to content. Here are a few of my favorite, should you want to level up:

Tim Pope's A Note About Git Commit Messages. This is formatting and syntax, but if you follow this I will be much, much happier.

Caleb Thompson's 5 Useful Tips For A Better Commit Message. A bit vim-centric, but great guidelines.

Stephen Ball's Deliberate Git. An in depth guide to commit messages. Worth the time to read and understand.

Keavy McMinn's How to write the perfect pull request. Slightly different than commit messages, but they really go well together.

Render a JBuilder template outside of a request

I like to use the built-in JBuilder for rendering JSON in my Rails apps. I particularly like that it is an actual view template, gives you the control and tools you need to easily build up the document. There is no need to teach your models about icky things like URLs and hostnames -- that can be rendered with view helpers as part of the request.

The one problem is there is no easy way to use those templates outside of a Rails response. But luckily Aaron came up with a helpful class to setup everything properly and mimic the Request. He even added support for setting things like the location header.

To use it in a service object (like say a rake task that runs on a worker process):

# Create the object
json_view = JsonView.new

# Set any headers that matter, where `posts` is the obligatory example resource.
location = json_view.location_url(posts)

# Render the template
payload = json_view.render(:show, posts: posts)

Pretty cool, eh?

His implmentation that takes care of all the details of the request:

class JsonView
  attr_reader :controller
  def initialize(env = {})
    @controller = API::V1::BeaconsController.new
    controller.request = ActionDispatch::Request.new(default_env.merge(env))

  def render(view, **locals)
    controller.render_to_string(view, locals: locals)

  def https?
    @_https ||= ENV.fetch("SERVER_HTTPS") {

  def default_env
      "SERVER_NAME"     => ENV.fetch("SERVER_NAME") {
        abort "Missing environment key: SERVER_NAME"
      "SERVER_PORT"     => https? ? "443" : "80",
      "HTTPS"           => https? ? "on" : "off",
      "rack.url_scheme" => https? ? "https" : "http",
      "CONTENT_TYPE"   => "application/json",
      "HTTP_ACCEPT"    => "application/json",

  # Helpful shim reaching into the controller private stuff
  def location_url(beacons)
    controller.send(:location_url, beacons)

  def respond_to_missing?(meth, include_all)
    controller.respond_to?(meth) || super

  def method_missing(sym, *args, &block)
    if controller.respond_to?(sym)
      controller.send(sym, *args, &block)

Upload to imgur from applescript

A little scrip

on run {input, parameters}
  tell application "Finder"
    -- convert file paths to posix
    set imageList to {}
    set linkList to {}
    repeat with i from 1 to (count input)
      set end of imageList to POSIX path of (item i of input as alias)
      set file_name to name of (item i of input as alias)
    end repeat

    -- no images selected
    if (count imageList) is 0 then
      display dialog "No image files selected" with title "Imgur uploader" buttons {"Quit"} default button "Quit"

      display notification "Preparing to upload " & (count imageList) & " images" with title "Uploaded Started"

      repeat with i from 1 to (count imageList)
        set apiKey to "26ff5c40cbedf50e7f81124ab473c1cc"
        set curlCommand to "curl -F \"key=" & apiKey & "\" -F \"image=@" & item i of imageList & "\" http://api.imgur.com/2/upload"
        set answer to do shell script curlCommand
        set atid to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "<original>"
        set link to text item 2 of answer
        set AppleScript's text item delimiters to "<"
        set link to text item 1 of link
        set AppleScript's text item delimiters to atid
        set end of linkList to link
      end repeat
    end if
  end tell

  set links to joinList(linkList, " ")
  set the clipboard to links

  display notification "Successfully Uploaded Screenshot and Copied the URL to the Clipboard" with title "Uploaded Finished" sound name "Purr"

end run

to joinList(aList, delimiter)
  set retVal to ""
  set prevDelimiter to AppleScript's text item delimiters
  set AppleScript's text item delimiters to delimiter
  set retVal to aList as string
  set AppleScript's text item delimiters to prevDelimiter
  return retVal
end joinList