Skip to content

Instantly share code, notes, and snippets.

View akshaymohite's full-sized avatar

Akshay Mohite akshaymohite

View GitHub Profile
@akshaymohite
akshaymohite / lambda-test-internet.py
Created May 28, 2021 05:17
lambda-test-internet.py
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""Secrets Manager RDS PostgreSQL Handler
@akshaymohite
akshaymohite / jquery-addClass.js
Created October 15, 2018 19:30
jquery-addClass.js
jQuery.fn.extend( {
addClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
} );
}
@akshaymohite
akshaymohite / luhn-algorithm-to-validate-card-number.rb
Last active October 10, 2019 22:14
Luhn's Algorithm to validate card numbers
number = "314143525252"
sum = 0
number.reverse.split("").each_slice(2) do |x,y|
sum += x.to_i + (2*y.to_i).divmod(10).sum
end
p sum%10 == 0 ? 'valid card number' : 'invalid card number'
@akshaymohite
akshaymohite / active-job-enqueued-with-patch.rb
Created September 26, 2018 12:11
active-job-enqueued-with-patch
module ActiveJobTestHelper
def prepare_args_for_assertion(args)
args.dup.tap do |arguments|
arguments[:at] = arguments[:at].to_f if arguments[:at]
end
end
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
expected = { job: job, args: args, at: at, queue: queue }.compact
expected_args = prepare_args_for_assertion(expected)
@akshaymohite
akshaymohite / solution-to-pdfkit-libssl-issue
Last active July 12, 2018 07:58
solution-to-pdfkit-libssl-issue
# You may or may not require this step
sudo apt install openssl build-essential xorg libssl-dev
sudo apt remove libssl-dev
sudo apt autoremove
sudo apt install libssl-dev=1.0.2g-1ubuntu4.12
sudo apt-cache policy libssl-dev
@akshaymohite
akshaymohite / backport_to_pg10.rb
Created May 7, 2018 08:07
backport_to_pg10.rb
# frozen_string_literal: true
require "active_record/connection_adapters/postgresql/schema_statements"
#
# Monkey-patch the refused Rails 4.2 patch at https://github.com/rails/rails/pull/31330
#
# Updates sequence logic to support PostgreSQL 10.
#
module ActiveRecord
@akshaymohite
akshaymohite / delegate_to.rb
Created February 7, 2018 12:42
delegate_to.rb
2.4.0 :031 > class A
2.4.0 :032?> def initialize
2.4.0 :033?> end
2.4.0 :034?> def a_method
2.4.0 :035?> puts self.class.name
2.4.0 :036?> end
2.4.0 :037?> end
:a_method
2.4.0 :038 > class B
2.4.0 :039?> def initialize
@akshaymohite
akshaymohite / bulk_creator.rb
Created January 15, 2018 09:45
Concern to help inserting records in Bulk in Rails
module BulkCreator
extend ActiveSupport::Concern
module ClassMethods
def bulk_create(columns, values, *args)
records_to_create(values, args).each_slice(500) do |records|
self.connection.execute bulk_insert_sql(columns, records)
end
end
@akshaymohite
akshaymohite / comparison.rb
Last active November 14, 2017 07:41
comparison.rb
params = {a: 4, b: 5, b: 4, to: 'akshay'}
mandatory_keys = [:from, :to, :caller_id, :call_type]
keys = params.keys
Benchmark.ips do |benchmark|
benchmark.report('new') { mandatory_keys.any?{|key| keys.include?(key)} }
benchmark.report('old') { mandatory_keys.all?{|key| params.keys.include?(key)} }
benchmark.compare!
end
@akshaymohite
akshaymohite / faker-vs-raw-random-approach.rb
Created October 26, 2017 12:11
faker-vs-raw-random-approach
2.4.0 :029 > Benchmark.ips do |benchmark|
2.4.0 :030 > benchmark.report('faker approach') { Faker::Number.number(6) }
2.4.0 :031?> benchmark.report('raw random approach') { rand(100000..999999).to_s }
2.4.0 :032?> end
Warming up --------------------------------------
faker approach 28.347k i/100ms
raw random approach 159.529k i/100ms
Calculating -------------------------------------
faker approach 315.433k (± 4.0%) i/s - 1.587M in 5.040671s
raw random approach 2.981M (± 7.0%) i/s - 14.836M in 5.002986s