doraemon 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d9c85bd3eb87662ed6053a4e0a1e649af527a6c5455b3593df390dcf9d76c3b
4
- data.tar.gz: 42fb2a77a7327bb6944224f480c282f87787f7f16bb7b7dc416a887851b5bab1
3
+ metadata.gz: 40d6031e236f4e0ecaad08587400227ecbc42f61eded9c4e446688ffb71e31a8
4
+ data.tar.gz: 42910edc1ff8273520e7c09edfd9105d243bbfa5553fd63cdacafe219d6973aa
5
5
  SHA512:
6
- metadata.gz: '0183784ade8020acca838281748b34f7edf262e2ad5d89961f378ababcea3f6d1e5bd0a96f1b39b60fdc05fb5328a6d501997dfa4227e58a8a326cc6fd2ee42a'
7
- data.tar.gz: 57d5383930e326d66e0164ba6019dab24f41b204b0635fbd6273ab95c8c32c57f1fce3219023a867ca59b03ee3218315ad2ac359c3604316f1ab67e901514853
6
+ metadata.gz: 02b10db8fc9d4c64b38a388dd52d4639c3187dc344d62f666bca6697b4bc593d16e830fb1d01bdda33ba26868154191404e21a95d3491fc3f5383f2430230210
7
+ data.tar.gz: 5eff94c8fc5571a4edc5743ac32173414a0b27a397c773b3170ad7c8bb5354599a890252b2c6d4e1c29cd77cfb7d0c17edaec6aeca77c5432ea35e74d0bd1c35
data/README.md CHANGED
@@ -1,35 +1,124 @@
1
1
  # Doraemon
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/doraemon`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ 一个简单的 API Mock Server,让你脱离后端进行高效的接口数据测试,同时无需侵入客户端代码
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## 安装
6
6
 
7
- ## Installation
7
+ 在命令行运行:
8
8
 
9
- Add this line to your application's Gemfile:
9
+ ```
10
+ $ [sudo] gem install doraemon
11
+ ```
12
+
13
+ 等待安装完成
14
+
15
+ ## 使用方法
16
+
17
+ ### 1. 创建 Mock 文件
18
+
19
+ 将 API 地址 path 部分中的斜杠(/)替换为下划线(_),并追加文件扩展名(.rb / .json / .api 均可)
20
+
21
+ 例如:v4/homepage/recommend 接口的 Mock 文件就是 v4_homepage_recommend.rb
22
+
23
+ ### 2. 启动 doraemon
24
+
25
+ 在 Mock 文件所在目录运行
10
26
 
11
- ```ruby
12
- gem 'doraemon'
27
+ ```
28
+ $ doraemon
13
29
  ```
14
30
 
15
- And then execute:
31
+ 此时可以看到 doraemon 已经将目录下所有 Mock 文件载入
32
+ ```
33
+ [~/api]  ls
34
+ v3_homepage_tiger_list.rb v4_homepage_recommend.rb
35
+ [~/api]  doraemon
36
+ /v4/homepage/recommend => v4_homepage_recommend.rb
37
+ /v3/homepage/tiger/list => v3_homepage_tiger_list.rb
38
+ ......
39
+ ```
16
40
 
17
- $ bundle
41
+ ### 3. 测试设备连接代理
18
42
 
19
- Or install it yourself as:
43
+ 将测试设备的代理设置到 doraemon 的默认 4000 端口上
20
44
 
21
- $ gem install doraemon
45
+ ### 4. 安装 SSL 证书(如需调试 HTTPS 接口)
22
46
 
23
- ## Usage
47
+ 打开手机浏览器,访问 doraemon 的 4001 端口,即可下载安装 SSL 证书
24
48
 
25
- TODO: Write usage instructions here
49
+ ## 使用案例
26
50
 
