Skip to content

Instantly share code, notes, and snippets.

@momo-lab
momo-lab / gist:2642521
Created May 9, 2012 06:49
Luhnアルゴリズムの実装サンプル
module Luhn
CODE_0 = "0".ord
CODE_9 = "9".ord
CODE_A = "A".ord
CODE_Z = "Z".ord
def self.add(data)
return data + get_char(data)
end
@momo-lab
momo-lab / open_uri.rb
Created May 9, 2012 07:00
HTTP_PROXY環境変数にProxy認証用のUser/Passの定義がある場合はそれを使用するようにする。(要open-uri)
def open_uri(uri, options = {}, &block)
uri = URI.parse(uri) until URI === uri
proxy = uri.find_proxy
unless proxy.nil?
if proxy.user.nil?
options[:proxy] = proxy
else
options[:proxy_http_basic_authentication] = [proxy, proxy.user, proxy.password]
end
end
@momo-lab
momo-lab / support_http_proxy_auth.diff
Created May 10, 2012 06:30
gistコマンドでProxy認証を使えるようにする差分。SSLのverifyがうまく動かなかったのでNONEにしてしまったので、なんとなくforkするのがはばかられる
diff --git a/lib/gist.rb b/lib/gist.rb
index d0be256..2274d7e 100644
--- a/lib/gist.rb
+++ b/lib/gist.rb
@@ -33,11 +33,17 @@ module Gist
PROXY = URI(ENV['HTTPS_PROXY'])
elsif ENV['HTTP_PROXY']
PROXY = URI(ENV['HTTP_PROXY'])
+ elsif ENV['http_proxy']
+ PROXY = URI(ENV['http_proxy'])
var util = require('util');
var url = require('url');
var http = require('http');
function download(u) {
u = url.parse(u)
var client = http.createClient(u.port || 80, u.hostname);
var req = client.request('GET', u.pathname, {host: u.hostname});
req.end();
req.on('response', function(res) {
@momo-lab
momo-lab / optparse.sh
Created May 15, 2012 10:31
オプションの説明文(ヘルプ)内容から、オプションの解析を行うbash用スクリプト
#!/bin/bash
# コマンドの説明(ヘルプ)の文章をそれなりに解釈して、
# 引数のオプション解析を行うbash用関数
#
# 詳しくは後ろの方にある使用例を参照のこと。
function optparse() {
if [[ ! $optparse_prefix ]]; then
optparse_prefix="opt_"
fi
@momo-lab
momo-lab / cr.bat
Created May 16, 2012 08:29
Eclipseのプロジェクト/Subversion/Gitのルートに移動するWindows用バッチファイル
@ECHO OFF
REM プロジェクトのルートディレクトリに移動する。
REM PUSHDしてあるので、元のディレクトリに戻りたい場合はPOPDすればよい。
REM
REM ルートディレクトリの定義(上位ほど優先順位高)
REM .projectファイルがある(Eclipseのプロジェクトルート)
REM .gitディレクトリがある(gitのルート)
REM .svnディレクトリがあるtrunkディレクトリ(svnのルート)
PUSHD .
@momo-lab
momo-lab / clear.rb
Created May 29, 2012 01:05
コンソールをクリアするだけのearthquake.gem用プラグイン
# -*- coding: utf-8 -*-
# earthquake.gem plugin
# screen cleaer command
Earthquake.init do
command :clear do
system "clear"
end
help :clear, "clear screen"
end
@momo-lab
momo-lab / deep_expand_url.rb
Created May 29, 2012 05:37
短縮URLを展開するearthquake.gem用プラグイン。…短縮じゃないやつでもひたすら展開するけど。
# -*- coding: utf-8 -*-
# earthquake.gem plugin
# expand url
require 'open-uri'
require 'uri'
require 'net/http'
def get_location(url)
begin
url = URI.parse(url.to_s)
@momo-lab
momo-lab / marker.rb
Created May 29, 2012 11:13
「ここまで読んだ」マークを付けるearthquake.gem用プラグイン
# -*- coding: utf-8 -*-
# earthquake.gem plugin
# insert maker
Earthquake.init do
cfg = config[:marker] || {}
command :marker do
input(":marker #{cfg[:comment]}")
end
@momo-lab
momo-lab / pocket.rb
Created May 31, 2012 14:35
Pocket(旧 ReadItLater)にツイート内のURLを追加するearthquake.gem用プラグイン
# -*- coding: utf-8 -*-
# earthquake.gem plugin
# add url to Pocket(http://getpocket.com)
require 'uri'
require 'open-uri'
Earthquake.init do
cfg = config[:pocket] || {}
command :pocket do |m|
pocket_uri = URI.parse('https://readitlaterlist.com/v2/add')