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 / UseKeyPress.ts
Last active September 4, 2024 04:28
TS: enhanced UseKeyPress React Hook
// https://novajs.co/react-hook-key-press
import { useState, useEffect } from 'react'
type KeyConfig = {
key: string
ctrl?: boolean
alt?: boolean
shift?: boolean
meta?: boolean
@ryochin
ryochin / ContentLength.ex
Created July 2, 2024 08:14
Elixir: Tesla Middleware of adding content-length header field
defmodule Tesla.Middleware.ContentLength do
@behaviour Tesla.Middleware
@impl Tesla.Middleware
def call(env, next, _options) do
env
|> Tesla.put_header(
"content-length",
(env.body || "") |> String.length() |> Integer.to_string()
)
@ryochin
ryochin / save_full_screenshot.rb
Created June 20, 2024 02:23
Rails: Rspec: save full screenshot on feature tests
def save_full_screenshot(page, path)
width = page.execute_script('return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);')
height = page.execute_script('return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);')
window = page.driver.browser.manage.window
window.resize_to width + 100, height + 100
page.save_screenshot path
end
@ryochin
ryochin / datetime_to_str.ex
Last active July 2, 2024 08:15
Elixir: 日時のタプルをメール日時形式でフォーマットする
@doc """
日時のタプルをメール日時形式でフォーマットする
### Example
```
iex> {{2021, 4, 3}, {9, 46, 58}} |> datetime_to_str
"Sat, 03 Apr 2021 09:46:58 +0900"
```
"""
@ryochin
ryochin / openssl3.2.spec
Last active December 26, 2023 05:47
OpenSSL 3 that installed to /usr/local/openssl-3.xx.xx without docs for CentOS 7
Name: openssl3.2
Version: 3.2.0
Release: 1%{?dist}
Summary: Utilities from the general purpose cryptography library with TLS implementation
License: Apache License v2.0
URL: https://www.openssl.org/
Source0: https://www.openssl.org/source/openssl-%{version}.tar.gz
BuildRequires: perl-IPC-Cmd
@ryochin
ryochin / range.rs
Created December 22, 2023 01:44
TypeScript: range()
const range = (begin: number, end: number) => ([...Array(end - begin)].map((_, i) => (begin + i)))
@ryochin
ryochin / create_zip.ex
Created December 20, 2023 04:31
Elixir: Create a ZIP archive from list of files
defmodule Zip do
@moduledoc """
Easy ZIP creator
you need to install zstream:
```
{:zstream, "~> 0.6"}
```
"""
@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