Skip to content

Instantly share code, notes, and snippets.

Janko Marohnić janko

Block or report user

Report or block janko

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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 / 1-activerecord.rb
Last active Jan 24, 2019
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 / 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"
@janko
janko / derivate_endpoint.md
Last active Nov 18, 2018
Plans for the new derivate_endpoint Shrine plugin
View derivate_endpoint.md

Derivate Endpoint

Derivate blocks are executed in context of the uploader instance:

class ImageUploader < Shrine
  plugin :derivate_endpoint

  derivate :thumbnail do |file, context|
@janko
janko / arel.rb
Created Jul 28, 2015
Insert statement in Arel
View arel.rb
require "active_record"
ActiveRecord::Base.establish_connection("postgres:///db")
insert = Arel::Nodes::InsertStatement.new
insert.relation = Arel::Table.new(:movies)
insert.columns = hash.keys.map { |k| Arel::Table.new(:movies)[k] }
insert.values = Arel::Nodes::Values.new(hash.values, insert.columns)
ActiveRecord::Base.connection.execute(insert.to_sql)
@janko
janko / script.rb
Created Oct 29, 2018
Zip unpacking with Shrine while retaining the same directory structure
View script.rb
require "sequel"
require "shrine"
require "shrine/storage/memory"
require "zip"
require "tempfile"
require "securerandom"
DB = Sequel.sqlite
DB.create_table :archives do
primary_key :id
You can’t perform that action at this time.