Class: Lhj::VikaHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/vika_helper.rb

Overview

pgyer upload

Constant Summary collapse

QUERY_SPACES_API_URL =
'https://api.vika.cn/fusion/v1/spaces'.freeze
QUERY_NODES_API_URL =
'https://api.vika.cn/fusion/v1/spaces/spcid/nodes'.freeze
QUERY_NODE_DETAIL_API_URL =
'https://api.vika.cn/fusion/v1/spaces/spcid/nodes/fodid'.freeze
QUERY_VIEWS_API_URL =
'https://api.vika.cn/fusion/v1/datasheets/dstid/views'.freeze
QUERY_RECORDS_API_URL =
'https://api.vika.cn/fusion/v1/datasheets/dstid/records?viewId=viwid'.freeze
QUERY_RECORDS_FIELD_KEY_API_URL =
'https://api.vika.cn/fusion/v1/datasheets/dstid/records?viewId=viwid&fieldKey=id&pageSize=page_size&pageNum=page_num'.freeze
'https://vika.cn/workbench/dstid/viwid/recid'.freeze
QUERY_RECORDS_PAGE_SIZE =
100

Class Method Summary collapse

Class Method Details

.data_sourceObject



162
163
164
# File 'lib/lhj/helper/vika_helper.rb', line 162

def self.data_source
  @data_source ||= {}
end

.dst_id?(id_str) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/lhj/helper/vika_helper.rb', line 270

def self.dst_id?(id_str)
  /^dst/ =~ id_str
end

.fetch_result(kind, *params) ⇒ Object



184
185
186
187
188
189
# File 'lib/lhj/helper/vika_helper.rb', line 184

def self.fetch_result(kind, *params)
  url = url_with_kind(kind, *params)
  res = nil
  res = get_with_url(url) if url.length.positive?
  res
end

.file_with_name(file_name) ⇒ Object



178
179
180
181
182
# File 'lib/lhj/helper/vika_helper.rb', line 178

def self.file_with_name(file_name)
  target_folder = File.join(Lhj::Config.instance.home_dir, 'vika')
  FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
  File.join(target_folder, file_name)
end

.fod_id?(id_str) ⇒ Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/lhj/helper/vika_helper.rb', line 266

def self.fod_id?(id_str)
  /^fod/ =~ id_str
end

.get_with_url(api_url) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/lhj/helper/vika_helper.rb', line 232

def self.get_with_url(api_url)
  url = URI(api_url)

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true

  request = Net::HTTP::Get.new(url)
  auth_str = "Bearer #{Lhj::VikaConfig.authorization}"
  request['Authorization'] = auth_str

  response = https.request(request)
  JSON.parse(response.read_body)
end

.handle_node_detail_response(spc_id, res) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lhj/helper/vika_helper.rb', line 99

def self.handle_node_detail_response(spc_id, res)
  return unless res['code'].to_i == 200

  res['data']['children'].each do |node|
    fod_dst_id = node['id']
    sym = type_from_id(fod_dst_id)
    case sym
    when :fod
      request_node_detail(spc_id, fod_dst_id)
    when :dst
      request_views(fod_dst_id)
    end
  end
end

.handle_nodes_response(spc_id, res) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/lhj/helper/vika_helper.rb', line 85

def self.handle_nodes_response(spc_id, res)
  return unless res['code'].to_i == 200

  res['data']['nodes'].each do |node|
    fod_id = node['id']
    request_node_detail(spc_id, fod_id)
  end
end

.handle_records_response(dst_id, viw_id, res) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lhj/helper/vika_helper.rb', line 138

def self.handle_records_response(dst_id, viw_id, res)
  return unless res['code'].to_i == 200

  res['data']['records'].each do |rec|
    # 处理查询记录显示
    next unless rec['fields']

    rec_id = rec['recordId']
    rec['fields'].each do |k, v|
      display_fields = Lhj::VikaConfig.display_fields
      next unless display_fields.any? { |f| f.eql?(k) }

      ds_key = "#{dst_id}_#{viw_id}_#{rec_id}"
      ds_value = "[#{v}](#{link_url(dst_id, viw_id, rec_id)})"
      data_source[ds_key] = ds_value
    end
  end
  res['data']['pageSize'].to_i
end

.handle_spaces_response(res) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/lhj/helper/vika_helper.rb', line 71

def self.handle_spaces_response(res)
  return unless res['code'].to_i == 200

  res['data']['spaces'].each do |spc|
    spc_id = spc['id']
    request_nodes(spc_id)
  end
end

.handle_views_response(dst_id, res) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/lhj/helper/vika_helper.rb', line 120

def self.handle_views_response(dst_id, res)
  return unless res['code'].to_i == 200

  res['data']['views'].each do |viw|
    viw_id = viw['id']
    request_records(1, dst_id, viw_id)
  end
end


158
159
160
# File 'lib/lhj/helper/vika_helper.rb', line 158

def self.link_url(dst_id, viw_id, rec_id)
  url_with_kind(:link, dst_id, viw_id, rec_id)
end

.query_network_with_rec_id(rec_id) ⇒ Object



55
56
57
58
59
# File 'lib/lhj/helper/vika_helper.rb', line 55

