2012年1月31日火曜日

[ruby]最初に知っておけば良かったbundlerの使い方 rails編

PHPの代わりというか、次のレベルとしてRubyを使うならまず最初にbundlerの使い方を知っておきたかった。 以下自分なりに覚えたbundlerの使い方を記録しておく。

作業ディレクトリを作る。

$ mkdir hoge_project

railsなんかはディレクトリ名がそのままアプリ名になっちゃうから気をつける
(これってオプションでディレクトリ名と別の名前渡せるのかな??)

作業ディレクトリでbundlerのinitを実行する。

$ cd hoge_project
$ bundle init
Writing new Gemfile to /Users/username/hoge_project/Gemfile

これでサンプルのGemfileが作られる。
▼内容

# A sample Gemfile
source "http://rubygems.org"

# gem "rails"

▼一行目を削除し、四行目のコメントアウトを外してバージョンを3.1.3に指定

source "http://rubygems.org"

gem "rails", "3.1.3"

プロジェクトローカルな状態でgemをインストール

$ bundle install --path vendor/bundle

地味にbundlerの便利なところはこの時点でvendorフォルダの有無に関わらずインストール出来ちゃうところ。

Fetching source index for http://rubygems.org/
Installing rake (0.9.2.2) 
Installing multi_json (1.0.4) 
Installing activesupport (3.1.3) 
Installing builder (3.0.0) 
Installing i18n (0.6.0) 
Installing activemodel (3.1.3) 
Installing erubis (2.7.0) 
Installing rack (1.3.6) 
Installing rack-cache (1.1) 
Installing rack-mount (0.8.3) 
Installing rack-test (0.6.1) 
Installing hike (1.2.1) 
Installing tilt (1.3.3) 
Installing sprockets (2.0.3) 
Installing actionpack (3.1.3) 
Installing mime-types (1.17.2) 
Installing polyglot (0.3.3) 
Installing treetop (1.4.10) 
Installing mail (2.3.0) 
Installing actionmailer (3.1.3) 
Installing arel (2.2.1) 
Installing tzinfo (0.3.31) 
Installing activerecord (3.1.3) 
Installing activeresource (3.1.3) 
Using bundler (1.0.21) 
Installing json (1.6.5) with native extensions 
Installing rack-ssl (1.3.2) 
Installing rdoc (3.12) 
Installing thor (0.14.6) 
Installing railties (3.1.3) 
Installing rails (3.1.3) 
Your bundle is complete! It was installed into ./vendor/bundle

こんな感じでインストール完了。
最初はrails以外のgemがわんさかインストールされちゃってる事にかなりびっくりしました。
今なら依存関係があるから当たり前やんって思うけど、この辺のニュアンスってなかなか初心者には分かりづらい。

ここまでだとrailsアプリはまだ出来てないのでスケルトンを作る

$ bundle exec rails new . -T -d mysql

このコマンドの意味するところは
プロジェクトローカルのgemを使い、
railsの新しいアプリケーションをこのディレクトリで作成し、
かつtestディレクトリを省略し、
DBはmysqlを選択
ということです。

▼で実行するとこんな問いかけをくらう

       exist  
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /Users/username/hoge_project/Gemfile? (enter "h" for help) [Ynaqdh] 

既存のGemfileをrails仕様に上書きしてもいいか?ということで問題ないので enter

       force  Gemfile
      create  app
      create  app/assets/images/rails.png

          ... 省略 ...

         run  bundle install

でここでエラーが発生するんですよねぇ…
/Users/username/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/resolver.rb:280:in `resolve': Could not find gem 'jquery-rails (>= 0) ruby' in any of the gem sources listed in your Gemfile. (Bundler::GemNotFound)

でも気にせずrspecをGemfileに追加

group :development, :test do
  gem 'rspec-rails'
end

でbundle installに再チャレンジ

$ bundle install
Fetching source index for http://rubygems.org/
Installing coffee-script-source (1.2.0) 
Installing execjs (1.3.0) 
Installing coffee-script (2.2.0) 
Installing coffee-rails (3.1.1) 
Installing diff-lcs (1.1.3) 
Installing jquery-rails (1.0.19) 
Installing mysql2 (0.3.11) with native extensions 
Installing rspec-core (2.8.0) 
Installing rspec-expectations (2.8.0) 
Installing rspec-mocks (2.8.0) 
Installing rspec (2.8.0) 
Installing rspec-rails (2.8.1) 
Installing sass (3.1.12) 
Installing sass-rails (3.1.5) 
Installing uglifier (1.2.3) 
Your bundle is complete! It was installed into ./vendor/bundle
※既にインストール済みの行は省きました

よし!
これでrailsアプリのスケルトン出来ました。
後はrailsコマンドとかrakeするときに常に bundle exec を付けておけば
プロジェクトローカルのgemで実行されます。

アプリケーション毎のbundlerの設定は bundle config で確認できる(.bundle/configファイルの中身を見てる)

$ bundle config
Settings are listed in order of priority. The top value will be used.

path
  Set for your local app (/Users/username/hoge_project/.bundle/config): "vendor/bundle"

disable_shared_gems
  Set for your local app (/Users/username/hoge_project/.bundle/config): "1"

開発用サーバを起動

$ bundle exec rails s
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-01-31 17:06:22] INFO  WEBrick 1.3.1
[2012-01-31 17:06:22] INFO  ruby 1.9.2 (2011-07-09) [x86_64-darwin10.8.0]
[2012-01-31 17:06:22] INFO  WEBrick::HTTPServer#start: pid=1895 port=3000

ちなみにこの時に -p (ポートオプション)を付ければ好きなポート番号でWEBrickを起動できますね

まだ初心者が知っておいたほうがいい事があって、
バージョン管理している場合気を付けなくちゃいけないのは vendor/bundle ディレクトリは管理の対象にしないこと。
これって本業のプログラマーさんなら当たり前なんだろうけど。
mysql2とか、環境に依存するgemもあるから環境毎にbundle installするべきだってことに気づくのが大分遅かった…

2012年1月6日金曜日

Nokogiriでページタイトルを取得する

これがあんまりうまく行かない場合が多い。ローカル(Mac)では取得できるけどherokuでダメってことも多い。
ということで今のところ以下のように落ち着いてる。 取得先のHTML構造が壊れていたり、URLに日本語がそのまま含まれていたりすると厄介。