sinatra ã¯ã·ã³ãã«ã§åããããã
ã¡ãã£ã¨ä½¿ãã«ã¯ sinatra ã®ä½¿ãæ¹ãæ¥æ¬èªã§ãæ®éã«ããã®ã§ã大ãã¦å°ããªãã§ãããã
ããã辺ãããã°ãsinatra èªä½ãããç¨åº¦ä½¿ããããpassenger ã使ã£ã¦ãµã¼ãã«ãããã¤ãã¦éç¨ããã®ãå°ããªãã
ãã®ãã
å ¬å¼ã®ããã¥ã¢ã«ã£ã¦èªãã§ãªãã£ããã§ããã©ãæ¹ãã¦è¦ã¦ã¿ãã¨åãããããã®ã§ãèªãã§ãªã人ã¯ä¸èªãã¦ãããæ¹ãè¯ãã§ãããã
èªãã§ãã¦çºè¦ããç¹
ãã¼ã«ã«å¤æ°ããã³ãã¬ã¼ãå ã§ä½¿ç¨ãããã¨ãã§ãã
ä¸è¨ã®ããã«ã :local => {} ã§ãã¼ã«ã«å¤æ°ããã³ãã¬ã¼ãå ã§æ±ããããã«ã§ããã¿ããã§ãã
get '/:id' do foo = Foo.find(params[:id]) haml '%h1= foo.name', :locals => { :foo => foo } endãã®ããæ¹ã¯ä»ã®ãã³ãã¬ã¼ãå ã§é¨åãã³ãã¬ã¼ãã¨ãã¦è¡¨ç¤ºããæã«å ¸åçã«ä½¿ç¨ããã¾ãã
å¼·å¶çµäº
ã«ã¼ããbeforeãã£ã«ã¿å ã§ç´ã¡ã«å®è¡ãçµäºããæ¹æ³:
haltbodyé¨ãæå®ãããã¨ãã§ãã¾ã â¦
halt 'ããã«bodyãæ¸ã'ã¹ãã¼ã¿ã¹ã¨bodyé¨ãæå®ãã â¦
halt 401, 'ç«ã¡å»ã!'
ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã®ã¨ã©ã¼ã¨ããä½ãä¾å¤ããã£ã¦å³åº§ã«æ¢ãããå ´åã«ä½¿ãã°è¯ãã®ã ãããã
ããã·ã³ã°(Passing)
ã«ã¼ãã¯passã使ã£ã¦æ¬¡ã®ã«ã¼ãã«é£ã°ããã¨ãã§ãã¾ã:
get '/guess/:who' do pass unless params[:who] == 'Frank' "è¦ã¤ãã£ã¡ãã£ã!" end get '/guess/*' do "ã¯ããã§ã!" end
å½¹ã«ç«ã¤å±é¢ãããã¨æãæµ®ãã°ãªããã©ãå ±éã®å¦çãããããæã¨ãã«å½¹ã«ç«ã¡ããã
è¨å®
ã©ã®ç°å¢ã§ãèµ·åæã«ï¼åã ãå®è¡ããã¾ãã
configure do ... endç°å¢å¤æ°:production(RACK_ENVç°å¢å¤æ°) ãã»ããããã¦ããæã ãå®è¡ããæ¹æ³:
configure :production do ... endç°å¢å¤æ°:production ã:testã®å ´åã«è¨å®ããæ¹æ³:
configure :production, :test do ... end
ãã㯠Rails ã§ããããããªæãã§ãããã¨ãããã configure ã¡ã½ããã¨ããã£ããã§ããã¼ã
ã¨ã©ã¼
error ãã³ãã©ã¼ã¯ã«ã¼ããããã¯ãbeforeãã£ã«ã¿å ã§ä¾å¤ãçºçããæã¯ãã¤ã§ãçºåãã¾ãã block or before filter. ä¾å¤ãªãã¸ã§ã¯ãã¯Rackå¤æ°sinatra.errorããåå¾ããã¾ãã
error do 'ã¨ã©ã¼ãçºçãã¾ããã - ' + env['sinatra.error'].name endã¨ã©ã¼ãã«ã¹ã¿ãã¤ãºããå ´åã¯ã
error MyCustomError do 'ã¨ã©ã¼ã¡ãã»ã¼ã¸...' + request.env['sinatra.error'].message endã¨æ¸ãã¦ããã¦,ä¸è¨ã®ããã«å¼ã³åºãã¾ãã
get '/' do raise MyCustomError, 'ä½ããã¾ããã£ãããã§ã' endããããã¨ãããªãã¾ã:
ã¨ã©ã¼ã¡ãã»ã¼ã¸... ä½ããã¾ããã£ãããã§ã
éçºç°å¢ã¨ãã¦å®è¡ãã¦ããå ´åãSinatraã¯ç¹å¥ãªnot_foundã¨errorãã³ãã©ã¼ãã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã
ãããªæãã§ã¨ã©ã¼ã notfound ãã«ã¹ã¿ãã¤ãºã§ãããã§ããããªãã¨ãªãç¥ã£ã¦ã¯ãããã®ã®ã使ã£ãäºãªãã£ãï¼ï¼
ãã¹ã
Sinatraã§ã®ãã¹ãã¯Rack-basedã®ãã¹ãã©ã¤ãã©ãªããã¬ã¼ã ã¯ã¼ã¯ã使ã£ã¦æ¸ããã¨ãã§ãã¾ãã Rack::Test ããããããã¾ããããæ¹:
require 'my_sinatra_app' require 'rack/test' class MyAppTest < Test::Unit::TestCase include Rack::Test::Methods def app Sinatra::Application end def test_my_default get '/' assert_equal 'Hello World!', last_response.body end def test_with_params get '/meet', :name => 'Frank' assert_equal 'Hello Frank!', last_response.body end def test_with_rack_env get '/', {}, 'HTTP_USER_AGENT' => 'Songbird' assert_equal "ããªãã¯Songbirdã使ã£ã¦ã¾ãã!", last_response.body end end注æ: ãã«ãã¤ã³ã®Sinatra::Testã¢ã¸ã¥ã¼ã«ã¨Sinatra::TestHarnessã¯ã©ã¹ã¯ 0.9.2ãªãªã¼ã¹ä»¥éãå»æ¢äºå®ã«ãªã£ã¦ãã¾ãã
Rack::Test ããã¾ããã¨ä½¿ããããã«ãªã£ã¦ãããã§ãããç¥ããªãã£ãã»ã»ã»ï¼
ã³ãã³ãã©ã¤ã³
ãã¼ã«ã«ç°å¢ã§å®è¡ããå ´åã¯ãã³ãã³ãã©ã¤ã³ãã sintara ãèµ·åãããã®ã ãã©ããã®ãªãã·ã§ã³ãããããããã
-h # ãã«ã
-p # ãã¼ãæå®(ããã©ã«ãã¯4567)
-e # ç°å¢ãæå® (ããã©ã«ãã¯development)
-s # rackserver/handlerãæå® (ããã©ã«ãã¯thin)
-x # mutex lockãä»ãã (ããã©ã«ãã¯off)
ãããæ©ã«
ããã¡ãã£ã¨ sinatra ã使ãè¾¼ãã§è¡ãããã§ããã
ãã¨ã Rack ã®æ©è½ããã¾ãç¥ããªãï¼(^o^)ï¼¼ã®ã§ãããã¡ãã£ã¨æ´»ç¨ããããªã
ä¾ãã°ãHTMLã®ã¨ã¹ã±ã¼ãã¨ãããã°å¨ãã¨ãã