Skip to content

Instantly share code, notes, and snippets.

View chsh's full-sized avatar
👍
Love your code.

CHIKURA Shinsaku chsh

👍
Love your code.
View GitHub Profile
@chsh
chsh / periodical_token.rb
Last active May 12, 2016 23:29
Generate periodically unique token.
require 'digest/sha2'
# usage:
# PeriodicalToken.generate(:min) # -> returns unique token in a minute.
#
class PeriodicalToken
SALT = 'RANDOMLLY-GENERATED-STRING'
RANGE_TO_DIV = {
min: 60,
min5: 60 * 5,
@chsh
chsh / Dockerfile
Created April 26, 2016 01:44
Dockerfile for Rails
FROM ruby:2.3.0
# see update.sh for why all "apt-get install"s have to stay as one long line
RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
# see http://guides.rubyonrails.org/command_line.html#rails-dbconsole
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
ENV RAILS_VERSION 5.0.0.beta3
RUN gem install rails --version "$RAILS_VERSION"
@chsh
chsh / webloc2webarchive.rb
Last active March 14, 2021 01:35
Save webarchive using webloc file.
#!/usr/bin/env ruby
require 'tmpdir'
require 'shellwords'
webloc_file = ARGV[0]
webloc_ext = File.extname(webloc_file)
webarchive_file = webloc_file.gsub(/#{webloc_ext}$/, '.webarchive')
url = nil
@chsh
chsh / hash_pathable_tweak.rb
Created December 19, 2015 11:04
Extract Hash#path method from ruby-tweaks gem.
# GenericUtils
module GenericSupport
module Pathable
module ClassMethods
def path(hash, *pathes)
target = hash
pathes.map! do |p|
p.to_s.split(/[\/\.]+/)
end
pathes.flatten.each do |element|
@chsh
chsh / nop.rb
Created August 30, 2015 04:51
Disable method execution by overriding "nothing to do" method.
module NOP
def self.nop(*args)
opts = args.extract_options!
methods = args
raise ArgumentError if methods.size < 2
klass = methods.shift
raise ArgumentError unless klass.is_a?(Class)
do_backup, backup_prefix, backup_suffix = extract_backup_options opts[:backup]
methods.map(&:to_sym).each do |method|
next unless klass.instance_methods.include? method
@chsh
chsh / eb_env.rake
Created August 16, 2015 04:12
Rake tasks to fetch/push aws elasticbeanstalk env vars on environment.
namespace :eb do
namespace :env do
desc "Fetch env vars from eb"
task :fetch, [:envname] do |t, params|
lines = `eb printenv #{params[:envname]}`.strip.split(/\r?\n/)
lines.shift if lines[0] =~ /Environment Variables:/
hash = {}
lines.sort.each do |line|
if line =~ /^\s+(.+)\s+=\s+(.+)$/
hash[$1] = $2
@chsh
chsh / get_routes.rb
Created July 24, 2015 15:55
Get Rails routes list from own application.
# note: This code does not point correct paths on "initializer".
# Use after all initialization process are completed.
#
class GetRoutePaths
def self.list
@@list ||= Rails.application.routes.routes.map { |r| r.path.spec.to_s }.uniq
end
end
@chsh
chsh / aws-eb-icon.png
Last active August 29, 2015 14:24
aws-eb-icon
aws-eb-icon.png
@chsh
chsh / queue_handler_controller.rb
Created June 15, 2015 04:14
Accept SQS message for Rails controller.
class QueueHandlerController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :verify_worker_tier
def receive
ActiveJob::Base.execute params
render text: ''
end
private
def verify_worker_tier
@chsh
chsh / webapp.conf
Created May 31, 2015 07:21
AWS Elastic Beanstalk nginx webapp.conf for ruby(rails).
upstream my_app {
server unix:///var/run/puma/my_app.sock;
}
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
location / {
proxy_pass http://my_app; # match the name of upstream directive which is defined above