API サンプル
カンニングしつつぱぱっと作ってみる。つうか手元に api_sample などというディレクトリがあるな。どうしよ。やっぱイチから作るか。
$ rails new api_sample
bundle install 付き。rails は 3.2.11 でした。ディレクトリ移動して controller と model を作ります。
$ cd api_sample $ rails generate controller api/tasks
あ、これって model も作ってくれるのか。
で、routes.rb は
namespace "api" do resources :tasks end
を、で良いのかどうか。と、その前に Gemfile だのを修正しておきます。
group :development do gem 'sqlite3' end
ってして、Heroku 向けに以下をナニ。
group :production do gem 'pg', '0.12.2' end
一応以下を実行。
$ bundle install --without production
あと、routes.rb も修正して rake routes 確認。
$ rake routes api_tasks GET /api/tasks(.:format) api/tasks#index POST /api/tasks(.:format) api/tasks#create new_api_task GET /api/tasks/new(.:format) api/tasks#new edit_api_task GET /api/tasks/:id/edit(.:format) api/tasks#edit api_task GET /api/tasks/:id(.:format) api/tasks#show PUT /api/tasks/:id(.:format) api/tasks#update DELETE /api/tasks/:id(.:format) api/tasks#destroy
あとは controller を云々すれば OK ってことで良いのかな。て、ここまでアレした時点でやっぱ scaffold 使うべき、ってことに気がつきました。
リトライ
rails new します。
$ rails new api_sample
scaffold で云々。
$ rails generate scaffold Tasks name:string
Gemfile 修正。
group :development do gem 'sqlite3' end group :production do gem 'pg', '0.12.2' end
これで git のあたりを云々なのかな。とりあえず Heroku にぶちこむのであれば config/database.yml は修正不要らしい。
$ git init $ git add . $ git commit
で、Heroku にアレ。
$ heroku login heroku: command not found
げげ。何故だ。とりあえず未導入ってことで以下。
$ wget https://toolbelt.heroku.com/install-ubuntu.sh $ sudo sh install-ubuntu.sh
なんか ruby1.9.1 とか導入されてるみたいだけどいいのかなぁ。入ったみたいなのでリトライするとこんどは正常終了。で、heroku create か
$ heroku create $ git push heroku master
あ、この端末ってやっぱ駄目なのかな。鍵を登録して (やはり未登録でした) リトライ。上手くいくかな、って思ったら pg が云々。修正して再度 push にトライ。
You have added to the Gemfile: * pg
て盛り込んであるんですが何だこれは。で、なんか適当にこんなカンジにしてみたけどやはり駄目。
group :postgres do gem 'pg' end
うーん何だこれは。いったんアプリを抹消した方が? と思ったら正常に動きはじめやがった。一回 Gemfile.lock を git rm して push した後で bundle install --without production して再度 git add Gemfile.lock して push したら動いたぞ。
む
動かぬ、と思ったらもう一仕事必要だった模様。
$ heroku run rake db:migrate
やれやれ、scaffold なナニが出ました。かなりへろへろ。
でも認証無しで簡単なデータのやりとりのみ、であれば scaffold のみで良いのか。ちょろっとデータを入れて curl で確認してみます。
$ curl -X GET http://shrouded-tundra-4125.herokuapp.com/tasks.json [{"created_at":"2013-03-28T07:28:39Z","id":1,"name":"a","updated_at":"2013-03-28T07:28:39Z"}, {"created_at":"2013-03-28T07:28:47Z","id":2,"name":"bb","updated_at":"2013-03-28T07:28:47Z"}, {"created_at":"2013-03-28T07:29:02Z","id":3,"name":"ccc","updated_at":"2013-03-28T07:29:02Z"}, {"created_at":"2013-03-28T07:29:09Z","id":4,"name":"dddd","updated_at":"2013-03-28T07:29:09Z"}]
JSONArray が戻ってくるのか。追加もしてみる。
$ curl -X POST -d "name=eeeee" http://shrouded-tundra-4125.herokuapp.com/tasks.json {"created_at":"2013-03-28T07:32:59Z","id":5,"name":null,"updated_at":"2013-03-28T07:32:59Z"}
あら、ヤり方がおかしいのかな。あ、params[:task] になっとるな。
で、以下だとオチて
$ curl -X POST -d "task=eeeee" http://shrouded-tundra-4125.herokuapp.com/tasks.json
html なソース見て以下にしたら正常終了。
$ curl -X POST -d "task[name]=eeeee" http://shrouded-tundra-4125.herokuapp.com/tasks.json {"created_at":"2013-03-28T07:36:20Z","id":6,"name":"eeeee","updated_at":"2013-03-28T07:36:20Z"}
何ヤッてんだ、というカンジですねorz
とりあえず
端末側の実装に移ります。