新しく作ろうとしているプロジェクトで使うライブラリがあって、しかも2つのプロジェクトで共通に使うつもりなので、こういう場合は gem にしてしまうのがいい。filestorage っていう一般名詞みたいな名前のライブラリだけど gem search してみたら見つからなかったので、いいや、作ってしまえ。
というわけで、ほんとにシンプルなものだけど gem を作って rubygems.org に登録してみた。以下手順のメモ。
Bundlerでプロジェクトを作る
^o^ > bundle gem filestorage
DL is deprecated, please use Fiddle
create filestorage/Gemfile
create filestorage/Rakefile
create filestorage/LICENSE.txt
create filestorage/README.md
create filestorage/.gitignore
create filestorage/filestorage.gemspec
create filestorage/lib/filestorage.rb
create filestorage/lib/filestorage/version.rb
Initializing git repo in C:/Users/hiro/Documents/tmp/filestorage
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in Gemfile.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in LICENSE.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in Rakefile.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in filestorage.gemspec.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in lib/filestorage.rb.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in lib/filestorage/version.rb.
The file will have its original line endings in your working directory.
これで gem の雛形ができる。git リポジトリもできていて、ファイルがステージされた状態になっている。まずはここでいったん commit。ライブラリの本体(lib/filestorage/local_filestorage.rb)を書いて、filestorage.gemspec を編集してもう一度 commit。GitHub にリポジトリを作って push しておく。
これでひとまずは出来上がり(まあ、バージョン 0.0.1 だからね)。
rubygems.orgからAPI Keyをとってくる
rubygems.org に gem を登録するには、API Key が必要。サインアップしてあったので(いつやったんだっけ?)、プロファイルの編集ページで API Key を見ることができる。これを %HOME%/.gems/credentials ファイルに保存するんだけど、ワンライナーがページに載っているのでそれを真似して保存した。ただし、載っているのは Unix 向けなので、Windows ではそのまま使えるわけじゃない。とはいってもやってることは簡単で、次のようにすればいい。
^o^ > curl -u takatoh https://rubygems.org/api/v1/api_key.yaml > %HOME%/.gem/credentials
gemをつくって登録する
もう一息だ。まずは gem を作る。
^o^ > bundle exec rake build DL is deprecated, please use Fiddle DL is deprecated, please use Fiddle filestorage 0.0.1 built to pkg/filestorage-0.0.1.gem.
これで pkg ディレクトリの中に filestorage-0.0.1.gem ができた。これを rubygems.org に登録する。
^o^ > bundle exec rake release DL is deprecated, please use Fiddle DL is deprecated, please use Fiddle filestorage 0.0.1 built to pkg/filestorage-0.0.1.gem. Tagged v0.0.1. Pushed git commits and tags. Pushed filestorage 0.0.1 to rubygems.org.
これだけで OK。なんか git のタグをうって GitHub に push までしてくれるみたいだな。
そんでもって、これが登録後のページ。おお、ちゃんと登録されてる!
追記
たいしたもんじゃないけど、書いたコードをさらしておく。
# encoding: utf-8
require 'pathname'
require 'fileutils'
module Filestorage
class LocalFilestorage
def initialize(base_dir)
@base_dir = Pathname.new(base_dir)
end
def store(path, file)
fullpath = @base_dir + path
FileUtils.mkdir_p(fullpath.parent)
if file.instance_of?(Pathname)
FileUtils.cp(file, fullpath)
elsif file.instance_of?(File)
File.open(fullpath, "wb") do |f|
f.write(file.read)
end
else
File.open(fullpath, "wb") do |f|
f.write(file)
end
end
path
end
def get(path)
fullpath = @base_dir + path
File.open(fullpath, "rb")
end
end # of class LocalFilestorage
end # of module LocalFilestorage
なんか、1時間くらい経ってからページをみたら 18 downloads とか書いてある。もう 18 回もダウンロードされたってこと?みんながっかりしただろうな。
さらに追記
参考にしたページを書くのを忘れてた。ここを参考にしました。

「自作のgemをrubygems.orgに公開してみた」への1件のフィードバック