id:onk さんのMount Engineの記事読んだら便利そうだったのでやってみた。
Railsプロジェクトの作成
まずおもむろにGitHubリポジトリをつくり
rbenv localbundle install --path=vendor/bundlebundle exec rails new .
等の儀式を済ませる。 差分は下記の通り。
welcome controllerというHello World的なControllerとViewを作る
[diff]Create a Rails controller
[diff]Create a Rails view
bundle exec rake routesの結果はこんな感じ
[mazgi@Balmung] $ bundle exec rake routes Prefix Verb URI Pattern Controller#Action root GET / welcome#index
Welcome!と表示された。

Mount Engineを試す
記事を参考にconfig/routes.rbに
mount proc {|env| [200, {}, ["Hello from mounted engine!"]] }, at: "hello_mount_engine"
の1行を書き加えてみる。
routeが増えた。
[mazgi@Balmung] $ bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
/hello_mount_engine #<Proc:0x007f138f7872c0@/home/mazgi/Creations/MountableEngineTestDrive/config/routes.rb:4>
/hello_mount_engineというpathでちゃんと表示された。

Sinatraアプリをマウントしてみる
まずとても簡単なSinatraアプリを作る。 内容はこれだけ:
[mazgi@Balmung] $ < lib/sinatra_app/app.rb
require 'sinatra/base'
class HelloFromSinatra < Sinatra::Base
set :bind, '0.0.0.0'
get "/" do
"I am Sinatra!"
end
get "/hello" do
"Hello from Sinatra!"
end
run! if app_file == $0
end
まずは単体でbundle exec ruby lib/sinatra_app/app.rb -e productionする。
'/'と'/hello'はそれぞれ次のように表示される。


ではいよいよSinatraアプリをRailsアプリにマウントしてみる。
まずconfig/application.rbに
require Rails.root.join("lib/sinatra_app/app")
の1行を追加し、次にconfig/routes.rbにSinatraアプリをmountする。
mount HelloFromSinatra, at: "hello_sinatra"
[diff]Add mount for Sinatra app to routes
bundle exec rake routesの出力が増えている。
[mazgi@Balmung] $ bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
/hello_mount_engine #<Proc:0x007fb0335f85a8@/home/mazgi/Creations/MountableEngineTestDrive/config/routes.rb:4>
hello_from_sinatra /hello_sinatra HelloFromSinatra
簡単にRailsアプリからSinatraアプリを呼び出せた。


Mount Engine便利っぽいしドリコム Advent Calendar 2014勉強になる。