sinatra + mongodb
ずっと mongodb をいじっていなかったので、改めて ruby で sinatra で データ CRUD の練習をしてみた。
// データ一覧、新規追加、削除、json 形式でのレスポンス の練習。
$ cat main.rb
# 2010-05-30 katoy
# $ mongod --dbpath data
# $ shotgun main.rb
# acccess to "http://localhost:9393"
#
# ruby 1.8.7
require 'rubygems'
require 'sinatra' # 1.0
require 'erb'
require 'mongo' # 1.0.1, mongodb 1.4.2
require 'json/pure'
require 'pp'
DB = 'mydb'
COLLECTION = 'test'
template :layout do
s = <<EOS
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
.item {
border: solid;
margin: 10px;
}
.alignTopLeft {
text-align:left;
vertical-align:top;
}
</style>
<head>
<body>
<hr>
<%= yield %>
<hr>
<h1><a href="/">一覧へ</a></h1>
</body>
</html>
EOS
end
helpers do
def get_collection
db = Mongo::Connection.new.db(DB)
db[COLLECTION]
end
end
#-- Top page
get '/' do
redirect '/list'
end
# -- Form for Create
get '/create' do
erb %{
<div>
<form action='/create' method='POST'>
<span class="alignTopLeft">KEY: </span>
<input type='text' name='key'><br>
<span class="alignTopLeft">VAL: </span>
<textarea cols="100" rows="8" name='val'></textarea><br>
<input type='submit' value='登録'>
</form>
</div>
}
end
# -- Create
post '/create' do
doc = { '_id' => BSON::ObjectID.new.to_s, 'key' => params[:key], 'val' => params[:val]}
coll = get_collection
id = coll.insert(doc)
redirect "/#{id}"
end
# -- Form for Edit
get '/edit/:uid' do
erb %{
<% coll = get_collection %>
<% coll.find('_id' => params[:uid]).each do |row| %>
<div>
<form action='/edit/<%= params[:uid] %>' method='POST'>
<span class="alignTopLeft">KEY: </span>
<input type='text' name='key' value="<%= row['key'] %>"><br>
<span class="alignTopLeft">VAL: </span>
<textarea cols="60" rows="10" name='val'><%= row['val'] %></textarea><br>
<input type='submit' value='変更'>
</form>
</div>
<% end %>
} , :locals => { :param => params }
end
# -- Edit
post '/edit/:uid' do
id = params[:uid]
doc = { '_id' => id, 'key' => params[:key], 'val' => params[:val]}
coll = get_collection
coll.update( {'_id' => id }, doc)
redirect "/"
end
# -- List
get '/list' do
erb %{
<% coll = get_collection %>
<%= coll.count %> 件<br/>
<a href="/create">新規追加</a><hr>
<% coll.find().each do |row| %>
<div class="item">
キー: <a href="/edit/<%= row['_id']%>"><%= row['key'] %></a> <a href="/del/<%= row['_id']%>">削除</a><br/>
<pre><%= row['val'] %></pre>
</div>
<% end %>
}
end
# -- List format json
get "/list/json" do
ans = []
coll = get_collection
coll.find().each do |row|
ans << row
end
content_type :json
ans.to_json
end
# -- Delete
get '/del/:uid' do
uid = params[:uid]
coll = get_collection
coll.remove('_id' => uid)
redirect '/list'
end
# Read
get '/:uid' do
uid = params[:uid]
coll = get_collection
# ans = coll.find({'_id' => uid}, {:limit => 1})
ans = coll.find({'_id' => uid}, {:limit => 1})
if ans.count > 0
erb %{
<% ans.each do |row| %>
<a href="/edit/<%= row['_id']%>">変更</a><br/>
<a href="/del/<%= row['_id']%>">削除</a><br/>
<div class="item">
key:<%= row['key'] %><br/>
val:<%= row['val'] %></p>
</div>
<% end %>
}, :locals => { :ans => ans }
else
erb %{
Not find the data.
}
end
end
# -- Read format json
get '/:uid/json' do
uid = params[:uid]
ans= nil
coll = get_collection
coll.find({'_id' => uid}, {:limit => 1}).each do |row|
ans = row
end
content_type :json
ans.to_json
end
#-- End of File ---
« ---------------- ピックアップ:次世代 Apple TVは iPhone OSベース, キャバ嬢シビア過ぎワロタ, etc... | トップページ | ピックアップ:【子ども手当て支給会場】ホクホク顔の親子, 「ヱヴァ破 売れすぎw」, etc... »
この記事へのコメントは終了しました。
« ---------------- ピックアップ:次世代 Apple TVは iPhone OSベース, キャバ嬢シビア過ぎワロタ, etc... | トップページ | ピックアップ:【子ども手当て支給会場】ホクホク顔の親子, 「ヱヴァ破 売れすぎw」, etc... »
コメント