Skip to content

Instantly share code, notes, and snippets.

View TheRusskiy's full-sized avatar
⌨️
Living the life, one keystroke at a time

Dmitry Ishkov TheRusskiy

⌨️
Living the life, one keystroke at a time
View GitHub Profile
@TheRusskiy
TheRusskiy / list_helper_methods.rb
Created December 7, 2016 17:17
List all Rails helper methods
def get_helper_modules(subtree = [])
helper_directory = Rails.root.join('app').join('helpers')
subtree.each do |folder|
helper_directory = helper_directory.join(folder)
end
entries = Dir.entries(helper_directory) - %w(. ..)
files = entries.select { |e| e.include?('.rb') }
modules = files.map do |file|
module_name = subtree.map(&:camelcase).join('::')
if module_name.present?
@TheRusskiy
TheRusskiy / persistent_http_client.rb
Created November 25, 2020 12:49
PersistentHttpClient
class PersistentHttpClient
def self.get(url)
uri = url.is_a?(URI) ? url : URI(url)
connection_manager.get_connection(uri)
end
def self.connection_manager
Thread.current[:persistent_http_connection_manager] ||= new_manager
end
@TheRusskiy
TheRusskiy / whenever.rb
Created November 26, 2020 15:50
Convert whenver-style strings to crontab format
Whenever::Output::Cron.new(:monday, nil, '3:01 am', {}).time_in_cron_syntax
# => "1 3 * * 1"
@TheRusskiy
TheRusskiy / MapsStaticApiExample.tsx
Last active May 4, 2021 09:16
google-maps-optimizations
export default function MapsStaticApiExample({
markers,
height,
width,
center,
zoom,
}: Props) {
return (
<img
alt="Map Preview"
@TheRusskiy
TheRusskiy / text_encryptor.rb
Created May 6, 2021 04:55
Text Encryption in Ruby on Rails
class TextEncryptor
class << self
def encrypt(text)
text = text.to_s unless text.is_a? String
len = ActiveSupport::MessageEncryptor.key_len
salt = SecureRandom.hex len
key = ActiveSupport::KeyGenerator.new(secret).generate_key salt, len
crypt = ActiveSupport::MessageEncryptor.new key
encrypted_data = crypt.encrypt_and_sign text
@TheRusskiy
TheRusskiy / phone_verification.rb
Created May 7, 2021 07:34
Phone Verification
class PhoneVerification
class << self
# time that user has before a code expires
EXPIRATION = 5.minutes
# an SMS can't be sent more frequently than that
TIME_BEFORE_RESEND = 30.seconds
# how many times can a user enter an invalid code
MAX_ATTEMPTS = 5
@TheRusskiy
TheRusskiy / example-page.tsx
Last active May 10, 2021 08:35
Restrict Next.js page to authenticated users
const ProfilePage: NextPage = () => {
return (
<PageLayout>
{/* PAGE BODY */}
</PageLayout>
)
}
// THE ACTUAL USAGE
requireAuth(ProfilePage)
@TheRusskiy
TheRusskiy / benchmark.rb
Last active May 17, 2021 13:10
Persistent HTTP Client in Ruby
require 'benchmark'
n = 100
def random_uri
URI("https://rickandmortyapi.com/api/character/#{rand(399) + 1}")
end
Benchmark.bm do |x|
x.report do
@TheRusskiy
TheRusskiy / application_mailer.rb
Last active May 25, 2021 09:28
Tracking opened emails in Postmark & Rails
class ApplicationMailer < ActionMailer::Base
def new_blog_post(blog_post, subscriber)
# by calling "store_message" we are saying that this
# emails need to be saved in our database
# for further tracking
store_message(
email_name: 'new_blog_post',
entity: blog_post,
user: subscriber
)
@TheRusskiy
TheRusskiy / remove_watch_later.js
Created July 3, 2021 20:00
Remove "Watch Later" videos from youtube
buttons=[]
document.querySelectorAll('ytd-playlist-video-renderer ytd-menu-renderer #button.style-scope.yt-icon-button').forEach(b => buttons.push(b))
while(bs.length > 0) {
let button = buttons.pop()
button.click()
await (new Promise(resolve => setTimeout(resolve, 500)))
let dropdownItems = [];
document.querySelectorAll('tp-yt-paper-listbox tp-yt-paper-item').forEach(n => dropdownItems.push(n));
dropdownItems.find(e => e.textContent.includes('Remove from Watch later')).click()
await (new Promise(resolve => setTimeout(resolve, 3000)))