doraemon 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6525d668d265446b3581c604163d8c9920c511f5
4
- data.tar.gz: 0f77daf6ec70676072986d3fe585577f17ff3220
3
+ metadata.gz: f4584162c612105d9192e8293f9366d1629586a1
4
+ data.tar.gz: db0536b103961c1b0c560601664f4bc9e1f06047
5
5
  SHA512:
6
- metadata.gz: 0fc82b02d2b71f29f0b196cdd0bd9c56ef78d27f1ff40b9351db5eeaf45bcc8652670dbb4dbd735a5aef5f11eb5414604c69cf693fcc5f6d4f87abb5ebfc194f
7
- data.tar.gz: d7211e89f0c49007f24137a156d9570c8dc292996c92531d6f202c5b81fd8add53f508106f0a3550b3bee047da14e22ca9316101346ed2345e3a68763308463c
6
+ metadata.gz: '0483de11155a3cc361c23648e6790b19d2534d339e0c072133a27257f9e441cf5ec82b47e9f6c4fb23f93d205eb3631b53edea76826b3662233a367a3eb8bd5d'
7
+ data.tar.gz: 867456d477ae53fd923b330299e697fd971e7fabd95cab9de8f0866b0f854b6a2cef9de9f95e0e1e5aa90bb9172c26aff8775d7c7031dba34a04265d58bee26c
data/exe/doraemon CHANGED
@@ -14,10 +14,8 @@ argv = CLAide::ARGV.new(ARGV)
14
14
  port = argv.option('port', 4000)
15
15
  root = File.expand_path(argv.option('root', Dir.pwd))
16
16
 
17
- api_manager = Doraemon::APIManager.new(root)
18
-
19
17
  proxy_server = Doraemon::ProxyServer.new(port.to_i, root)
20
- proxy_server.start api_manager.load_api
18
+ proxy_server.start
21
19
 
22
20
  http_server = Doraemon::HTTPServer.new(port.to_i+1, root)
23
21
  http_server.start
data/lib/doraemon.rb CHANGED
@@ -3,7 +3,6 @@ require 'doraemon/version'
3
3
  module Doraemon
4
4
 
5
5
  require 'doraemon/utils/date'
6
- require 'doraemon/api_manager'
7
6
  require 'doraemon/http_server'
8
7
  require 'doraemon/proxy_server'
9
8
 
@@ -11,7 +11,7 @@ module Doraemon
11
11
  @root = root
12
12
  end
13
13
 
14
- def start(api_list)
14
+ def start()
15
15
 
16
16
  Cert.generate_cert
17
17
 
@@ -28,15 +28,30 @@ module Doraemon
28
28
 
29
29
  _session.on_response do |_req, _resp|
30
30
 
31
- if api_list.include?(_req.path)
31
+ puts "-----------------------------------------------------------------"
32
+ puts " - URI: #{_req.request_uri}"
32
33
 
33
- puts "- - - > #{_req.path}"
34
+ name = _req.path
35
+ if name.start_with?('/')
36
+ name = name[1, name.size - 1]
37
+ end
38
+ name = name.gsub('_', '__').gsub('/', '_')
39
+
40
+ _api_path = nil
41
+ for ext in [".rb", ".json", ".api"] do
42
+ path = File.join(@root, "#{name}#{ext}")
43
+ if File.exist?(path)
44
+ _api_path = path
45
+ break
46
+ end
47
+ end
48
+
49
+ _params = begin JSON.parse(_req.body) rescue _req.body end
50
+ _result = begin JSON.parse(_resp.body) rescue {} end
34
51
 
35
- _params = begin JSON.parse(_req.body) rescue _req.body end
36
- _result = begin JSON.parse(_resp.body) rescue {} end
52
+ if _api_path
37
53
 
38
54
  begin
39
- _api_path = File.join(@root, api_list[_req.path])
40
55
  _result = eval(File.read(_api_path)) if File.exist?(_api_path)
41
56
  rescue
42
57
  _result = {
@@ -44,15 +59,48 @@ module Doraemon
44
59
  "msg": "#{_req.path} 处理错误"
45
60
  }
46
61
  end
47
-
62
+
48
63
  _resp.status = 200
49
64
  _resp.body = _result.to_json
50
65
  _resp.header['content-length'] = _resp.body.bytesize
51
- _resp.header['content-type'] = 'application/json;charset=UTF-8'
52
-
53
- puts "#{JSON.pretty_generate(_result)}\n\n"
66
+ _resp.header['content-type'] = 'application/json; charset=utf-8'
67
+
54
68
  end
55
69
 
70
+ print_req_body = false
71
+ print_resp_body = false
72
+ req_content_type = _req.header['content-type']
73
+ ["text", "json"].each do | t |
74
+ if req_content_type.class == Array
75
+ req_content_type.each do | tt |
76
+ if tt.downcase.index(t) != nil
77
+ print_req_body = true
78
+ end
79
+ end
80
+ end
81
+ if _resp.header['content-type'].downcase.index(t) != nil
82
+ print_resp_body = true
83
+ end
84
+ end
85
+
86
+ if print_req_body
87
+ puts " - Request: #{_req.body}\n"
88
+ else
89
+ if _req.body.class != NilClass
90
+ puts " - Response: <#{_req.body.length}>"
91
+ end
92
+ end
93
+
94
+ if print_resp_body
95
+ puts " - Response: #{_resp.body}\n" if print_resp_body
96
+ else
97
+ if _resp.body.class != NilClass
98
+ puts " - Response: <#{_resp.header['content-type']}>, size #{_resp.body.length}"
99
+ end
100
+ end
101
+
102
+ puts "\n"
103
+
56
104
  end
57
105
 
58
106
  _session.start
@@ -1,3 +1,3 @@
1
1
  module Doraemon
2
- VERSION = "1.0.6"
2
+ VERSION = "1.0.7"
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.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhuoyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-14 00:00:00.000000000 Z
11
+ date: 2018-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,6 @@ files:
91
91
  - README.md
92
92
  - exe/doraemon
93
93
  - lib/doraemon.rb
94
- - lib/doraemon/api_manager.rb
95
94
  - lib/doraemon/http_server.rb
96
95
  - lib/doraemon/proxy_server.rb
97
96
  - lib/doraemon/res/image_source.json
@@ -1,29 +0,0 @@
1
- module Doraemon
2
-
3
- class APIManager
4
-
5
- def initialize(root = Dir.pwd)
6
- @root = root
7
- end
8
-
9
- def load_api
10
-
11
- accept_exts = [".rb", ".json", ".api"]
12
-
13
- api_list = {}
14
-
15
- Dir.foreach(@root) do |filename|
16
- next if !accept_exts.include? File.extname(filename).downcase
17
- api_name = "/#{File.basename(filename, '.*')}".tr('_', '/')
18
- api_name = api_name.gsub('//', '_')
19
- api_list[api_name] = filename
20
- puts "#{api_name} => #{filename}"
21
- end
22
-
23
- return api_list
24
-
25
- end
26
-
27
- end
28
-
29
- end