2012-04-11 :-)
_ [リッジレーサー7][heroku][sinatra][ruby][haml][git]heroku で sinatra を使ってリッジレーサー7 のコースとマシンをランダムに印字するだけの簡単なウェブアプリケーションを作ってみた
どう見ても ARC( ANS RidgeRacer Championship ) 用なんだがそれはそれとして。
これからRubyを始める人たちへ に触発されてというか sinatra ってこんな簡単なのか、ということで見よう見まねでやってみた。
heroku への準備
sinatra でローカルで動作するようになったら heroku の準備をする。公式に手順がある。倉貫さんや machu さんの所にもある。
- Getting Started with Ruby on Heroku/Cedar
- Heroku上でSinatraアプリを動かすまでのまとめ - まちゅダイアリー(2011-10-02)
- Ruby版PaaSの"Heroku"で無料Railsホスティング環境を手に入れよう - Social Change!
Gemfile と Procfile を用意。
% touch Gemfile
中身
source :rubygems gem 'sinatra' gem 'thin' gem 'haml'
% touch Procfile
中身
web: bundle exec ruby rr7.rb -p $PORT
bundle しておく
% bundle install
foreman 入れる
% gem install forema
foreman で起動
% foreman start 22:49:10 web.1 | started with pid 3388 22:49:12 web.1 | == Sinatra/1.3.2 has taken the stage on 5000 for development with backup from Thin
http://localhost:5000 にアクセスして動作してれば ok
heroku へデプロイ
手順どおりにやる。
% git init Initialized empty Git repository in /home/rin/work/lang/ruby/RR7/.git/
% git add .
% git commit -m "create" [master (root-commit) 38a72ff] create 6 files changed, 206 insertions(+), 0 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Procfile create mode 100644 course.rb create mode 100644 machine.rb create mode 100755 rr7.rb
heroku create するときに --stack cedar を指定しないと git push するときに「 Rails か Rack アプリ以外は受け付けねーよ」と怒られてしまう。
% heroku create --stack cedar rr7 Creating rr7... done, stack is cedar http://rr7.herokuapp.com/ | git@heroku.com:rr7.git Git remote heroku added
% git remote heroku
% git remote show heroku Warning: Permanently added the RSA host key for IP address '50.19.85.132' to the list of known hosts. Enter passphrase for key '/home/rin/.ssh/id_rsa': * remote heroku Fetch URL: git@heroku.com:rr7.git Push URL: git@heroku.com:rr7.git HEAD branch: (unknown)
push する。
% git push heroku master Enter passphrase for key '/home/rin/.ssh/id_rsa': Counting objects: 8, done. Delta compression using up to 4 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (8/8), 1.88 KiB, done. Total 8 (delta 0), reused 0 (delta 0) -----> Heroku receiving push -----> Ruby app detected -----> Installing dependencies using Bundler version 1.1.2 Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment Fetching gem metadata from http://rubygems.org/..... Installing daemons (1.1.8) Installing eventmachine (0.12.10) with native extensions Installing haml (3.1.4) Installing rack (1.4.1) Installing rack-protection (1.2.0) Installing tilt (1.3.3) Installing sinatra (1.3.2) Installing thin (1.3.1) with native extensions Using bundler (1.1.2) Your bundle is complete! It was installed into ./vendor/bundle Cleaning up the bundler cache. -----> Discovering process types Procfile declares types -> web Default types for Ruby -> console, rake -----> Compiled slug size is 3.8MB -----> Launching... done, v3 http://rr7.herokuapp.com deployed to Heroku To git@heroku.com:rr7.git * [new branch] master -> master r0
ok
heroku create したときにアプリケーション名を指定しておいたので http://rr7.herokuapp.com/ にアクセスすれば ok 。
Heroku push rejected, no Rails or Rack app detected
Gemfile や Procfile を準備せずに heroku create して git push しようとすると怒られる。
% git push heroku master Enter passphrase for key '/home/rin/.ssh/id_rsa': Counting objects: 10, done. Delta compression using up to 4 threads. Compressing objects: 100% (9/9), done. Writing objects: 100% (10/10), 2.04 KiB, done. Total 10 (delta 1), reused 0 (delta 0) -----> Heroku receiving push ! Heroku push rejected, no Rails or Rack app detected To git@heroku.com:deep-sunrise-7357.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'git@heroku.com:deep-sunrise-7357.git'
この状態になって、後から Gemfile や Procfile を追加してもやはり同じように怒られてしまうので訳わかんなくなったので一度 rm -rf .git して heroku create --stack cedar してから再度 git init から作業したらちゃんとデプロイ出来た。よく分からない。
_ [ruby]ruby 1.9 の Array#sample
これからRubyを始める人たちへ に Array#sample が使われてるんだが残念ながら手元の環境が ruby 1.8.7 だったので Array#sample が実装されてない( クエリ:sample - るりまサーチ )。代替手段面倒くさかった。
#!/usr/bin/ruby # -*- encoding: utf-8 -*- # これからRubyを始める人たちへ # http://melborne.github.com/2012/04/09/to-newbie/ def password_gen(size = 8) # ruby1.9 # [*0..9, *'a'..'z', *'A'..'Z'].sample(size).join # ruby1.8 a = [*0..9] + [*'a'..'z'] + [*'A'..'Z'] pass ||= [] size.times { pass << a[rand(a.length)] } pass.join end if __FILE__ == $0 puts 10.times.map { password_gen } end