Skip to content

Instantly share code, notes, and snippets.

View briankung's full-sized avatar

Brian Kung briankung

  • Chicago
View GitHub Profile
@briankung
briankung / activerecord vs arel command comparison.mysql
Created April 24, 2019 02:56
ActiveRecord vs Arel command comparison
-- Story.base.positive_ranked.joins(:taggings).where(taggings: { tag_id: tags.map(&:id) })
EXPLAIN SELECT `stories`.*
FROM `stories`
INNER JOIN `taggings`
ON `taggings`.`story_id` = `stories`.`id`
WHERE `stories`.`merged_story_id` IS NULL
AND `stories`.`is_expired` = false
AND ( ( Cast(upvotes AS signed) - Cast(downvotes AS signed) ) >= 0 )
AND `taggings`.`tag_id` = 159;
import { FluentBundle } from 'fluent/compat';
import { negotiateLanguages } from 'fluent-langneg/compat';
const MESSAGES_ALL = {
'pl': `
title = Witaj świecie!
today-is = Dziś jest { DATETIME($date, month: "long", day: "numeric") }.
`,
'en-US': `
title = Hello, world!
@briankung
briankung / 00 - lobsters top level comment search.js
Last active March 29, 2019 02:14
lobste.rs top level comment search using the root level comment input. If you're using the bookmarklet, save it as a bookmark and then click it once when you want to search top-level comments. Otherwise just copy the contents into console.
javacript:void((() => {
const topLevelComments = [...document.querySelectorAll('ol.comments > li > div')].slice(1),
hide = (node) => { node.closest('li').style.display = 'none' },
show = (node) => { node.closest('li').style.display = null },
reset = () => { topLevelComments.forEach(show) };
window.query = (query) => {
// Convenient for using it as an event handler
if (query && query.target) { query = query.target.value }
@briankung
briankung / lobsters new comments.js
Last active March 25, 2019 22:03
Bookmarklet to scroll through lobste.rs comments in order
javascript:void(
(function() {
this.distanceFromTop = (a, b) => {
return Math.sign(a.getBoundingClientRect().top - b.getBoundingClientRect().top);
};
this.setUnreadElements = () => {
document.unreadElements = [...document.querySelectorAll('[class*=unread]')].sort(this.distanceFromTop);
};
@briankung
briankung / Advent of Code.md
Last active December 2, 2018 22:45
Advent of Code 2018
@briankung
briankung / timingattack.rb
Created October 7, 2018 14:51
Timing attack test
# $ gem install benchmark-ips
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("long correct string: ") { "abfhbeufangi" == "abfhbeufangi" }
x.report("long mostly correct string: ") { "abfhbeufangx" == "abfhbeufangi" }
x.report("long incorrect string: ") { "bbfhbeufangi" == "abfhbeufangi" }
x.report("too short: ") { "bbfhbe" == "abfhbeufangi" }
x.report("too long: ") { "abfhbeufangibbfhbe" == "abfhbeufangi" }
@briankung
briankung / 00.md
Last active December 4, 2020 12:13
[RFC] Making sense of secure token authentication in Rails

/ be me

/ be working on new Rails API web app

/ ...I can't. I hate this format

...so I was looking up how to do simple, secure authentication in Rails for my super secret project which you definitely can't find by looking at recent activity on my [GitHub account] (Hail [Mammon][[0][zero]]) and realized that I hadn't implemented authentication in Rails in a long time. The closest I'd come to was throwing [Devise] into my app and grumpily remembering to configure the mailer after the fact.

This will seem unrelated, but bear with me: I learned how to calculate interest rates on loans in something like 6th grade. I have since forgotten how to do that without assistance. Much like how I would have been much better prepared to choose a mortgage back then, I would in some ways be much better prepared to figure out authentication a few years ago when I was just starting out in web dev - and had just implemented it from the [Hartl tutorial]. That said, just like how I now know more of what I'm looking for

@briankung
briankung / 00 - I never said it was good.md
Last active May 24, 2018 20:14
[RFC] GitHub Gists are officially the backend of my blog

Well! I'm officially pretty happy with the fruits of my labor. It's not the prettiest, aesthetically or in terms of code quality, but hey, it does exactly what I want it to. Now I can use GitHub Gists as the backend to my blog - at least until I hit [3000 gists]. I count that among problems I probably won't have to deal with, including:

  1. Getting slashdotted / hackernewsed
  2. GitHub changing their access model for public gists
  3. GitHub rate limiting me
  4. Having more blog posts than I know what to do with
  5. Having more viewers or subscribers than I know what to do with

I'll deal with those problems as they come. In the meantime, a quick rundown of what changed since the last post:

@briankung
briankung / 00.md
Last active May 25, 2022 17:11
[RFC] Forcing GitHub Gists into a blog-shaped hole

Well, I didn't know this was going to be as hard as it turned out to be, but sometime yesterday I decided that I didn't really want to manage a backend and GitHub Gists were where my technical thoughts ended up, anyway (and I didn't want to handle embeds, either) so why not just hit up Gists for my tech blog? That way, I'd get syntax highlighted embeds, comments, version management, and more.

As with a lot of technical things, it was much more difficult than I gave it credit for at first, mostly because of embeds and the CORS policy on GitHub Gists.

The first step was to get my GitHub Pages running locally, which I did with this convenient python3 one-liner: python -m http.server 8000. Loading up http://localhost:8000/ gave me a look at my static files. I set up a barebones html file with some script tags for the majority of the logic.

I knew I could access the GitHub Gists through some sort of [API], but I wasn't sure what I got with it. Shouldn't be a problem, right? Sure enough, it was as easy as hi