Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
@troelskn
troelskn / gist:773216
Created January 10, 2011 18:40
json_encode_pretty
<?php
/**
* Input an object, returns a json-ized string of said object, pretty-printed
*
* @param mixed $obj The array or object to encode
* @return string JSON formatted output
*/
function json_encode_pretty($obj, $indentation = 0) {
switch (gettype($obj)) {
case 'object':
@troelskn
troelskn / app.rb
Last active August 12, 2021 17:25 — forked from dstrelau/app.rb
Gollum protected by HTTP Basic
require 'gollum/frontend/app'
require 'digest/sha1'
class App < Precious::App
User = Struct.new(:name, :email, :password_hash, :can_write)
before { authenticate! }
before /^\/(edit|create|delete|livepreview|revert)/ do authorize_write! ; end
helpers do
@troelskn
troelskn / underscore.php
Created December 22, 2010 13:45
camelize + underscore in php
<?php
/**
* Transforms an under_scored_string to a camelCasedOne
*/
function camelize($scored) {
return lcfirst(
implode(
'',
array_map(
module Mojibake
# UTF-8 content, interpreted as latin1
class Utf8
CANARY = "æøåÆØÅ".chars.map { |c| c.encode(Encoding::UTF_8).force_encoding(Encoding::ISO_8859_1).encode(Encoding::UTF_8) }.freeze
def detect?(content)
CANARY.any? { |c| content.include?(c) }
end
def repair(content)
@troelskn
troelskn / Capfile
Created January 26, 2016 11:02
Sample cap setup
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
@troelskn
troelskn / smtp_email_validate.rb
Created September 30, 2020 10:09
Validate email via smtp
class EmailValidation
SMTP_PORT = 25
CONNECTION_TIMEOUT = 3
READ_TIMEOUT = 3
REGEX_SMTP_ERROR_BODY_PATTERN = /(?=.*550)(?=.*(user|account|customer|mailbox)).*/i.freeze
def initialize(verifier_domain:, verifier_email:)
@verifier_domain, @verifier_email = verifier_domain, verifier_email
end
@troelskn
troelskn / gist:10102663
Created April 8, 2014 08:52
jquery - find closest data attribute.
var listId = $(this).parents().filter(function() { return $(this).data("list-id"); }).eq(0).data("list-id");
# takes an array of hashes and prints as a table on the console
def table_print(rows)
table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) }
sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max)
puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
end
@troelskn
troelskn / ruby_print.rb
Created April 26, 2019 09:18
Ruby pretty print with rubocop standard syntax
class RubyPrint
def self.pp(mixed)
puts new.transform(mixed)
end
def transform(mixed, indentation = 1)
if mixed.is_a?(Hash)
transform_hash mixed, indentation
elsif mixed.is_a?(Array)
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound = Class.new(ActiveRecord::ActiveRecordError)
# Like +first!+, but will also fail if the query would match more than
# one record.