danott.co

Archive 2020

Today I Learned

June 27, 2020

A byte order mark is an optional character that may appear at the beginning of a text stream. The unicode character U+FEFF can be used to signal several things to a program reading the text.

My program reading the text is Ruby’s JSON.parse. The text being read a JSON payload from the Pinboard API.

A helpful email exchange with Pinboard support is what pointed me in the right direction. A recent server upgrade began inserting the byte order mark where it wasn’t present before. It sounds like this insertion is going to be removed, because it makes the raw JSON payloads un-parsable by JSON.parse in both Ruby and JavaScript.

In the meantime, I wanted to get my parsing fixed, so I could keep my Pinboard powered links up-to-date. With the tip from Pinboard support and some StackOverflow I got my JSON parsing fixed.

  def load_remote_json
    uri = URI("https://api.pinboard.in/v1/posts/all?tag=danott.co&format=json&auth_token=#{auth_token}")
    body = Net::HTTP.get(uri)
+   body = strip_byte_order_mark(body)
    JSON.parse(body)
  end

+ def strip_byte_order_mark(potentially_unparsable)
+   potentially_unparsable.dup.sub("\xEF\xBB\xBF".force_encoding("UTF-8"), "")
+ end

This experience highlighted the tradeoffs of using vcr for tests. On the positive side, I had a cassette recording that captured the old API response. This allowed me to diff the old response and the new response to dig down and find what changed. On the negative side, I didn’t encounter the failure in parsing until it broke my link importing in production.

Tradeoffs.




Deku Deals

Nintendo Switch price tracking and wishlist notifications

I started building something like this on AWS when I wanted to wait for Celeste to go back on sale. I’m so happy somebody else did it and I can delete those stale resources.





































Today I Learned

January 13, 2020

Needing to reduce a pathname into the filename without it’s extension is something I need to do often. But not too often. Just often enough to forget how I last did it, and clobber together a solution anew.

So this time I’m writing down the briefest solution I’ve found in Ruby, in hopes that it’ll commit to my memory.

require "pathname"
pathname = Pathname.new("~/some/path/to/the/mountains.jpg")
pathname.basename(pathname.extname).to_s # => "mountains"

Pathname is a great tool in the standard library.




Today I Learned

January 7, 2020

Sometimes you install one gem.

gem install bundler

Sometimes you install it with a specific version.

gem install bundler -v "2.0.2"

Sometimes you install multiple gems.

gem install bundler minitest

But what do you do if you need to install multiple gems with specific versions? It is possible with a special syntax!

gem install "bundler:2.0.2" "minitest:5.13.0"


Today I Learned

January 6, 2020

Something I really like from React Native is the global variable __DEV__. Being able to do things only in the development environment is quite handy. This concept is not unique to React Native, but that’s where I was introduced to it.

I’ve found myself in need of a similar strategy while building this static site in Rails. I have some dynamic interactions that I can use to curate content on my own machine, but I don’t want those scripts bloating the static build that is deployed to production.

This can be achived in Rails with webpacker using Webpack’s DefinePlugin!

// config/webpacker/environment.js
const { environment } = require("@rails/webpacker")
const webpack = require("webpack")

environment.plugins.prepend(
  "Define",
  new webpack.DefinePlugin({
    STATIC: process.env.NODE_ENV === "production",
  }),
)

module.exports = environment









Rise of Skywalker is the Rian Johnson movie we thought we wanted. Knives Out is the Rian Johnson movie we needed.