Skip to content

Instantly share code, notes, and snippets.

View takeshy's full-sized avatar

takeshy takeshy

  • Sun Asterisk
  • Yokohama
View GitHub Profile
@takeshy
takeshy / dinamic_reverse_proxy.js
Created December 28, 2012 08:28
http-proxyを使ったWebSocket対応のreverse proxy。 proxy先のHostの情報のJSONファイルを監視していて、変更があれば読み込むようになっているため動的にproxy先のサーバを変更できる。死活監視プログラムがJSONを作成すればFailOverが実現できる。
var http = require('http')
, httpProxy = require('http-proxy')
, Cookie = require('cookie')
, fs = require('fs');
var SERVERS_FILE_NAME = "servers.json"
var PROXY_PORT = 4000
var SERVERS = null;
var g_httpProxies = {};
@takeshy
takeshy / gist:3040198
Created July 3, 2012 14:50
10枚中1枚が当たりがある中で、自分で1枚選んで、無作為に他の八枚を開いて当たりが途中で出た時は最初からやり直して、残り2枚になるまで開けた場合
#!/usr/bin/ruby
def init
(1..9).to_a.shuffle
end
success=0
failure=0
10000000.times do
@takeshy
takeshy / hash_with_value_indifferent_access_spec.rb
Created November 12, 2011 17:01
HashWithValueIndifferentAccessのspec
require 'rspec'
require File.expand_path('../../hash_with_value_indifferent_access',__FILE__)
describe HashWithValueIndifferentAccess do
context "initialize" do
before do
@hash = HashWithValueIndifferentAccess.new({:a=>:b})
end
it "symbole value compare string value " do
@hash[:a].should == "b"
@takeshy
takeshy / hash_with_value_indifferent_access.rb
Created November 12, 2011 17:00
hashの値の比較にsymbolでも文字列でも同値とする
require 'forwardable'
class HashWithValueIndifferentAccess
extend Forwardable
attr_accessor :hash
def_delegators(:@hash,*({}.public_methods - Object.new.public_methods - ["[]","[]=","values","==","to_s"]))
def initialize(hash={})
@hash={}
hash.each do|k,v|
if v.is_a?(Symbol)
@takeshy
takeshy / gist:1277528
Created October 11, 2011 08:02
encodeがわからないファイルをcp932統一する
require 'nkf'
Dir.glob("c:/howm/2011/*/*.howm"){|g|
d = File.read(g)
File.open(g,"w"){|f|
f.write(d.force_encoding(NKF.guess(d)).encode("cp932"))
}
}
@takeshy
takeshy / gist:1096367
Created July 21, 2011 02:09
url unescape for vim
function GetCode(pos,str)
let s:pos = a:pos
let s:n_code = str2nr(strpart(a:str,s:pos,2),16)
let s:pos = s:pos + 2
"in a certain sns
"if s:n_code == 37
" let s:n_code = str2nr(strpart(s:line,s:pos,2),16)
" let s:pos = s:pos + 2
"endif
return [s:pos,s:n_code]
@takeshy
takeshy / gist:895709
Created March 31, 2011 02:21
emacs scp
(defun post-ftp()
"Post current file to Server."
(interactive )
(setq nowfile (buffer-file-name))
(setq before_buffer (buffer-name))
(setq dir_pos (string-match "/[^/]*$" nowfile))
(setq dir_name (substring nowfile 0 dir_pos))
(setq remote_dir_name (substring nowfile (length "c:/") dir_pos))
(setq file_name (substring nowfile (+ 1 dir_pos) (length nowfile)))
(find-file "c:\\tmp\\scpcmd.txt")
@takeshy
takeshy / gist:895706
Created March 31, 2011 02:16
vim scp
" You should execute pagent before you call those command.
function PutScp()
let s:nowDir = substitute(expand("%:p:h"),"[cC]:\\(.*\\)$","\\1","")
let s:nowFile = expand("%:t")
let s:fn = "c:\\tmp\\scpcmd.txt"
if filereadable(s:fn)
let s:ret = delete(s:fn)
endif
@takeshy
takeshy / gist:891964
Created March 29, 2011 07:50
rackベースのoauth signatureチェック
module Rack
class SignatureCheck
def initialize(app)
@app = app
end
def call(env)
return [ 401,{ 'Content-Type' => 'text/plain','Content-Length' => '0'},[]] unless ::OauthSignature.valid?(env)
@app.call(env)
end
end
@takeshy
takeshy / gist:838095
Created February 22, 2011 02:07
OAuth Consumer Request(2-legged OAuth)
import java.net.URI
import org.apache.http._
import org.apache.http.client._
import org.apache.http.impl.client._
import org.apache.http.entity.InputStreamEntity
import org.apache.http.client.methods._
import scala.util.parsing.json.JSON._
import java.io.StringBufferInputStream
import org.apache.commons.codec.binary.Base64.encodeBase64
import javax.crypto