Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / rodauth.rb
Last active December 19, 2023 19:51
Rodauth 2FA example (TOTP & recovery codes) via JWT
require "roda"
require "sequel"
require "rack/test"
require "json"
DB = Sequel.sqlite
DB.create_table :accounts do
primary_key :id
String :status_id, default: 1, null: false
String :email, null: false
@janko
janko / application_controller.rb
Last active November 7, 2023 21:25
Implementing Devise groups in Rodauth
class ApplicationController < ActionController:Base
extend ControllerMacros
end

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@janko
janko / Gemfile
Last active November 1, 2020 17:26
Benchmark measuring execution time and memory allocation of different strategies of inserting many records in Sequel – 1a) individual insert, 1b) individual prepared insert, 2a) bulk insert, 2b) batched bulk insert, and 3) database insert
source "https://rubygems.org"
gem "sequel"
gem "pg"
gem "sequel_pg"
gem "memory_profiler"
@janko
janko / benchmark.rb
Last active September 11, 2020 21:08
Comparing performance of Sequel and Active Record when fetching 1000 records
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord", "6.0.3.3", require: "active_record"
gem "sequel", "5.36"
gem "sequel_pg", "1.13.0", require: false
gem "pg", "1.2.3"
gem "mysql2", "0.5.3"
@janko
janko / shrine-cli.rb
Created March 24, 2020 10:58
The CLI I wrote for creating new AWS S3 buckets for reproducing issues reported for Shrine. It's can serve as an example for programmatically creating new S3 buckets with bucket-specific credentials.
#!/usr/bin/env ruby
require "dry/cli"
require "aws-sdk-s3"
require "aws-sdk-iam"
require "json"
ENV["AWS_ACCESS_KEY_ID"] = "..."
@janko
janko / application_controller.rb
Last active May 8, 2021 06:58
Simpler implementation of ActionController::Live
class ApplicationController < ActionController::Base
private
def stream_body(async: false, **options, &block)
self.status ||= 200
self.headers["Cache-Control"] = "no-cache"
self.headers.delete("Content-Length")
stream_class = async ? AsyncStream : Stream
@janko
janko / results
Last active December 14, 2022 09:15
MiniMagick vs libvips benchmark (generating a 500x500 thumbnail)
width, imagemagick, libvips
1000, 0.129, 0.045
1500, 0.190, 0.067
2000, 0.276, 0.054
2500, 0.380, 0.068
3000, 0.498, 0.085
@janko
janko / script.rb
Last active January 25, 2019 00:50
Ruby operator coercion inconsistency
Value = Struct.new(:value) do
def coerce(other)
puts "called #{self}#coerce(#{other.inspect})"
[Value.new(other), self]
end
def +(other)
puts "called #{self} + #{other.inspect}"
end
@janko
janko / derivate_endpoint.md
Last active November 18, 2018 10:59
Plans for the new derivate_endpoint Shrine plugin

Derivate Endpoint

Derivate blocks are executed in context of the uploader instance:

class ImageUploader < Shrine
  plugin :derivate_endpoint

 derivate :thumbnail do |file, context|