Skip to content

Instantly share code, notes, and snippets.

View akagr's full-sized avatar

Akash Agrawal akagr

View GitHub Profile
@akagr
akagr / optparse_example.rb
Created May 20, 2013 17:06
The best example of Optparse available on Internet Taken from official ruby docs.
require 'optparse'
require 'optparse/time'
require 'ostruct'
require 'pp'
class OptparseExample
CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
@akagr
akagr / cache_expiry_extesion_for_apache
Last active February 17, 2017 07:08
Serve gzipped version of rails assets from apache server and set their expiry headers to 1 year
# The Expires* directives requires the Apache module `mod_expires` to be enabled.
<Location /assets/>
# Use of ETag is discouraged when Last-Modified is present
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Location>
@akagr
akagr / closures.js
Created February 20, 2014 12:44
Borrowed from john resig's tutorial. A very nice example to test one's grasp on closures.
var a = 5;
function runMe(a){
assert( a == ___, "Check the value of a." );
function innerRun(){
assert( b == ___, "Check the value of b." );
assert( c == ___, "Check the value of c." );
}
var b = 7;
@akagr
akagr / file_commands.sh
Last active August 29, 2015 14:03
File commands
# Find all files less than 2kb
find /path/to/directory -type f -size -2k
# Find all text files less than 2kb
find /path/to/directory -type f -name '*.txt' -size -2k
# Find all files with given pattern and count them
find -type f -name '*.jpg' | wc -l
find -type f -name 'e_*' | wc -l
@akagr
akagr / poem
Created October 18, 2014 07:02
PROGRAMMER'S NIGHT BEFORE CHRISTMAS
'Twas the night before implementation and all through the house,
not a program was working not even a browse.
The programmers hung by their tubes in despair,
with hopes that a miracle would soon be there.
The users were nestled all snug in their beds,
while visions of inquiries danced in their heads.
When out in the machine room there arose such a clatter,
I sprang from my desk to see what was the matter.
openssl genrsa -des3 -out config/topbox-ca-key.pem 1024
openssl req -new -key config/topbox-ca-key.pem -out config/topbox-ca.pem
openssl x509 -req -days 365 -in config/topbox-ca.pem -out config/topbox-ca-csr.pem -signkey config/topbox-ca-key.pem
openssl genrsa -des3 -out config/topbox-key.pem 1024
openssl req -new -key config/topbox-key.pem -out config/topbox-csr.pem
cp config/topbox-key.pem config/server.key.org
openssl rsa -in config/server.key.org -out config/topbox-key.pem
rm config/server.key.org
@akagr
akagr / squareup.sh
Last active December 19, 2018 18:17
Squares up rectangular images by filling empty space with blur of same image
# To get the resolution, multiply 118 by cm. For example, 7 cm image at 300ppi will be 7 * 118 = 826 px
RESOLUTION=826
convert $1 -resize ${RESOLUTION}x${RESOLUTION}^ -gravity center -crop ${RESOLUTION}x${RESOLUTION}+0+0 +repage -blur 0x8 temp.jpg
convert temp.jpg $1 -resize ${RESOLUTION}x -gravity center -composite -matte "./output/$1-out.jpg"
@akagr
akagr / keybase.md
Created April 1, 2020 17:19
keybase.md

Keybase proof

I hereby claim:

  • I am akagr on github.
  • I am akashagrawal (https://keybase.io/akashagrawal) on keybase.
  • I have a public key ASB2jpNzAa37164ZMfrGb0Te3fV4rcXowwYbpKIG-AeTwwo

To claim this, I am signing this object:

# Just a copy of https://stackoverflow.com/a/30724359/1825727
def from_hashstring(value)
json_string = value
.gsub(/([{,]\s*):([^>\s]+)\s*=>/, '\1"\2"=>') # Handle ruby symbols as keys
.gsub(/([{,]\s*)([0-9]+\.?[0-9]*)\s*=>/, '\1"\2"=>') # Handle numbers as keys
.gsub(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>\s*:([^,}\s]+\s*)/, '\1\2=>"\3"') # Handle symbols as values
.gsub(/([\[,]\s*):([^,\]\s]+)/, '\1"\2"') # Handle symbols in arrays
.gsub(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>/, '\1\2:') # Finally, convert => to :
JSON.parse(json_string)
@akagr
akagr / em.scpt
Last active November 17, 2023 10:34
MacOS launch helper to start emacs daemon or attach to one
-- ███████╗███╗░░░███╗░█████╗░░█████╗░░██████╗
-- ██╔════╝████╗░████║██╔══██╗██╔══██╗██╔════╝
-- █████╗░░██╔████╔██║███████║██║░░╚═╝╚█████╗░
-- ██╔══╝░░██║╚██╔╝██║██╔══██║██║░░██╗░╚═══██╗
-- ███████╗██║░╚═╝░██║██║░░██║╚█████╔╝██████╔╝
-- ╚══════╝╚═╝░░░░░╚═╝╚═╝░░╚═╝░╚════╝░╚═════╝░
--
-- Open this script with 'Script Editor' on MacOS, then save it
-- inside /Applications as an 'Application', not 'Script'.