Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

  • Colorado, USA
  • 16:14 (UTC -07:00)
  • LinkedIn in/jko170
View GitHub Profile
@justinko
justinko / application.html.erb
Created September 14, 2024 16:44
Capybara wait for JavaScript to be ready
<!DOCTYPE html>
<html>
<body data-controller="test-readiness">
</body>
</html>
require 'rspec/autorun'
class FlattenArray
def self.call(array)
new(array).call
end
def initialize(array)
@array = array
@flat_array = []

Keybase proof

I hereby claim:

  • I am justinko on github.
  • I am jko170 (https://keybase.io/jko170) on keybase.
  • I have a public key whose fingerprint is 01F0 22A4 834B B3C4 FF22 4029 EEF7 D932 65EA 5E49

To claim this, I am signing this object:

@justinko
justinko / roman_numeral_calculator.rb
Last active June 4, 2017 21:34
Roman Numeral Calculator
require 'rspec/autorun'
class RomanNumeralCalculator
SYMBOL_MAPPING = {
1 => 'I',
4 => 'IV',
5 => 'V',
9 => 'IX',
10 => 'X',
40 => 'XL',
def merge_string(a, b)
[a, b].map(&:size).max.times.with_object('') do |(index), output|
[a, b].each {|var| output << (var[index] || '') }
end
end
@justinko
justinko / distributed_mutex.rb
Created May 9, 2017 23:20
Distributed Mutex with sidekiq-ent
class DistributedMutex
Blocked = Class.new(StandardError)
DEFAULT_OPTIONS = {
wait_timeout: 0,
lock_timeout: 30,
policy: :raise,
ttl: 1.hour
}
@justinko
justinko / realtime.rb
Created June 12, 2014 21:56
Ghetto Benchmark Function
require 'benchmark'
def realtime(message = nil, count: 1, &block)
results = []
count.times { results << Benchmark.realtime(&block) }
puts
puts message if message
puts results.sum / results.size.to_f
puts
end
@justinko
justinko / api_versioning.rb
Last active August 29, 2015 13:56
Inheritance based API versioning in Rails
class ApiVersioning
class Middleware
def initialize(app)
@app = app
end
def call(env)
if version_number = extract_version_number(env)
ApiVersioning.current_version_number = version_number.to_i
Rails.application.eager_load! unless Rails.application.config.cache_classes
@justinko
justinko / output.txt
Last active December 21, 2015 13:39
Parse streaming JSON
[3] pry(main)> streaming_json = StreamingJSON.new
=> #<StreamingJSON:0x007fe222b9d8e8
@buffer=
#<StreamingJSON::Buffer:0x007fe222b9d898
@complete=[],
@incomplete=[],
@nesting=#<StreamingJSON::Buffer::Nesting:0x007fe222b9d7d0 @level=0>,
@recording=false>>
[4] pry(main)> streaming_json[%({"a":)]
=> []
@justinko
justinko / mongo_aggregation.rb
Created June 10, 2013 18:00
Demonstrate MongoDB aggregation with the Ruby Mongoid gem.
require 'mongoid'
require 'pp'
Mongoid.configure do |config|
config.allow_dynamic_fields = false
config.connect_to 'mongo_test', { consistency: :strong }
end
Mongoid.default_session.drop