2016年2月1日月曜日

【作業メモ】2016.01.31

Herokuにsinatraで簡単なApiサーバーを作る。

/Gemfile
2ruby "2.2.2"
3gem 'sinatra', '1.4.7'
/Procfile
1web: bundle exec ruby app.rb -p $PORT
/config.rb
1web: bundle exec rackup config.ru -p $PORT
/app.rb
01require 'rubygems'
02require 'sinatra'
03require 'json'
04 
05get "/app" do
06  json_data = open("settings/shuzo.json") do |io|
07    JSON.load(io)
08  end
09  json_data["items"].each do | item |
10      content_type :json
11      if params["id"] == item["id"]
12        body << item.to_json
13      end
14  end
15  body
16end
/settings/shuzo.json
01{
02  "items":[{
03    "id":"1",
04    "name":"Kuwako",
05    "age":"29"
06  },{
07    "id":"2",
08    "name":"Matsuoka",
09    "age":"48"
10  }]
11}
この辺が参考:https://devcenter.heroku.com/articles/getting-started-with-ruby-o

0 件のコメント:

コメントを投稿