Skip to content

Instantly share code, notes, and snippets.

View usahg's full-sized avatar

Usman Gondal usahg

View GitHub Profile
@usahg
usahg / Silos.rb
Last active May 19, 2021 08:11
Thinking through the silo models
types= ["breast", "diaper", "sleep", "memory", "solids", "pumping"]
ages= ["2 weeks", "4 weeks", "8 weeks", "12 weeks", "16 weeks", "20 weeks", "24 weeks"]
types.each do |type|
if type == "breast"
print "\n => breast section \n\n\n"
end
if type == "diaper"
print "\n\n => diaper section \n\n\n"
@usahg
usahg / clone_and_run_script_remotely.rb
Created April 2, 2013 08:22
Cloning a script and running it on remote server with ruby
# this is a perfect use case for a capistrano task
# capistrano includes ruby ssh libraries and is used for deployments
# assuming ssh access is NOT password based but key-based
require 'capistrano'
default_run_options[:pty] = true
# Application Name
set :application, "remote_script"
# Repository Settings
@usahg
usahg / ruby_upload_s3.rb
Last active December 15, 2015 16:39
Using only Ruby and not the Rails environment : how to upload a file from the local filesystem to a predetermined location in Amazon S3
#requiring official aws-sdk ruby gem
# stable and widely used : https://github.com/aws/aws-sdk-ruby
# last authored 8 hours from now
require 'aws-sdk'
s3 = AWS::S3.new
s3.config(
:access_key_id => ENV['ACCESS_KEY_ID'], #storing access keys on remote server as ENV Variables is a good practice
@usahg
usahg / json_quoter.rb
Last active December 15, 2015 02:19
quotes nested values in json
# problem :
# was trying to import a 3D model in a web page in json format for use with threejs : https://github.com/mrdoob/three.js
# 3D model was exported from maya using this threejs plugin # https://github.com/mrdoob/three.js/tree/master/utils/exporters/maya
# exported json had two problems:
# 1. all of it was in one line, i.e no indents
# 2. float values inside keys were not quoted i.e stringified.
# here's the snippet, it converts the 'bad' json to 'good' ( quoted json )
@parsed = JSON.parse(Rails.root.join("app","assets","javascripts","model.js").read)
@usahg
usahg / window_dot_open_crawler.py
Created January 30, 2012 06:16
python crawler extract javascript masked links window.open
#author : osman ehmad
import urllib2
page = urllib2.urlopen('http://www.javascript-coder.com/files/window-popup/javascript-window-open-example1.html')
source = page.read()
# SCRIPT or script will depend upon pages you want crawled
# or both can easily be incorporated
@usahg
usahg / exponential_backoff.rb
Created January 19, 2012 13:23
ruby implementation of exponential back off algorithm
# EC = 1/2*(2**exp-1)
# ^equation
def exp_backoff(upto)
result = [ ]
# ^ stores wait periods
(1..upto).each do |iter|
result << (1.0/2.0*(2.0**iter - 1.0)).ceil
@usahg
usahg / w3c_validator_with_mechanize_authentication.rb
Created January 19, 2012 07:36
validate html of a login protected page, gems used :: mechanize and w3c_validator
#dependencies :
#gem install mechanize
# ^ if this doesnt work, and you are on ubuntu, you'll have to install nokogiri
# gem install __name__ is now by default equivalent of gem install __name__ -y (-y for including all dependencies)
#gem install w3c_validtors
# I had to write this in order to validate pages against html5 compliance for our in house product
# my first repo.