LoginSignup
26
27

More than 5 years have passed since last update.

Ruby 2.0 & Sinatra 1.4.0.d でアプリ作った

Posted at

AAが出力できて便利です。

Gemfile
source "https://rubygems.org"
ruby '2.0.0'

gem 'sinatra', '1.4.0.d'
gem 'slim'
gem 'pebbles-soreyuke'
gem 'puma'

group :development do
  gem 'foreman'
  gem 'sinatra-contrib', github: 'sinatra/sinatra-contrib', require: 'sinatra/reloader'
end
app.rb
require 'bundler'
Bundler.require
require "sinatra/reloader" if development?

helpers do
  # This is a Ruby 2 feature
  def presence_of(word: nil)
    word.nil? || word.size <= 0 ? nil : word
  end
end

configure :production do
  set :server, :puma
end

get '/' do
  slim :index
end

get '/say-hello' do
  word = presence_of(word: params[:word])
  @text = Pebbles::Soreyuke::AA(params[:type], word)
  slim :hello
end
__END__

@@ layout
doctype html
html
  head
    title 便利ウェブアプリサンプル
    link href="/css/bootstrap.min.css" rel="stylesheet"
    css:
      body { padding-top: 60px; }

  body
    .navbar.navbar-inverse.navbar-fixed-top
      .navbar-inner
        .container
          a.brand href="/" The Sinatra 1.4 & Ruby 2.0 Example
    .container
      == yield
      .well.well-small
        p © Uchio KONDO 2013.
        p
          | Using
          code= RUBY_DESCRIPTION
          |  & Sinatra
          code= Sinatra::VERSION

    script src="http://code.jquery.com/jquery.js"
    script src="/js/bootstrap.min.js"

@@ index
.row
  .hero-unit
    h1 Hello
    p コレがルビー2.0 & シナトリャ 1.4 だァーッ
  .span9
    h2 インタラクティブコンテンツを試す
    form action='/say-hello' method='GET'
      fieldset
        label タイプ :
        select name="type"
          option value="apm" あんパン
          option value="cpm" カレーパン
          option value="spm" 食パン
        label 挨拶の文言 :
        input type='text' name='word'
        br
        input.btn type="submit" value="シナトラを試す"

@@ hello
.row
  .span9
    pre
      = @text
  .span9
    p
      a.btn href="/" Back
Procfile
web: bundle exec ruby app.rb -p $PORT -e $RACK_ENV

Tips

うごいたgem

  • sinatra-reloader: githubのHEADから取った。普通に動いた。sinatra-contrib自体はeventmachineって言う凄いやつに依存しているので取り扱い注意(reloaderだけのときは依存していないけど……)。
  • slim: 動いた。
  • pebbles-soreyuke: 便利だった。動いた。 see also: http://qiita.com/items/19807642971ebcc92f52
  • puma: 動いた。Railsでも http://qiita.com/items/71e9f7df7f6bdb93966e

Heroku でRuby 2.0 を使う

ruby '2.0.0' ってGemfileに書いとくだけでヨイ。すごい。

コード

26
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
27