Skip to content

Instantly share code, notes, and snippets.

View tonkla's full-sized avatar
🏠
Working from home

Surakarn Samkaew tonkla

🏠
Working from home
View GitHub Profile
@tonkla
tonkla / drupal_to_rails_db_migration.thor
Created November 1, 2011 16:32
Thor script is used to migrate blog posts from Drupal to Rails
#! /usr/bin/env thor
require 'mysql2'
class Tonkla < Thor
desc 'migrate', 'migrate tonkla.com\'s blog posts from Drupal to Rails'
def migrate
db_from = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'tonkla_com')
db_to = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'klass_blog_development')
result = db_from.query('SELECT r.title, r.body, n.created AS created_at, r.timestamp AS updated_at
@tonkla
tonkla / blogelf.thor
Created March 15, 2012 15:01
Blog migration script. Migrate from MySQL to Markdown text file (Jekyll).
require 'rubygems'
require 'erb'
require 'mysql2'
require 'thor'
class Blogelf < Thor
desc 'migrate', 'Migrate from MySQL to Markdown text file (Jekyll)'
def migrate
Dir.mkdir('_posts') unless Dir.exist?('_posts')
db = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'blogelf')
@tonkla
tonkla / round.go
Last active January 28, 2017 13:10
Go's math.Round(num float64, precision int)
func Round(num float64, precision uint) float64 {
pow := math.Pow(10, float64(precision))
n := int((num * pow) + math.Copysign(0.5, num))
return float64(n) / pow
}

Keybase proof

I hereby claim:

  • I am tonkla on github.
  • I am tonkla (https://keybase.io/tonkla) on keybase.
  • I have a public key ASDrxFwTdCyuuJHtwdhv4JQXe6Nfmv79AU9wnjHidGf-AQo

To claim this, I am signing this object:

@tonkla
tonkla / whois
Created March 13, 2017 05:38
Batch WHOIS lookup
#!/bin/bash
if [ "$#" == "0" ]; then
echo "You need to supply at least one argument!"
exit 1
fi
DOMAINS=('.com' '.net' '.org')
ELEMENTS=${#DOMAINS[@]}
@tonkla
tonkla / httpSrc.directive.ts
Created September 2, 2017 04:07
httpSrc.directive.ts
import { Directive, ElementRef, Input, OnInit } from '@angular/core';
import { Http, Headers, RequestOptionsArgs, ResponseContentType } from '@angular/http';
import { AuthenticationService } from '../services';
@Directive({ selector: '[httpSrc]' })
export class HttpSrcDirective implements OnInit {
@Input('httpSrc') url: string;
constructor(private el: ElementRef, private http: Http, private authService: AuthenticationService) {}
@tonkla
tonkla / Dockerfile
Last active December 21, 2017 09:58
Dockerfile of Ubuntu 16.04 + Ruby 2.3 + PostgreSQL 10
FROM ubuntu:16.04
# Install required libraries
RUN apt-get update &&\
apt-get upgrade -y &&\
apt-get install -y --no-install-recommends \
build-essential python-software-properties wget autoconf git-core curl \
zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt1-dev \
libcurl4-openssl-dev libffi-dev
@tonkla
tonkla / convertObjectKeysCase.js
Last active March 5, 2018 08:56
ES6 module to recursively convert between `snake_case` and `camelCase` keys in an object using `lodash`.
// Credit: https://gist.github.com/felixjung/a00879103892af44524f
// Credit: https://gist.github.com/emcmanus/eb735299788c820b4eb85c38f02598e4
import {
camelCase,
cloneDeep,
isArray,
isPlainObject,
map,
mapKeys,
@tonkla
tonkla / upload.js
Last active August 28, 2023 12:35
Parsing 'multipart/form-data' Excel (.xlsx) with Busboy on AWS Lambda (Node.js)
'use strict'
const Busboy = require('busboy')
const XLSX = require('xlsx')
function parseMultipartFormData(input, contentType) {
return new Promise((resolve, reject) => {
const buffers = []
const busboy = new Busboy({
headers: { 'content-type': contentType },
@tonkla
tonkla / store.js
Last active July 20, 2019 05:53
React app's global state management with the Context API and Hooks
import { createContext, useContext, useCallback, useState } from 'react'
const AppContext = createContext({})
const useAppContext = () => {
return useContext(AppContext)
}
const useAppState = () => {
const initialState = { count: 0 }