def self.query_network_with_rec_id(rec_id)
  sync
  key = data_source.keys.find { |k| k =~ /#{rec_id}/ }
  data_source[key]
end

.query_with_rec_id(rec_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lhj/helper/vika_helper.rb', line 40

def self.query_with_rec_id(rec_id)
  file = vika_file
  if File.exist?(file)
    map = YAML.load_file(file)
    key = map.keys.find { |k| k =~ /#{rec_id}/ }
    if key && key.length.positive?
      map[key]
    else
      query_network_with_rec_id(rec_id)
    end
  else
    query_network_with_rec_id(rec_id)
  end
end

.rec_id?(id_str) ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/lhj/helper/vika_helper.rb', line 278

def self.rec_id?(id_str)
  /^rec/ =~ id_str
end

.request_node_detail(spc_id, node_id) ⇒ Object



94
95
96
97
# File 'lib/lhj/helper/vika_helper.rb', line 94

def self.request_node_detail(spc_id, node_id)
  response = fetch_result(:nodes_detail, spc_id, node_id)
  handle_node_detail_response(spc_id, response)
end

.request_nodes(spc_id) ⇒ Object



80
81
82
83
# File 'lib/lhj/helper/vika_helper.rb', line 80

def self.request_nodes(spc_id)
  response = fetch_result(:nodes, spc_id)
  handle_nodes_response(spc_id, response)
end

.request_records(page_num, dst_id, viw_id) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/lhj/helper/vika_helper.rb', line 129

def self.request_records(page_num, dst_id, viw_id)
  url = url_with_kind(:records_key, dst_id, viw_id)
  url = url.gsub('page_size', QUERY_RECORDS_PAGE_SIZE.to_s)
  url = url.gsub('page_num', page_num.to_s)
  response = get_with_url(url)
  page_size = handle_records_response(dst_id, viw_id, response)
  request_records(page_num + 1, dst_id, viw_id) if page_size == QUERY_RECORDS_PAGE_SIZE
end

.request_spacesObject



66
67
68
69
# File 'lib/lhj/helper/vika_helper.rb', line 66

def self.request_spaces
  response = fetch_result(:spaces)
  handle_spaces_response(response)
end

.request_views(dst_id) ⇒ Object

区分开,使用缓存dst来操作



115
116
117
118
# File 'lib/lhj/helper/vika_helper.rb', line 115

def self.request_views(dst_id)
  response = fetch_result(:views, dst_id)
  handle_views_response(dst_id, response)
end

.save_data_sourceObject



166
167
168
# File 'lib/lhj/helper/vika_helper.rb', line 166

def self.save_data_source
  File.write(vika_file, data_source.to_yaml)
end

.spc_id?(id_str) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/lhj/helper/vika_helper.rb', line 262

def self.spc_id?(id_str)
  /^spc/ =~ id_str
end

.syncObject



61
62
63
64
# File 'lib/lhj/helper/vika_helper.rb', line 61

def self.sync
  request_spaces
  save_data_source
end

.trans_vika(note) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lhj/helper/vika_helper.rb', line 25

def self.trans_vika(note)
  str = note.dup
  if /#.+#/ =~ str
    note.scan(/#[^#]+#/) do |m|
      ma = m.match(/rec\w+/)
      next unless ma && ma[0] && ma[0].length.positive?

      rec_id = ma[0]
      rec_value = query_with_rec_id(rec_id)
      str = str.gsub(m, rec_value) if rec_value && rec_value.length.positive?
    end
  end
  str
end

.type_from_id(id_str) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/lhj/helper/vika_helper.rb', line 246

def self.type_from_id(id_str)
  t = :spc
  if spc_id? id_str
    t = :spc
  elsif fod_id? id_str
    t = :fod
  elsif dst_id? id_str
    t = :dst
  elsif viw_id? id_str
    t = :viw
  elsif rec_id? id_str
    t = :rec
  end
  t
end

.url_with_kind(kind, *params) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lhj/helper/vika_helper.rb', line 191

def self.url_with_kind(kind, *params)
  url = ''
  case kind
  when :spaces
    url = url_with_param(QUERY_SPACES_API_URL, *params)
  when :nodes
    url = url_with_param(QUERY_NODES_API_URL, *params)
  when :nodes_detail
    url = url_with_param(QUERY_NODE_DETAIL_API_URL, *params)
  when :views
    url = url_with_param(QUERY_VIEWS_API_URL, *params)
  when :records
    url = url_with_param(QUERY_RECORDS_API_URL, *params)
  when :records_key
    url = url_with_param(QUERY_RECORDS_FIELD_KEY_API_URL, *params)
  when :link
    url = url_with_param(RECORD_LINK_URL, *params)
  end
  url
end

.url_with_param(url, *params) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/lhj/helper/vika_helper.rb', line 212

def self.url_with_param(url, *params)
  handle_url = url.dup
  params.each do |p|
    sym = type_from_id(p)
    case sym
    when :spc
      handle_url.gsub!('spcid', p)
    when :fod
      handle_url.gsub!('fodid', p)
    when :dst
      handle_url.gsub!('dstid', p)
    when :viw
      handle_url.gsub!('viwid', p)
    when :rec
      handle_url.gsub!('recid', p)
    end
  end
  handle_url
end

.vika_fileObject



170
171
172
# File 'lib/lhj/helper/vika_helper.rb', line 170

def self.vika_file
  file_with_name(vika_file_name)
end

.vika_file_nameObject



174
175
176
# File 'lib/lhj/helper/vika_helper.rb', line 174

def self.vika_file_name
  'vika_data_source.yml'
end

.viw_id?(id_str) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/lhj/helper/vika_helper.rb', line 274

def self.viw_id?(id_str)
  /^viw/ =~ id_str
end