2012年4月10日火曜日

Sinatraの起動ポート変更

Sinatra起動時のポートは4567だが、変更する場合は、下記のように -p をつけて起動すれば良い。
$ ruby api.rb  -p 4568
[2012-04-10 18:05:45] INFO  WEBrick 1.3.1
[2012-04-10 18:05:45] INFO  ruby 1.9.2 (2011-07-09) [x86_64-linux]
== Sinatra/1.3.2 has taken the stage on 4568 for development with backup from WEBrick
[2012-04-10 18:05:45] INFO  WEBrick::HTTPServer#start: pid=13966 port=4568
他にもいくつかの起動オプションあり。
$ ruby api.rb  -h
Usage: api [options]
    -p port                          set the port (default is 4567)
    -o addr                          set the host (default is 0.0.0.0)
    -e env                           set the environment (default is development)
    -s server                        specify rack server/handler (default is thin)
    -x                               turn on the mutex lock (default is off)



2012年4月5日木曜日

RubyからMongoへのアクセス

単一のMongoDB
conn = Mongo::Connection.new("localhost",27017)

ReplicaSetsのあるSecondaryに接続
conn = Mongo::Connection.new("localhost",27017, :slave_ok=>true)

ReplicaSetsでMongoDB
conn = Mongo::ReplSetConnection.new(['10.20.30.40:27017','10.20.30.41:27017'], :read => :secondary)
Secondaryが落ちてる場合は、Primaryに接続される。

PrimaryとSecondaryのどちらに接続されているかを確認ができる
conn.read_primary?


参考
http://api.mongodb.org/ruby/current/Mongo/ReplSetConnection.html

Sinatraのロケールファイル

railsみたいにロケールファイルを用意するにはどうすれば良いのだろう?と
調べていたら辿りついたのがこのサイト!ありがとうございます♪
http://d.hatena.ne.jp/ruedap/20110331/ruby_sinatra_i18n_r18n

gemのインストール
$ gem install sinatra-r18n
Fetching: r18n-core-0.4.14.gem (100%)
Fetching: sinatra-r18n-0.4.14.gem (100%)
Successfully installed r18n-core-0.4.14
Successfully installed sinatra-r18n-0.4.14
2 gems installed
ロケールファイルの作成
プロジェクトルート以下に、i18nのフォルダを作成し、ja.ymlやen.ymlといったファイルを作成。

YAMLファイルなので、下記のように記載。
message:
  error1: サーバーが停止しています

呼び出し側の記述下記のrequireを追加して、tオブジェクトを用いて
ロケールファイルの定義を記載するだけで表示!
require 'sinatra/r18n'
…
t.message.error1

2012年4月2日月曜日

JSONView

Chromeに便利なJSONを整形して表示するプラグインがあったのでメモっておきます

開発元
https://github.com/gildas-lormeau/JSONView-for-Chrome

ダウンロード先
https://chrome.google.com/webstore/detail/chklaanhfefbnpoihckbnefhakgolnmc

sinatraを使ってjsonを返す際に、見やすくなります。

はじめてのSinatra

見よう見まねで、とりあえず
http://www.sinatrarb.com/intro-jp.htmlを試してみた♪
あっさり動いて感動!

gemのインストール
$ gem install sinatra
Fetching: rack-1.4.1.gem (100%)
Fetching: rack-protection-1.2.0.gem (100%)
Fetching: sinatra-1.3.2.gem (100%)
Successfully installed rack-1.4.1
Successfully installed rack-protection-1.2.0
Successfully installed sinatra-1.3.2
3 gems installed
Installing ri documentation for rack-1.4.1...
Installing ri documentation for rack-protection-1.2.0...
Installing ri documentation for sinatra-1.3.2...
Installing RDoc documentation for rack-1.4.1...
Installing RDoc documentation for rack-protection-1.2.0...
Installing RDoc documentation for sinatra-1.3.2...

アプリの作成
# myapp.rb
require 'sinatra'
get '/' do
  'Hello world!'
end

Webサーバーの起動
$ ruby -rubygems myapp.rb
[2012-04-02 16:37:45] INFO  WEBrick 1.3.1
[2012-04-02 16:37:45] INFO  ruby 1.9.2 (2011-07-09) [x86_64-linux]
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from WEBrick
[2012-04-02 16:37:45] INFO  WEBrick::HTTPServer#start: pid=24126 port=4567

アクセス
http://localhost:4567/