Rails3で自分専用のブログを作る(14) ─ Cucumber, RSpecをgenerate

Railsアプリを生成した後、CucumberやRSpecを生成していなかったので、生成する事にします。

まず、cucumber-rails の README を参照すると、Gemfileに下記の追加を行う必要があるらしい。

gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'rspec-rails'
gem 'spork'
gem 'launchy'

これを追記して bundle install を実行します。おっと、nokogiriのインストールでエラーに。libxslt関連のヘッダがないようです。

sudo apt-get install libxslt-dev

を実行してから、再び bundle install で大丈夫になりました。これで、cucumber を生成できます。

ruby script/rails generate cucumber:install --help

でオプションを確認し、次のように生成しました。

ruby script/rails generate cucumber:install --capybara  --rspec

つぎはRSpec です。Gemfileにrspec-railsが登録済なので、次のようにうってみると、

ruby script/rails generate rspec:install

rspec generatorが見つからないとのエラー。調べてみると、Rails 3に対応したRSpecは 2.0ですが、正式リリースはされていません。

gem install rspec-rails --pre

で 2.0.0.beta.20がインストールできました。ただ、このままでbundle install しても、rspecは古いままなので、Gemfileのrspec関係の記述を次のように修正しました。

gem 'rspec-rails', '2.0.0.beta.20'
gem 'rspec', '2.0.0.beta.20'

これでbundle install してから、

ruby script/rails generate rspec:install

これでOKでした。

Rails3で自分専用のブログを作る(13) ─ データベースを作成

データベースを作成するために、PostgreSQLにユーザーを登録します。

# su postgres
$ createuser DIY-Blog
新しいロールをスーパーユーザとしますか? (y/n) n
新しいロールにデータベース作成権限を与えますか? (y/n) y
新しいロールにロールを作成する権限を与えますか? (y/n) n
$ exit
$ exit

/etc/postgresql/8.4/main/pg_hba.conf を編集しました。

(略)
local   all         DIY-Blog                   trust
local   all         all                               ident
(略)

データベースを生成します。

rake db:create:all

テストしてみましょう。

rails s

http://localhost:3000/ にアクセスしてWelcome画面が表示される事を確認します。

Rails3で自分専用のブログを作る(12) ─ Railsアプリを生成

新しくRailsアプリを生成します。ディレクトリとREADMEを生成済みなので、上書きを指定する -f を指定します。また、テストにはRspecを使用するのでtestディレクトリ以下の生成を抑制する -T スイッチを指定しました。

$ cd ~
$ rails new DIY-Blog -d postgresql -f -T

gitリポジトリに登録しておきます。

$ cd DIY-Blog
$ git add -A
$ git commit -m "rails app generated"

Rails3で自分専用のブログを作る(11) ─ Cucumber と Rspec をインストール

今回の開発はBDDでやってみようと思うので、Cucumber と Rspec をインストールします。

Cucumberは、cucumber-railsを指定してインストール。

masatoshi@ubuntu:~$ gem install cucumber-rails
Building native extensions.  This could take a while...

(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)

                     (::)   U P G R A D I N G    (::)

Thank you for installing cucumber-0.8.5.
Please be sure to read http://wiki.github.com/aslakhellesoy/cucumber/upgrading
for important information about this release. Happy cuking!

(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)

Successfully installed trollop-1.16.2
Successfully installed gherkin-2.1.5
Successfully installed term-ansicolor-1.0.5
Successfully installed diff-lcs-1.1.2
Successfully installed json_pure-1.4.6
Successfully installed cucumber-0.8.5
Successfully installed cucumber-rails-0.3.2
7 gems installed
Installing ri documentation for trollop-1.16.2...
Installing ri documentation for gherkin-2.1.5...
Installing ri documentation for term-ansicolor-1.0.5...
Installing ri documentation for diff-lcs-1.1.2...
Installing ri documentation for json_pure-1.4.6...
Installing ri documentation for cucumber-0.8.5...
Installing ri documentation for cucumber-rails-0.3.2...
Installing RDoc documentation for trollop-1.16.2...
Installing RDoc documentation for gherkin-2.1.5...
Installing RDoc documentation for term-ansicolor-1.0.5...
Installing RDoc documentation for diff-lcs-1.1.2...
Installing RDoc documentation for json_pure-1.4.6...
Installing RDoc documentation for cucumber-0.8.5...
Installing RDoc documentation for cucumber-rails-0.3.2...

Rspecrspec-railsを指定してインストール。

masatoshi@ubuntu:~$ gem install rspec-rails
**************************************************

  Thank you for installing rspec-1.3.0

  Please be sure to read History.rdoc and Upgrade.rdoc
  for useful information about this release.

**************************************************
**************************************************

  Thank you for installing rspec-rails-1.3.2

  If you are upgrading, do this in each of your rails apps
  that you want to upgrade:

    $ ruby script/generate rspec

  Please be sure to read History.rdoc and Upgrade.rdoc
  for useful information about this release.

