# Libr #### 安装运行 * 下载postgres http://postgresapp.com/ 并运行 * 下载redis, brew install redis 并运行: redis-server * git clone 代码 #### 安装Ruby依赖文件 ``` bundle install ``` #### 初始化postgres数据库登陆信息 ``` create role twer with superuser; alter user twer login; ``` ``` rake db:create rake db:migrate ``` #### 运行Rails ``` rails s #运行 sidekiq, 在项目跟目录下: bundle exec sidekiq -C config/sidekiq.yml ``` or ``` bundle exec puma -t 5:5 -p 3000 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] - 返回结果 ```json { "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] - 返回结果: ```json { "user": { "id": 3, "name": null, "email": "wahyd4@qq.com", "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IjU0MGU4OTU4ODg3ZmI1MGI5YTA5YzI4MjQ2NTZlYTRlIn0.XQBWCNRYVdDvZwuBtafCyOkNpP8FEzwRqk8AhaHlaFc" } } ``` 3 提交分享资源 POST: http://localhost:3000/api/v1/posts - 参数 * 需要登录 传Authorization Bearer header * url 网址 * type article 或者 page * description 可选 - 返回 ``` json { "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 获取用户发布的posts GET http://localhost:3000/api/v1/posts - 参数 * Authorization - 返回 ``` json [ { "description": "这是一个特别cool的github 项目", "sharable_type": "Post", "sharable": { "viewed_times": 0, "url": "https://github.com/sparklemotion/mechanize", "title": "sparklemotion/mechanize · GitHub", "type": "posts", "processed": true, "good_format": false, "created_at": "2015-08-07T00:25:56.942+08:00" } }, { "description": "教你如何面试", "sharable_type": "Post", "sharable": { "viewed_times": 0, "url": "http://www.jianshu.com/p/27956b0ce2fc", "title": "良心干货|如何惊呆你的面试官 - 简书", "type": "articles", "processed": true, "good_format": false, "created_at": "2015-08-07T00:30:56.788+08:00" } } ] ``` 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 - 返回结果 ``` json { "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 - 返回结果 ``` json { "msg": "done", "comments_count": 3 } ``` 10 评论列表 GET http://localhost:3000/api/v1/posts/1/comments - 返回结果: ``` json [ { "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 - 返回结果: ```json { "msg": "done", "likes_count": 1 } ``` 12 取消喜欢评论 comment: POST http://localhost:3000/api/v1/posts/1/comments/1/unlike 13 增加文章的浏览数(当用户点击查看某个文章时使用),不需要关心返回值 POST http://localhost:3000/api/v1/posts/3/view 14 获取用户喜欢的文章 GET http://localhost:3000/api/v1/posts/liked - 支持分页 ?page=1 - 返回结果 ``` json [ { "viewed_times": 0, "url": "http://www.jianshu.com/p/27956b0ce2fc", "title": "良心干货|如何惊呆你的面试官 - 简书", "type": "articles", "processed": true, "good_format": false, "created_at": "2015-08-07T00:30:56.788+08:00" }, { "viewed_times": 0, "url": "https://github.com/sparklemotion/mechanize", "title": "sparklemotion/mechanize · GitHub", "type": "posts", "processed": true, "good_format": false, "created_at": "2015-08-07T00:25:56.942+08:00" }, { "viewed_times": 3, "url": "https://github.com/strongloop/express", "title": "strongloop/express · GitHub", "type": "web_pages", "processed": true, "good_format": false, "created_at": "2015-08-07T00:33:27.420+08:00" } ] ``` 15 获取最受欢迎的文章 GET http://localhost:3000/api/v1/top - 参数 * 无 - 返回结果 ```json [ { "viewed_times": 3, "url": "https://github.com/strongloop/express", "title": "strongloop/express · GitHub", "type": "web_pages", "processed": true, "good_format": false, "created_at": "2015-08-07T00:33:27.420+08:00" }, { "viewed_times": 0, "url": "https://github.com/sparklemotion/mechanize", "title": "sparklemotion/mechanize · GitHub", "type": "posts", "processed": true, "good_format": false, "created_at": "2015-08-07T00:25:56.942+08:00" } ] ``` #### 注意 * 注意,API返回结果中的token是利用JWT 技术加密的token * [jwt](http://jwt.io/) * 通常执行创建(POST) 操作时,如果没有返回结果,仅仅返回200,也是成功的表示, 当返回400时即为失败