Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / openssl.md
Last active August 29, 2015 13:58
OpenSSL upgrade instructions

These are upgrading instructions for the new OpenSSL vulnerability, Heartbleed. This is only for Mac, and assumes you have Homebrew installed.

OpenSSL

You need to upgrade OpenSSL to at least version 1.0.1g.

$ brew update
$ brew upgrade openssl
$ brew link --force openssl
@janko
janko / ldap.rb
Last active August 29, 2015 14:02
Example Faraday integration with the LDAP protocol, with caching to the database.
require "faraday"
module Faraday
class Adapter
class NetLdap < Faraday::Adapter
def call(env)
# LDAP request, and call `save_response(env, status, body, headers)`
@app.call(env)
end
end
@janko
janko / application.rb
Created June 7, 2014 16:57
Custom error pages in Rails
module MyApp
class Application < Rails::Application
# ...
config.exceptions_app = self.routes
config.action_dispatch.rescue_responses.merge!(
"RDS::ResourceNotFound" => :not_found,
)
# ...
end
@janko
janko / dive.rb
Last active August 29, 2015 14:09
A script that opens any gem in your EDITOR.
#!/usr/bin/env ruby
# USAGE: dive GEM_NAME
#
# Opens the lib/ directory of the specified gem in EDITOR. If you're in a Bundler
# project, it will open the locked-down version of the gem.
#
# You can supply only the first few letters of the gem, and the first gem that
# matches will be opened.
@janko
janko / Gemfile
Last active August 29, 2015 14:13
Specifying dependencies: Java vs Ruby vs Node
source "https://rubygems.org"
gem "rspec", "~> 3.1"
gem "rspec-rails", "~> 3.1"
@janko
janko / activerecord.rb
Created April 4, 2015 20:53
Sequel's vritual rows are awesome
Movie.select(
:year,
"ts_headline(title, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS title",
"ts_headline(plot, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS plot",
"ts_headline(episode, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS episode"
)
@janko
janko / benchmark.rb
Last active August 29, 2015 14:18
Minitest loads *slower* than RSpec?
require "benchmark/ips"
File.write "minitest_test.rb", <<-EOS
require "minitest/autorun"
require "minitest/pride"
class MintestTest < Minitest::Test
def test_foo
assert true
end
@janko
janko / benchmark.rb
Created May 19, 2015 23:17
Serialization performance
gem "active_model_serializers", "0.10.0.rc1"
require "yaks"
require "active_record"
require "active_model_serializers"
require "benchmark/ips"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "testing")
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
@janko
janko / 1-models.rb
Created May 27, 2015 17:50
Components of good application design (my presentation for a local Ruby meetup)
# 1. Models (Entities)
#
# * The nouns of your business logic
# * Usually persisted in the database
# * Usually contain validations
# * Usually expose associations with other models
# * Usually have scopes (to encapsulate the query logic)
################
# ActiveRecord #
@janko
janko / 01-classic.rb
Created May 27, 2015 22:38
Controller refactoring (my presentation at a local Ruby meetup)
#################
# CLASSIC STYLE #
#################
class SessionsController < ApplicationController
# ...
def create
if user = AuthenticateUser.call(params[:username], params[:password])
sign_in!(user)