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 / Image.tsx
Last active February 27, 2022 15:21
Next.js Cloudinary Image
import NextImage from 'next/image'
import { useCallback } from 'react'
import compact from 'lodash/compact'
const CLOUDINARY_HOST = 'https://some-domain.mo.cloudinary.net'
type ImageLoaderProps = {
src: string
width: number
quality?: number
@TheRusskiy
TheRusskiy / migrateDatabase.ts
Last active October 31, 2021 17:15
Typeorm - migrations with advisory lock
import { migrateDatabase } from './typeormMigrationUtills'
;(async () => {
await migrateDatabase()
})()
@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)))
@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 / 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 / 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 / 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 / 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 / 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 / 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"