Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kenjij's full-sized avatar
👨‍💻
Hacking away...

Ken J. kenjij

👨‍💻
Hacking away...
View GitHub Profile
@kenjij
kenjij / neko-logger.rb
Last active October 17, 2022 18:26
Generic Ruby Logger Wrapper
# Configure logger
NekoLogger.logger = Logger.new($stdout, formatter: proc { |s, d, n, m| "#{s} : #{m}\n" })
L = NekoLogger.logger
lvl = ENV['NEKO_LOG_LEVEL']
if String === lvl && ['DEBUG', 'INFO', 'WARN', 'ERROR'].include?(lvl.upcase)
L.level = eval("Logger::#{lvl.upcase}")
end
#
@kenjij
kenjij / config
Created February 18, 2022 02:15
User SSH config
# Save this at ~/.ssh/config
Host nickname
Hostname 192.168.0.123
User nick
Port 2222
IdentityFile ~/.ssh/id_nick
Compression yes
Host *
Compression no
@kenjij
kenjij / exceptions.rb
Created January 29, 2022 00:21
Exception handling in Ruby
begin
# do something bad
rescue => e # StandardError by default
puts "Exception class: #{ e.class.name }"
puts "Exception message: #{ e.message }"
puts "Exception backtrace: #{ e.backtrace }"
# Or just: puts "#{e}"
end
@kenjij
kenjij / ddb_batch_put.rb
Created October 5, 2021 00:19
AWS DynamoDB batch write (put) in Ruby, ideally use in Lambda
require 'aws-sdk-dynamodb'
# DynamoDB batch write (put) routine
# @param items [Array<Hash>] DynamoDB items to put
# @param table [String] DynamoDB table name
def ddb_batch_put(items, table)
ddb = Aws::DynamoDB::Client.new
batch = items.map { |item| {put_request: {item: item}} if Hash === item }
batch.compact!
puts "Batch write to: #{table}, #{batch.length} items."
@kenjij
kenjij / networking.sh
Created July 15, 2021 19:36
Ubuntu 20.04 Focal Networking
# Check IP address
ip a
# Check default gateway
ip r
route -n
# Check DNS server
systemd-resolve --status
@kenjij
kenjij / twilio.rb
Created April 28, 2020 18:53
[Twilio] Validate Signature of Request (in pure Ruby)
require 'base64'
require 'openssl'
# See: https://www.twilio.com/docs/usage/security#validating-requests
#
# Example of AWS API Gateway (HTTP) + Lambda
def is_twilio_request_valid?(event)
# Create a string that is your URL with the full query string
url = "https://#{event['headers']['host']}#{event['rawPath']}"
# Sort the list of POST variables by the parameter name
@kenjij
kenjij / README.md
Created April 11, 2020 02:35
macOS Terminal Setup

Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install Oh My ZSH!

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
@kenjij
kenjij / GET.json
Created February 7, 2020 07:48
Sample: AWS Lambda function event contents received from API Gateway (HTTP API; route: "/event")
{
"version":2,
"path":"/event",
"httpMethod":"GET",
"headers":{
"Content-Length":"0",
"Host":"abcde12345.execute-api.us-west-2.amazonaws.com",
"User-Agent":"curl/7.54.0",
"X-Amzn-Trace-Id":"Root=1-67891233-abcdef012345678912345678",
"X-Forwarded-For":"192.168.1.23",
@kenjij
kenjij / Makefile
Created February 7, 2020 06:00
Build and Package RubyGems for AWS Lambda Layer Use (example)
#
# Build and Package RubyGems for AWS Lambda Layer Use
#
all: build package
build:
gem i firebase-ruby -Ni ruby/gems/2.5.0
gem i darksky-ruby -Ni ruby/gems/2.5.0
ls -m ruby/gems/2.5.0/gems
@kenjij
kenjij / readme.md
Last active January 29, 2019 17:22
Swap Control and Caps Lock on Windows

Edit Windows Registry

  1. Open Registry Editor
  2. Go to: Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout
  3. Edit, or add, binary value key: Scancode Map (see below)
  4. Restart to take effect

Map Value