Skip to content

Instantly share code, notes, and snippets.

View ryochin's full-sized avatar
🏠
Working at home

Ryo Okamoto ryochin

🏠
Working at home
View GitHub Profile
@ryochin
ryochin / AuthenticityToken.tsx
Created October 15, 2023 02:26
Rails: CSRF token on React
import React from 'react'
import { csrfToken } from '@rails/ujs'
export const AuthenticityToken = (): React.ReactElement =>
<input type="hidden" name="authenticity_token" value={csrfToken() ?? ''} />
@ryochin
ryochin / lock_tables.ex
Last active June 27, 2023 07:15
Elixir: lock tables
@doc """
テーブルをロックしコードを実行する
### Example
```
iex> MyProject.Repo.lock_tables fn -> IO.puts "hello" end, users: :read
```
"""
@spec lock_tables(fun, keyword(atom)) :: no_return
@ryochin
ryochin / bit_at.ex
Last active June 19, 2023 02:25
Elixir: 与えられた数値の指定されたビットの値を読み出す
import Bitwise
@doc """
与えられた数値の指定されたビットの値を読み出す
### Example
```
iex> bit_at(42, 3)
true
@ryochin
ryochin / Gemfile
Last active June 1, 2023 23:32
ruby: initial script template
# frozen_string_literal: true
source "https://rubygems.org"
gem 'activesupport'

ESRGAN

python3.8 -m venv esrgan
cd esrgan
source bin/activate.fish
pip install torch torchvision
python -m pip install --upgrade pip setuptools
pip install realesrgan
class Main
require 'aws-sdk-appconfigdata'
def initialize(application:, environment:, profile:)
@application = application
@environment = environment
@profile = profile
@client = Aws::AppConfigData::Client.new(
region: 'ap-northeast-1',
@ryochin
ryochin / new-rails-env.sh
Last active November 12, 2024 09:59
Rails new env creation
#!/bin/sh
PROJECT="rails8"
set -e
rm -rf .bundle .devcontainer .gitignore .github .kamal .rubocop.yml Gemfile* README.md Rakefile app bin compose.yml config* db lib log public script storage test tmp *.js* *.dev *.lock
rm -rf .dockerignore .gitattributes .node-version .tool-versions Dockerfile
rm -rf .git
rm -f Gemfile*
@ryochin
ryochin / check_tmbackup.rb
Created August 1, 2022 03:54
macOS: Simple Time Machine Backup Checker conformed to nagios/sensu/mackerel plugin format.
#!/usr/bin/env ruby
#
# you need add 'full disk access' privilege to /opt/homebrew/opt/mackerel-agent/bin/mackerel-agent.
class Main
require 'timeout'
CRITERIA = 3
def run
@ryochin
ryochin / rails_mysql_decrypt.rb
Created July 22, 2022 09:28
Rails: AES decryption using MySQL
query = 'select AES_DECRYPT(FROM_BASE64(:encrypted), :key) as plain'
sql = ActiveRecord::Base.sanitize_sql_array([
query, encrypted: '+mACVel1fgUZIc2Dd+CiFw==', key: 'secret'
])
if (result = ActiveRecord::Base.connection.select_all(sql)).present?
result.to_a.first.dig('plain')
end
@ryochin
ryochin / serializeArray.ts
Created July 16, 2022 01:39
TypeScript version of jQuery's serializeArray()
// see also: https://barker.codes/blog/serialize-form-data-into-an-array-in-vanilla-js/
// NOTE: type FormDataEntryValue = File | string
type SerializeArray = { [x: string]: FormDataEntryValue }
const serializeArray = (form: HTMLFormElement): SerializeArray => {
const result: SerializeArray = <SerializeArray>{}
const formData: FormData = new FormData(form)