Class: Lhj::TbHelper

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

Overview

tb upload

Constant Summary collapse

SEARCH_API_URL =
'https://open.teambition.com/api/task/tqlsearch'.freeze

Class Method Summary collapse

Class Method Details

.req_with_task(id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lhj/helper/tb_helper.rb', line 33

def self.req_with_task(id)
  now = Time.now.to_i
  payload = {
    _appId: Lhj::TBConfig.tb_app_id,
    iat: now,
    exp: now + 3600
  }
  token = JWT.encode(payload, Lhj::TBConfig.tb_app_key)

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

  request = Net::HTTP::Post.new(url)
  request['authorization'] = "Bearer #{token}"
  request['x-tenant-id'] = Lhj::TBConfig.tb_tenant_id
  request['x-tenant-type'] = 'organization'
  request['Content-Type'] = 'application/json'
  req_body = {
    "tql": "projectId=#{Lhj::TBConfig.projectId} AND uniqueId=#{id}"
  }
  request.body = JSON.dump(req_body)

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

.trans_tb(note) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lhj/helper/tb_helper.rb', line 13

def self.trans_tb(note)
  str = note
  if /#.+#/ =~ note
    note.scan(/#[^#]+#/) do |m|
      rec_reg = m.match(/rec\w+/)
      next if rec_reg

      ma = m.match(/\d+/)
      next if !ma || !ma[0] || ma[0].length <= 0

      res_body = req_with_task(ma[0])
      if res_body && res_body['code'].to_i == 200
        url_str = "[#{res_body['result'][0]['content']}](https://www.teambition.com/task/#{res_body['result'][0]['taskId']})"
        str = str.gsub(m, url_str)
      end
    end
  end
  str
end