lhj-tools 0.1.30 → 0.1.31
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lhj/helper/vika_config.rb +4 -11
- data/lib/lhj/helper/vika_helper.rb +255 -8
- data/lib/lhj/tools/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b6137bd64ebed91f50de3ca6d35f4c65adcfa6ab2a7e57d5553e9fd49643aa5
|
4
|
+
data.tar.gz: e8f239f0d6893d8654a10f188413bd1b0f2ea6ee7dff4bd276e5727ca89d10cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f84252797b129d329cfceddc18244253d5f3efedbcca4b9cdf6c432c532cedd4a583f93e7e658a5a7382a3c1350a3a7bad69e2b355f1e47558a45a0709fa654
|
7
|
+
data.tar.gz: 67e0baddcd638984097e5b37646d50533771130ea67313654c84fdd7a4a905646e3ff8c31b37db61203261927cd87b94802d9f0b2e9b3e101db5c484bf524a9b
|
@@ -16,20 +16,13 @@ module Lhj
|
|
16
16
|
@yaml ||= YAML.load_file(config_file)
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.
|
20
|
-
config['
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.view_id
|
24
|
-
config['view_id']
|
19
|
+
def self.authorization
|
20
|
+
config['authorization']
|
25
21
|
end
|
26
22
|
|
27
|
-
def self.
|
28
|
-
config['
|
23
|
+
def self.display_fields
|
24
|
+
config['display_fields']
|
29
25
|
end
|
30
26
|
|
31
|
-
def self.authorization
|
32
|
-
config['authorization']
|
33
|
-
end
|
34
27
|
end
|
35
28
|
end
|
@@ -6,24 +6,271 @@ module Lhj
|
|
6
6
|
# pgyer upload
|
7
7
|
class VikaHelper
|
8
8
|
|
9
|
-
|
9
|
+
QUERY_SPACES_API_URL = 'https://api.vika.cn/fusion/v1/spaces'.freeze
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
QUERY_NODES_API_URL = 'https://api.vika.cn/fusion/v1/spaces/spcid/nodes'.freeze
|
12
|
+
|
13
|
+
QUERY_NODE_DETAIL_API_URL = 'https://api.vika.cn/fusion/v1/spaces/spcid/nodes/fodid'.freeze
|
14
|
+
|
15
|
+
QUERY_VIEWS_API_URL = 'https://api.vika.cn/fusion/v1/datasheets/dstid/views'.freeze
|
16
|
+
|
17
|
+
QUERY_RECORDS_API_URL = 'https://api.vika.cn/fusion/v1/datasheets/dstid/records?viewId=viwid'.freeze
|
18
|
+
|
19
|
+
QUERY_RECORDS_FIELD_KEY_API_URL = 'https://api.vika.cn/fusion/v1/datasheets/dstid/records?viewId=viwid&fieldKey=id'.freeze
|
20
|
+
|
21
|
+
RECORD_LINK_URL = 'https://vika.cn/workbench/dstid/viwid/recid'.freeze
|
22
|
+
|
23
|
+
def self.trans_vika(note)
|
24
|
+
str = note.dup
|
25
|
+
if /#.+#/ =~ str
|
26
|
+
note.scan(/#[^#]+#/) do |m|
|
27
|
+
ma = m.match(/rec\w+/)
|
28
|
+
next unless ma && ma[0] && ma[0].length.positive?
|
29
|
+
|
30
|
+
rec_id = ma[0]
|
31
|
+
rec_value = query_with_rec_id(rec_id)
|
32
|
+
str = str.gsub(m, rec_value) if rec_value && rec_value.length.positive?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
str
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.query_with_rec_id(rec_id)
|
39
|
+
file = vika_file
|
40
|
+
if File.exist?(file)
|
41
|
+
map = YAML.load_file(file)
|
42
|
+
key = map.keys.find { |k| k =~ /#{rec_id}/ }
|
43
|
+
if key && key.length.positive?
|
44
|
+
map[key]
|
45
|
+
else
|
46
|
+
query_network_with_rec_id(rec_id)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
query_network_with_rec_id(rec_id)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.query_network_with_rec_id(rec_id)
|
54
|
+
sync
|
55
|
+
key = data_source.keys.find { |k| k =~ /#{rec_id}/ }
|
56
|
+
data_source[key]
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.sync
|
60
|
+
request_spaces
|
61
|
+
save_data_source
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.request_spaces
|
65
|
+
response = fetch_result(:spaces)
|
66
|
+
handle_spaces_response(response)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.handle_spaces_response(res)
|
70
|
+
return unless res['code'].to_i == 200
|
71
|
+
|
72
|
+
res['data']['spaces'].each do |spc|
|
73
|
+
spc_id = spc['id']
|
74
|
+
request_nodes(spc_id)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.request_nodes(spc_id)
|
79
|
+
response = fetch_result(:nodes, spc_id)
|
80
|
+
handle_nodes_response(spc_id, response)
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.handle_nodes_response(spc_id, res)
|
84
|
+
return unless res['code'].to_i == 200
|
85
|
+
|
86
|
+
res['data']['nodes'].each do |node|
|
87
|
+
fod_id = node['id']
|
88
|
+
request_node_detail(spc_id, fod_id)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.request_node_detail(spc_id, node_id)
|
93
|
+
response = fetch_result(:nodes_detail, spc_id, node_id)
|
94
|
+
handle_node_detail_response(spc_id, response)
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.handle_node_detail_response(spc_id, res)
|
98
|
+
return unless res['code'].to_i == 200
|
99
|
+
|
100
|
+
res['data']['children'].each do |node|
|
101
|
+
fod_dst_id = node['id']
|
102
|
+
sym = type_from_id(fod_dst_id)
|
103
|
+
case sym
|
104
|
+
when :fod
|
105
|
+
request_node_detail(spc_id, fod_dst_id)
|
106
|
+
when :dst
|
107
|
+
request_views(fod_dst_id)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# 区分开,使用缓存dst来操作
|
113
|
+
def self.request_views(dst_id)
|
114
|
+
response = fetch_result(:views, dst_id)
|
115
|
+
handle_views_response(dst_id, response)
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.handle_views_response(dst_id, res)
|
119
|
+
return unless res['code'].to_i == 200
|
120
|
+
|
121
|
+
res['data']['views'].each do |viw|
|
122
|
+
viw_id = viw['id']
|
123
|
+
request_records(dst_id, viw_id)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.request_records(dst_id, viw_id)
|
128
|
+
response = fetch_result(:records_key, dst_id, viw_id)
|
129
|
+
handle_records_response(dst_id, viw_id, response)
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.handle_records_response(dst_id, viw_id, res)
|
133
|
+
return unless res['code'].to_i == 200
|
134
|
+
|
135
|
+
res['data']['records'].each do |rec|
|
136
|
+
# 处理查询记录显示
|
137
|
+
next unless rec['fields']
|
138
|
+
|
139
|
+
rec_id = rec['recordId']
|
140
|
+
rec['fields'].each do |k, v|
|
141
|
+
display_fields = Lhj::VikaConfig.display_fields
|
142
|
+
next unless display_fields.any? { |f| f.eql?(k) }
|
143
|
+
|
144
|
+
ds_key = "#{dst_id}_#{viw_id}_#{rec_id}"
|
145
|
+
ds_value = "#{v}[#{link_url(dst_id, viw_id, rec_id)}]"
|
146
|
+
data_source[ds_key] = ds_value
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def self.link_url(dst_id, viw_id, rec_id)
|
152
|
+
url_with_kind(:link, dst_id, viw_id, rec_id)
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.data_source
|
156
|
+
@data_source ||= {}
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.save_data_source
|
160
|
+
File.write(vika_file, data_source.to_yaml)
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.vika_file
|
164
|
+
file_with_name(vika_file_name)
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.vika_file_name
|
168
|
+
'vika_data_source.yml'
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.file_with_name(file_name)
|
172
|
+
target_folder = File.join(Lhj::Config.instance.home_dir, 'vika')
|
173
|
+
FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
|
174
|
+
File.join(target_folder, file_name)
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.fetch_result(kind, *params)
|
178
|
+
url = url_with_kind(kind, *params)
|
179
|
+
res = nil
|
180
|
+
res = get_with_url(url) if url.length.positive?
|
181
|
+
res
|
182
|
+
end
|
183
|
+
|
184
|
+
def self.url_with_kind(kind, *params)
|
185
|
+
url = ''
|
186
|
+
case kind
|
187
|
+
when :spaces
|
188
|
+
url = url_with_param(QUERY_SPACES_API_URL, *params)
|
189
|
+
when :nodes
|
190
|
+
url = url_with_param(QUERY_NODES_API_URL, *params)
|
191
|
+
when :nodes_detail
|
192
|
+
url = url_with_param(QUERY_NODE_DETAIL_API_URL, *params)
|
193
|
+
when :views
|
194
|
+
url = url_with_param(QUERY_VIEWS_API_URL, *params)
|
195
|
+
when :records
|
196
|
+
url = url_with_param(QUERY_RECORDS_API_URL, *params)
|
197
|
+
when :records_key
|
198
|
+
url = url_with_param(QUERY_RECORDS_FIELD_KEY_API_URL, *params)
|
199
|
+
when :link
|
200
|
+
url = url_with_param(RECORD_LINK_URL, *params)
|
201
|
+
end
|
202
|
+
url
|
203
|
+
end
|
204
|
+
|
205
|
+
def self.url_with_param(url, *params)
|
206
|
+
handle_url = url.dup
|
207
|
+
params.each do |p|
|
208
|
+
sym = type_from_id(p)
|
209
|
+
case sym
|
210
|
+
when :spc
|
211
|
+
handle_url.gsub!('spcid', p)
|
212
|
+
when :fod
|
213
|
+
handle_url.gsub!('fodid', p)
|
214
|
+
when :dst
|
215
|
+
handle_url.gsub!('dstid', p)
|
216
|
+
when :viw
|
217
|
+
handle_url.gsub!('viwid', p)
|
218
|
+
when :rec
|
219
|
+
handle_url.gsub!('recid', p)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
handle_url
|
223
|
+
end
|
224
|
+
|
225
|
+
def self.get_with_url(api_url)
|
226
|
+
url = URI(api_url)
|
13
227
|
|
14
228
|
https = Net::HTTP.new(url.host, url.port)
|
15
229
|
https.use_ssl = true
|
16
230
|
|
17
231
|
request = Net::HTTP::Get.new(url)
|
18
|
-
|
232
|
+
auth_str = "Bearer #{Lhj::VikaConfig.authorization}"
|
233
|
+
request['Authorization'] = auth_str
|
19
234
|
|
20
235
|
response = https.request(request)
|
21
|
-
|
236
|
+
JSON.parse(response.read_body)
|
237
|
+
end
|
238
|
+
|
239
|
+
def self.type_from_id(id_str)
|
240
|
+
t = :spc
|
241
|
+
if spc_id? id_str
|
242
|
+
t = :spc
|
243
|
+
elsif fod_id? id_str
|
244
|
+
t = :fod
|
245
|
+
elsif dst_id? id_str
|
246
|
+
t = :dst
|
247
|
+
elsif viw_id? id_str
|
248
|
+
t = :viw
|
249
|
+
elsif rec_id? id_str
|
250
|
+
t = :rec
|
251
|
+
end
|
252
|
+
t
|
22
253
|
end
|
23
254
|
|
24
|
-
def self.
|
25
|
-
|
26
|
-
puts map.to_yaml
|
255
|
+
def self.spc_id?(id_str)
|
256
|
+
/^spc/ =~ id_str
|
27
257
|
end
|
258
|
+
|
259
|
+
def self.fod_id?(id_str)
|
260
|
+
/^fod/ =~ id_str
|
261
|
+
end
|
262
|
+
|
263
|
+
def self.dst_id?(id_str)
|
264
|
+
/^dst/ =~ id_str
|
265
|
+
end
|
266
|
+
|
267
|
+
def self.viw_id?(id_str)
|
268
|
+
/^viw/ =~ id_str
|
269
|
+
end
|
270
|
+
|
271
|
+
def self.rec_id?(id_str)
|
272
|
+
/^rec/ =~ id_str
|
273
|
+
end
|
274
|
+
|
28
275
|
end
|
29
276
|
end
|
data/lib/lhj/tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhj-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.31
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lihaijian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: claide
|