Skip to content

Instantly share code, notes, and snippets.

View mziwisky's full-sized avatar

Mike Ziwisky mziwisky

  • Lumio
  • Seattle, WA
View GitHub Profile
@mziwisky
mziwisky / binary_search.rb
Created May 13, 2021 07:27
Binary search a Rails model by created_at when created_at is not indexed
# binary search by ID to find record of type `model` whose created_at is closest to `timestamp`.
# assumes created_at is a monotonically increasing function of ID.
# `low_end_bias` is used to begin the search somewhere other than the first
# model ever. it's a percentage of records. useful when you know that the thing
# you're looking for is near the end of the list, which is the case for the
# thing I'm currently looking for, so I'm defaulting it to 0.99
def find_closest_to(model, timestamp, low_end_bias = 0.99)
high_mark = model.last
low_id = (high_mark.id * low_end_bias).round
@mziwisky
mziwisky / main.rb
Last active November 9, 2020 02:23
proxying I/O from one script to another
#!/usr/bin/env ruby
require 'open3'
puts 'party time'
output = ''
status = Open3.popen2e('./sub.rb') do |std_in, std_out_err, wait_thr|
Thread.new do
begin
while out = std_out_err.readpartial(64)
@mziwisky
mziwisky / dinghy.yml
Created June 6, 2019 14:58
dory and dinghy config
# ~/.dinghy/preferences.yml
---
:preferences:
:unfs_disabled: false
:proxy_disabled: true
:dns_disabled: false
:fsevents_disabled: false
:create:
cpus: 4
memory: 16384
@mziwisky
mziwisky / focusWithMethod.jsx
Created November 1, 2017 17:49
comparison of using props vs invoking a method to tell a child component to focus something
Child = React.createClass({
focusThing () {
this.btn.focus();
},
render () {
return (
<div>
Here's the thing to <button ref={ el => this.btn = el }>focus</button>
@mziwisky
mziwisky / output.txt
Created February 28, 2017 01:02
aws-sdk Aws::S3::Object#copy_from vs #copy_to
irb(main):001:0> require_relative 'test'
AWS SDK version: 2.7.16
== TEST: test_copy_west_from_east
IT'S GOOD
== TEST: test_copy_east_from_west
IT'S GOOD
# ^ copy_from works fine regardless of direction
@mziwisky
mziwisky / foo.js
Created December 6, 2016 20:38
unbinding of `this` for `import { x } from "y";` syntax
let foo = {
alpha () {
return this.bravo();
},
bravo () {
return "charlie";
}
}
export default foo;
* 3e39dc2 (HEAD -> declare-positionState, origin/declare-positionState) declare positionState var
* 6bb203e (origin/remove-jsx-pragma, remove-jsx-pragma) remove `@jsx` pragma from source
* e64cb8e (origin/react-0.14, react-0.14) update for react-0.14
* 18fcafd (origin/allow-role-override, allow-role-override) allow overriding role and setting arbitrary props on MenuOption
* dee59db (neil/master, neil-clickblur-handling) Limit focusing of react-menu to fix UI bug
* db914fc (duane/remove-constrain, duane/master, duane) Remove constrain
* 16a04d3 (neil/preferred_position, duane/preferred_position) Use react getDefaultProps instead of constrain
* fac6ffa Add preferredHorizontal and preferredVertical props
* 103b0c3 (neil/toggle_option, duane/toggle_option) Add keepOpenOnSelect boolean option to Menu
| * 8288c86 (karma-webpack) WIP replace browserify with webpack for builds
@mziwisky
mziwisky / AjaxInterceptorManager.js
Created January 22, 2015 23:07
ajax interceptor management
var authExpirationHandler,
errorHandler;
function catchUnauthorizedResponses(data) {
if (data.status === 401 && !maybeLocalStorage.getItem("token")) {
// ensure the in-memory session really is expired before destroying it
return axios.get("/api/auth/session").then(
(response) => {
// we ARE still auth'd, so just throw the error down the chain
throw data;
@mziwisky
mziwisky / Oauth2.md
Last active November 17, 2024 01:09
Oauth2 Explanation

OAUTH2

The Problem

I’m a web app that wants to allow other web apps access to my users’ information, but I want to ensure that the user says it’s ok.

The Solution

I can’t trust the other web apps, so I must interact with my users directly. I’ll let them know that the other app is trying to get their info, and ask whether they want to grant that permission. Oauth defines a way to initiate that permission verification from the other app’s site so that the user experience is smooth. If the user grants permission, I issue an AuthToken to the other app which it can use to make requests for that user's info.

Note on encryption

Oauth2 has nothing to do with encryption -- it relies upon SSL to keep things (like the client app’s shared_secret) secure.

ruby-debug cheatsheet

s[tep] -- step into next call

n[ext] -- step over next call

l[ist] = -- show the current line, in context. e.g.:

   26          end

27 end