Files
libr-2/README.md
T
2015-08-16 22:57:00 +08:00

4.5 KiB

Libr

安装运行

  • 下载postgres http://postgresapp.com/ 并运行
  • 下载redis, brew install redis 并运行: redis-server
  • git clone 代码
  • bundle install
  • rake db:create, rake db:migrate
  • rails s
  • 运行 sidekiq, 在项目跟目录下: bundle exec sidekiq -C config/sidekiq.yml

API 调用

1 注册 POST: http://127.0.0.1:3000/api/v1/sign_up

  • 参数:
    • user[email]
    • user[password]
    • user[password_confirmation]
  • 返回结果
{
  "user": {
    "id": 10,
    "name": "test5",
    "email": "test5@qq.com",
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6ImU3M2YzYTljMTIxMjVkNDViNjJlNjkxNjQ4MjgyNGVmIn0.Rv_DkYEDWOIhObnTjC_8N8L-BACm8cFCOSqEgSmPUDc"
  }
}

2 登录 POST: http://127.0.0.1:3000/api/v1/sign_in

  • 参数:
  • user[email]
  • user[password]
  • 返回结果:
 {
   "user": {
     "id": 3,
     "name": null,
     "email": "wahyd4@qq.com",
     "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IjU0MGU4OTU4ODg3ZmI1MGI5YTA5YzI4MjQ2NTZlYTRlIn0.XQBWCNRYVdDvZwuBtafCyOkNpP8FEzwRqk8AhaHlaFc"
   }
 }

3 提交分享资源 POST: http://localhost:3000/api/v1/posts

  • 参数
  • 需要登录 传Authorization header
  • url 网址
  • type article 或者 web_page
  • description 可选
  • 返回
  {
    "id": 32,
    "user_id": 3,
    "sharable_id": 3,
    "sharable_type": "WebPage",
    "created_at": "2015-08-04T00:33:25.735+08:00",
    "updated_at": "2015-08-04T00:33:25.735+08:00",
    "description": "这是一个特别cool的github 项目",
    "sharable": {
      "id": 3,
      "name": null,
      "url": "http://mp.weixin.qq.com/s?__biz=MzA4NjAzMjEyOA==&mid=207485444&idx=1&sn=c0d5e1b2399fffbcdc5a5065eea52af8",
      "created_at": "2015-08-04T00:33:25.627+08:00",
      "updated_at": "2015-08-04T00:33:25.627+08:00",
      "processed": false,
      "view_account": 0
    }
  }

4 未注册用户首页数据 GET http://localhost:3000/api/v1/posts

  • 参数
  • 返回
  [
    {
      "description": "这是我看到底第一篇他写的文章",
      "sharable_type": "Article",
      "sharable": {
        "view_account": 0,
        "url": "http://www.jianshu.com/p/bfbc77eecdb0",
        "title": null
      }
    },      
    {
      "description": "到底能不能成功啊,我去",
      "sharable_type": "Article",
      "sharable": {
        "view_account": 0,
        "url": "http://www.cnbeta.com/articles/416787.htm",
        "title": null
      }
    },
    {
      "description": "这是一个特别cool的github 项目",
      "sharable_type": "WebPage",
      "sharable": {
        "view_account": 0,
        "url": "https://github.com/sparklemotion/mechanize",
        "title": "sparklemotion/mechanize · GitHub"
      }

5 关注follow 用户:POST http://localhost:3000/api/v1/users/2/follow

6 取消关注unfollow 用户: POST: http://localhost:3000/api/v1/users/2/unfollow

7 喜欢 post POST: http://localhost:3000/api/v1/posts/3/like

  • 返回结果
{
  "msg": "done",
  "likes_count": 1
}

8 取消喜欢 post POST: http://localhost:3000/api/v1/posts/3/unlike

9 创建评论 POST: http://localhost:3000/api/v1/posts/1/comments

  • 参数
    • content
  • 返回结果
{
  "msg": "done",
  "comments_count": 3
}

10 评论列表 GET http://localhost:3000/api/v1/posts/1/comments

  • 返回结果:
[
  {
    "content": "这篇文章写得很深入,很好",
    "likes_count": 0,
    "user": "test",
    "time": "2015-08-12T22:00:27.556+08:00"
  },
  {
    "content": "太长了,不太好读",
    "likes_count": 1,
    "user": "test",
    "time": "2015-08-12T22:00:42.356+08:00"
  },
  {
    "content": "太长了,不太好读",
    "likes_count": 0,
    "user": "test",
    "time": "2015-08-16T22:48:39.099+08:00"
  }
]

11 喜欢评论 comment: POST http://localhost:3000/api/v1/posts/1/comments/1/like

  • 返回结果:
{
  "msg": "done",
  "likes_count": 1
}

12 取消喜欢评论 comment: POST http://localhost:3000/api/v1/posts/1/comments/1/unlike

注意

  • 注意,API返回结果中的token是利用JWT 技术加密的token,密钥是每个用户的email
  • jwt
  • 通常执行创建(POST) 操作时,如果没有返回结果,仅仅返回200,也是成功的表示, 当返回400时即为失败