Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / 01-activerecord.rb
Created May 27, 2015 22:50
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
require "active_record"
ActiveRecord::Base.establish_connection('postgres:///testing')
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.class_eval do
create_table :played_quizzes, force: true do |t|
t.integer :player_ids, array: true
t.json :quiz_snapshot
end
@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
@janko
janko / 1-activerecord.rb
Last active June 13, 2023 20:00
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Migration.class_eval do
create_table(:records) do |t|
t.string :column
end
end
data = 50_000.times.map { |i| Hash[column: "Column #{i}"] }
@janko
janko / 01-requirements.md
Last active March 6, 2023 11:15
ActiveRecord vs Sequel (require time)
$ brew install cloc
$ bundle install
@janko
janko / Gemfile
Created May 31, 2018 19:57
Memory profiling of http.rb and other popular Ruby HTTP client libraries
source "https://rubygems.org"
gem "roda"
gem "http", "~> 3.3"
gem "rest-client"
gem "httparty"
@janko
janko / 01-safe-download.rb
Last active January 9, 2023 11:40
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
@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 September 5, 2022 23:13
Workaround for embedded documents in shrine-mongoid
require "mongoid"
require "shrine"
require "shrine/storage/memory"
require "stringio"
Mongoid.configure do |config|
config.clients.default = { hosts: ['localhost:27017'], database: 'my_db' }
config.log_level = :debug
end
@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