Skip to content

Instantly share code, notes, and snippets.

Janko Marohnić janko

View GitHub Profile
@janko
janko / script.rb
Last active Apr 30, 2020
Workaround for embedded documents in shrine-mongoid
View script.rb
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 / 01-activerecord.rb
Created May 27, 2015
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
View 01-activerecord.rb
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 / shrine-cli.rb
Created Mar 24, 2020
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.
View shrine-cli.rb
#!/usr/bin/env ruby
require "dry/cli"
require "aws-sdk-s3"
require "aws-sdk-iam"
require "json"
ENV["AWS_ACCESS_KEY_ID"] = "..."
@janko
janko / 1-activerecord.rb
Last active Jan 28, 2020
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
View 1-activerecord.rb
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 / application_controller.rb
Last active Oct 31, 2019
Simpler implementation of ActionController::Live
View application_controller.rb
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 Aug 24, 2019
MiniMagick vs libvips benchmark (generating a 500x500 thumbnail)
View results
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 / 01-safe-download.rb
Last active Jul 24, 2019
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
View 01-safe-download.rb
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
@janko
janko / 01-presentation.md
Last active Jun 6, 2019
PostgreSQL full-text search capabilites (my presentation from our local Ruby meetup)
View 01-presentation.md

Full-text search

  • Keywords

  • Typos

  • Stemming

  • Stopword ignore

@janko
janko / script.rb
Last active Jan 25, 2019
Ruby operator coercion inconsistency
View script.rb
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 / Gemfile
Created May 31, 2018
Memory profiling of http.rb and other popular Ruby HTTP client libraries
View Gemfile
source "https://rubygems.org"
gem "roda"
gem "http", "~> 3.3"
gem "rest-client"
gem "httparty"
You can’t perform that action at this time.