构建API服务器6

文档

将slate集成到项目中

$ mkdir app/docs

集成slate

$ cd app/docs

$ git clone git@github.com:tripit/slate.git

$ rm -rf slate/.git

$ cd slate

$bundle install

配置构建文件app/docs/slate/config.rb

+ set :build_dir, '../../../public/docs/'

现在开始编写获取用户信息这个API的文档
app/docs/slate/source/index.md

---
title: API Reference

language_tabs:
  - ruby

toc_footers:
  - <a href='http://github.com/tripit/slate'>Documentation Powered by Slate</a>

includes:
  - errors

search: true
---

# 介绍

API文档

#获取用户信息

## V1

## HTTP请求

`GET http://my-site/api/v1/users/<id>`

## 请求参数

参数名 | 是否必须 | 描述
--------|------------| -------
id        | 是             | 用户id

##响应
\```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文档加访问权限
**配置路由:**
**```routes.rb```**
  • get '/docs/index', to: 'docs#index'
**建立相关控制器**

$ 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```文件转移到```app/views/docs/```目录下面**。
**我们可以写一个脚本```docs_build.sh```,注意将这个脚本放在项目的根目录下。**

!/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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,168评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 用手机记录真的很不方便。 尤其是坐在床上记录,算了,明天再正式记吧。
    Do_More阅读 68评论 0 0
  • 饯花之期,正是春残花消之时。大观园里那些在春日盛极一时的凤仙、石榴,曾经的浓艳抵不住世间的种种萧条,最终竟也落红成...
    姑射阅读 352评论 0 2