文檔
將slate集成到項(xiàng)目中
$ mkdir app/docs
集成slate
$ cd app/docs
$ git clone git@github.com:tripit/slate.git
$ rm -rf slate/.git
$ cd slate
$bundle install
配置構(gòu)建文件app/docs/slate/config.rb
+ set :build_dir, '../../../public/docs/'
現(xiàn)在開始編寫獲取用戶信息這個API的文檔
app/docs/slate/source/index.md
---
title: API Reference
language_tabs:
- ruby
toc_footers:
- <a >Documentation Powered by Slate</a>
includes:
- errors
search: true
---
# 介紹
API文檔
#獲取用戶信息
## V1
## HTTP請求
`GET http://my-site/api/v1/users/<id>`
## 請求參數(shù)
參數(shù)名 | 是否必須 | 描述
--------|------------| -------
id | 是 | 用戶id
##響應(yīng)
\```json
{
"user":
{
"id":1,
"email":"test-user-00@email.com",
"name": "test-user-00",
"activated": "2015-05-02T07:47:14.697Z",
"admin":false,
"created_at":"2015-05-02T07:47:14.708Z",
"updated_at":"2015-05-02T07:47:14.708Z"
}
}
\``````
##給API文檔加訪問權(quán)限
**配置路由:**
**```routes.rb```**
- get '/docs/index', to: 'docs#index'
**建立相關(guān)控制器**
$ rails g controller docs
**修改```app/controllers/docs_controller.rb```**
class DocsController < ApplicationController
USER_NAME, PASSWORD = 'doc_reader', '123123'
before_filter :basic_authenticate
layout false
def index
end
private
def basic_authenticate
authenticate_or_request_with_http_basic do |user_name, password|
user_name == USER_NAME && password == PASSWORD
end
end
end
**同時我們需要把```public/docs/index.html```文件轉(zhuǎn)移到```app/views/docs/```目錄下面**。
**我們可以寫一個腳本```docs_build.sh```,注意將這個腳本放在項(xiàng)目的根目錄下。**
!/bin/bash
app_dir=pwd
cd $app_dir/app/docs/slate
bundle exec middleman build --clean
cd $app_dir
mv $app_dir/public/docs/index.html $app_dir/app/views/docs
**重建build文檔**
$ ./docs_build.sh