**************************************************
Successfully installed rspec-1.3.0
Successfully installed rspec-rails-1.3.2
2 gems installed
Installing ri documentation for rspec-1.3.0...
Installing ri documentation for rspec-rails-1.3.2...
Installing RDoc documentation for rspec-1.3.0...
Installing RDoc documentation for rspec-rails-1.3.2...

Rails3で自分専用のブログを作る(9)ー github.comにリポジトリを登録

今回のプログラムは公開するつもりなので、github.comにリポジトリを登録しました。
すでにアカウントとssh-keyは登録してあるので、Webから登録します。DIY Blogとしておきました。

あとの作業は Creating a new repository - GitHub Help を参考にしました。

まず、Ubuntuでユーザー名とメールアドレスを登録します。

$ git config --global user.name "Masatoshi Itagaki"
$ git config --global user.email "xxxxxxxx@xxxxx.xx"

つぎに、リポジトリ用のディレクトリを作成し、リポジトリを作成しリモートリポジトリを登録します。

$ mkdir DIY-Blog
$ cd DIY-Blog
$ git init
$ touch README
$ git add README
$ git commit -m "first commit"
$ git remote add origin git@github.com:masa-ita/DIY-Blog.git
$ git push origin master

Rails3で自分専用のブログを作る(10)ー Ruby 1.9.2 p0 にアップグレード

Ruby 1.9.2 の正式リリース(p0)があったので、開発環境をrc2からp0にアップグレードしました。

http://ruby.about.com/od/rubyversionmanager/ss/Upgrading-To-1-9-2-Using-Rvm.--qC.htm を参考にrvmを使ってアップグレードします。

(1)rvm自体のアップデート
rvmが最新のバージョン、パッチレベルの情報を持っているので、まずrvmをアップデートします。このためにはgithub.comから最新のrvm情報を取得する必要がありますので、git-coreがインストールされている必要があります。今回はインストール済ですね。

$ rvm update --head

(2)rvmのリロード
アップデートした情報を反映させるためには、rvmのシェルスクリプトのリロードが必要です。

$ rvm reload

(3)rvmが知っているrubyの確認
リロードの結果、最新版のruby情報が保持されているかどうかを確認します。

$ rvm list known

(4)ruby 1.9.2 p0 のインストール
ruby 1.9.2 p0 のソースをダウンロードしてコンパイルします。rvmのインストールコマンドを実行するだけです。

$ rvm install 1.9.2

(5)インストール済 gem のコピー
ruby 1.9.2 rc2 にインストールした gem を、1.9.2 p0 の環境にコピーします。

$ rvm gemset copy 1.9.2-rc2 1.9.2-p0

(6)デフォルトRubyの切り替え
ruby 1.9.2 p0 のインストールで、1.9.2 は p0 を指すように更新されていますが、rvmコマンドで明示的に切り替えないと、rc2 のままです。手動でデフォルトのRubyを1.9.2 p0 に切り替えます。

$ rvm use 1.9.2 --default

(7)コピーしたgemの確認
インストール済のgemを確認しておきましょう。

$ gem list

O'ReillyのiPhone AppからEPUBファイルを抽出するRubyスクリプト【Windows版】

404 Blog Not Found:perl - O'ReillyのiPhoneアプリ本からepubをぶっこぬくを参考に、Ruby版を作ってみるテストのWindows版です。

Windowsにはzip/unzipコマンドがないので、ziprubyライブラリを使ってみました。

require "fileutils"
require "zipruby"

def clean
  FileUtils.rmtree('Payload')
end

def die(message)
  puts message
  exit(1)
end

def zip_add_dir_r(zipfile, dir)
  Zip::Archive.open(zipfile) do |ar|
    ar.add_dir(dir)

    Dir.glob("#{dir}/**/*").each do |path|
      if File.directory?(path)
        ar.add_dir(path)
      else
        ar.add_file(path, path)
      end
    end
  end
end

$src = ARGV.shift
die "Usage:#{$0} src.ipa [dst.epub]" unless $src && File.exist?($src)

$dst = ARGV.shift
unless $dst
  $dst = File.basename($src)
  $dst = $dst.sub(/\w+$/, 'epub')
end

Zip::Archive.open($src) do |ar|
  ar.each do |zf|
    if zf.directory?
      FileUtils.mkdir_p(zf.name)
    else
      next if zf.name =~ /iTunes/
      dirname = File.dirname(zf.name)
      FileUtils.mkdir_p(dirname) unless File.exist?(dirname)

      open(zf.name, 'wb') do |f|
        f << zf.read
      end
    end
  end
end

$app = Dir.glob("Payload/*.app/book").first
die "No book found in the archive" unless $app

Dir.chdir($app) do |path|
  $updir = '../../..'

  Zip::Archive.open("#{$updir}/#{$dst}", Zip::CREATE, Zip::BEST_SPEED) do |ar|
    ar.add_file('mimetype')
  end

  zip_add_dir_r("#{$updir}/#{$dst}", 'META-INF')
  zip_add_dir_r("#{$updir}/#{$dst}", 'OEBPS')
end

clean

iTunesに登録するところは、手動でどうぞ。