lhj-tools 0.1.30 → 0.1.31

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: d687a5f900925541da6435425f42e505ff3d80abcab177e1c5a619082da05b6a
4
- data.tar.gz: 593fcf17a101528199b43cc9905865e94d185ba687e81f2733623a99c512981c
3
+ metadata.gz: 3b6137bd64ebed91f50de3ca6d35f4c65adcfa6ab2a7e57d5553e9fd49643aa5
4
+ data.tar.gz: e8f239f0d6893d8654a10f188413bd1b0f2ea6ee7dff4bd276e5727ca89d10cb
5
5
  SHA512:
6
- metadata.gz: b4d2b8d1da73e9a0906501f26a419a7f7bde4195d428bd30b2c2e119d2c18882430e69015dacc9e88e1dc1a5c50ae986201cec584213230bc5c298bbe0331423
7
- data.tar.gz: 1f1d936c41971b3ab3214c8eea90c49cbac6931e8c4b8174a11ae083d09cab9ad4ee0b3c05f718545d9763ffd07bb4b15c67f2c750bfa8a4dd2b8d53c1610ae0
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.datasheet_id
20
- config['datasheet_id']
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.record_id
28
- config['record_id']
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
- RECORDS_API_URL = 'https://api.vika.cn/fusion/v1/datasheets/dstmDXBooTe1Wj0QqE/views'.freeze
9
+ QUERY_SPACES_API_URL = 'https://api.vika.cn/fusion/v1/spaces'.freeze
10
10
 
11
- def self.records
12
- url = URI(RECORDS_API_URL)
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
- request["Authorization"] = "Bearer usk3cvEicoXG84y32J61BRg"
232
+ auth_str = "Bearer #{Lhj::VikaConfig.authorization}"
233
+ request['Authorization'] = auth_str
19
234
 
20
235
  response = https.request(request)
21
- File.write(File.join(Lhj::Config.instance.home_dir, 'vika_all_records.json'), JSON.parse(response.read_body))
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.all_records
25
- map = {'v6.2.0' => '月底发版分支'}
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.30"
5
+ VERSION = "0.1.31"
6
6
  end
7
7
  end
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.30
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-15 00:00:00.000000000 Z
11
+ date: 2022-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide