Skip to content

Instantly share code, notes, and snippets.

Aleksandar Kostadinov akostadinov

Block or report user

Report or block akostadinov

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
akostadinov / xml_builder.rb
Created Jan 26, 2017
Building XML under Rails 5.
View xml_builder.rb
require 'builder'
# See more about syntax on
xml = +""
builder = xml, indent: 2)
builder.rootelementname { |x|
akostadinov / post_string_file.rb
Created Jan 26, 2017
ruby rest-client posting String as a file multipart/form-data
View post_string_file.rb
# rest-client recognizes files in multipart/form-data by checking
# the object type passed as the field content. We need to craft
# our string to appear as a file IO. `#stringfile` does that.
# An alternative approach is to just write string to a file and
# pass file IO to rest-client but this would be suboptimal
# provided we already have the content as a String.
def stringfile(string,
filename="file_#{rand 100000}",
akostadinov / quine.rb
Created Jan 26, 2017
my first suboptimal ruby quine
View quine.rb
pr = %q{puts %{pr = %q{#{pr}}}, pr}
puts %{pr = %q{#{pr}}}, pr
akostadinov / rails_test.rb
Last active Nov 9, 2016
Rails Active Record simple test
View rails_test.rb
# run by `ruby rails_test.rb`
# credits to
gem 'activerecord', ''
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
akostadinov /
Last active Jan 16, 2020
Get stack trace in Bash shell script/program.
# LICENSE: MIT, wtfpl or whatever OSS license you like
function get_stack () {
local i message="${1:-""}"
local stack_size=${#FUNCNAME[@]}
# to avoid noise we start with 1 to skip the get_stack function
for (( i=1; i<$stack_size; i++ )); do
local func="${FUNCNAME[$i]}"
[ x$func = x ] && func=MAIN
local linen="${BASH_LINENO[$(( i - 1 ))]}"
akostadinov / wsse_xml.rb
Last active Jun 13, 2016
generate XML WSSE credentials with the simple `wsse` gem.
View wsse_xml.rb
require 'io/console'
require 'wsse'
header = WSSE::header('username', STDIN.noecho(&:gets).chomp)
def wsse_xml(user, password)
raw = WSSE::header(user, password)
# username = raw.gsub(/^.* Username="(.*)", PasswordDigest=".*(?!PasswordDigest)$/, '\\1')
digest = raw.gsub(/^.*, PasswordDigest="(.*)", Nonce=".*$/, '\\1')
nonce = raw.gsub(/^.*, Nonce="(.*)", Created=".*$/, '\\1')
View add_dup_child_ns_nokogiri_bug.rb
#!/usr/bin/env ruby
# see
require 'nokogiri'
src =<<EOD
<wrapper xmlns="ns">
<record xml:id="r1" xmlns:extra="extra">
akostadinov / add_child_ns_nokogiri_bug.rb
Last active Jun 9, 2016 — forked from gioele/add_child_ns.rb
Nokogiri `add_child` does not copy namespaces
View add_child_ns_nokogiri_bug.rb
#!/usr/bin/env ruby
# see
require 'nokogiri'
src =<<EOD
<wrapper xmlns="ns">
<record xml:id="r1" xmlns:extra="extra">
View get_cert_chain.rb
Copyright (c) 2015 Red Hat inc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
akostadinov / arch_comp.rb
Last active Aug 29, 2015
find duplicate files (WIP)
View arch_comp.rb
require 'fileutils'
# this is ugly quickly hacked script, not very nice and reliable at the moment
# it finds what files from one archive need to be backed-up (i.e. are not present in some other archive dir)
# $ find new_dir -type f > arch.txt
# $ find old_dir -type f > arch_old.txt
# `find -name ".?*" -prune -o -type f` to avoid hidden files
# cat to_arch.txt | xargs -d "\n" stat -c "%s" | awk '{size+=$1} END {print size}' # to see size of files to copy
# sudo rsync -avx --files-from=to_arch.txt ./ /path/to/store/backup
# TODO: get files fast hash (md5?) and compare based on it; avoid external tools like find
You can’t perform that action at this time.