27
- ## Development
51
+ ### 例1. 直接 Mock JSON 数据
52
+ 假设我们想测试首页搜索栏中的提示文案,涉及 v3/word/default接口
53
+ 创建 v3_word_default.rb 文件,内容如下:
54
+ ```
55
+ {
56
+ "code": 0,
57
+ "res": {
58
+ "id": 2018050924,
59
+ "hintWord": "天王盖地虎",
60
+ "word": "宝塔镇河妖"
61
+ },
62
+ "msg": "请求已成功处理"
63
+ }
64
+ ```
65
+ 切换 tab 至首页,可以看到上方搜索栏的提示文案已被 Mock 数据替代
28
66
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+ ### 例2. 操作请求参数
68
+ 假设我们想测试优惠券兑换相关流程,涉及 v4/activity/coupon/code/exchange 接口
69
+ 创建 v4_activity_coupon_code_exchange.rb 文件,内容如下:
70
+ ```
71
+ # 通过 _params 变量访问请求的参数
72
+ correct = _params['exchageCode'] == 'abc123'
73
+ {
74
+ 'code': correct ? 0 : -1,
75
+ 'msg': correct ? '兑换成功' : '兑换码无效'
76
+ }
77
+ ```
78
+ 这样,当我们输入兑换码 abc123 进行验证,将会返回成功的数据,其他情况均为失败的结果
79
+
80
+ ### 例3. 获取原始返回数据
81
+ 假设我们想调试个我的页面的会员信息展示,涉及 v2/users/center 接口
82
+ 创建 v2_users_center.rb 文件,内容如下:
83
+ ```
84
+ # 通过 _result 变量访问原始的返回数据
85
+ _result['res']['level'] = 3
86
+ _result['res']['score'] = 666666
87
+ _result
88
+ ```
89
+ 切换到我的页面,可以发现等级已经被改写成 3,分数被改为 66666,如果想测试其他等级的数据,直接修改保存即可。
90
+
91
+ ⚠️ 所有 Mock 数据文件最后一个表达式必须是这个文件的返回值,比如例3中,如果缺额少最后一行的 _result,那该文件返回的就是最后那句赋值表达式的值(即666666)
92
+
93
+ ## 参数
94
+ ### 指定端口
95
+ ```
96
+ $ doraemon --port=5000
97
+ ```
98
+ 这时 API 监听端口就是5000,而证书下载的端口再加1也就是5001
99
+
100
+ ### 指定目录
101
+ ```
102
+ $ doraemon --root=~/api
103
+ ```
104
+ 指定工作目录为 ~/api,可以在这个目录下维护一些测试 API 文件
105
+
106
+ ## Tips:
107
+
108
+ 1. API 的修改是支持热重载的,即修改文件内容无需重启服务。但增加 API 文件是需要重启的,后期必要的话可以添加更完善的热重载机制
109
+
110
+ 2. iOS 端可以通过 Shadowrocket 创建一个 Today Widget 快速切换是否 Mock,不过这个工具是收费的
111
+
112
+ 3. 使用时可以固定一个 api 目录(假设 ~/api),这时你可以在 .bash_profile 中增加一行
113
+ ```
114
+ alias mock='doraemon --root=~/api --port=4000'
115
+ ```
116
+
117
+ 然后运行以下命令
118
+ ```
119
+ $ source ~/.bash_profile
120
+ ```
30
121
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
122
+ 以后只要打开 Terminal 执行命令 mock 就可以开始以 ~/api 为工作目录进行数据 mock
32
123
 
33
- ## Contributing
34
124
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/doraemon.
data/exe/doraemon CHANGED
@@ -12,7 +12,7 @@ include Doraemon
12
12
 
13
13
  argv = CLAide::ARGV.new(ARGV)
14
14
  port = argv.option('port', 4000)
15
- root = argv.option('root', Dir.pwd)
15
+ root = File.expand_path(argv.option('root', Dir.pwd))
16
16
 
17
17
  api_manager = Doraemon::APIManager.new(root)
18
18
 
@@ -1,3 +1,3 @@
1
1
  module Doraemon
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doraemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhuoyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-02 00:00:00.000000000 Z
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler