Module: JPSClient::API::Template

Included in:
Client
Defined in:
lib/jpsclient/api/template.rb

Overview

Template 相关 API 处理模板管理相关接口

Instance Method Summary collapse

Instance Method Details

#copy_template(template_id:, new_name:) ⇒ Hash

复制模板(创建模板副本)

Parameters:

  • template_id (String, Integer)

    源模板ID

  • new_name (String)

    新模板名称

Returns:

  • (Hash)

    API响应



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jpsclient/api/template.rb', line 151

def copy_template(template_id:, new_name:)
  # 先获取原模板
  original = get_template_detail(id: template_id)

  # 如果获取失败,直接返回错误响应
  return original unless original && original['data']

  template_data = original['data']

  # 创建新模板
  create_template(params: {
    name: new_name,
    type: template_data['type'] || template_data[:type],
    content: template_data['content'] || template_data[:content],
    description: "Copy of #{template_data['name'] || template_data[:name]}: #{template_data['description'] || template_data[:description]}"
  })
end

#create_template(params: {}) ⇒ Hash

创建模板

Parameters:

  • params (Hash) (defaults to: {})

    模板信息参数

    • name: 模板名称(必需)

    • type: 模板类型(必需)

    • content: 模板内容(必需)

    • description: 模板描述(可选)

Returns:

  • (Hash)

    API响应

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jpsclient/api/template.rb', line 55

def create_template(params: {})
  config = @request_config && @request_config["template_create"]
  raise JPSClient::ExceptionError, "Missing config for template_create" unless config && config["url"]

  # 参数验证
  required_fields = [:name, :type, :content]
  missing_fields = required_fields.select { |field| params[field].nil? || params[field].to_s.empty? }

  if missing_fields.any?
    raise JPSClient::ExceptionError, "Missing required fields: #{missing_fields.join(', ')}"
  end

  path = config["url"]

  return request_with_auth(:post, path, body: params)
end

#delete_template(id:) ⇒ Hash

删除模板

Parameters:

  • id (String, Integer)

    模板ID(必需)

Returns:

  • (Hash)

    API响应

Raises:



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jpsclient/api/template.rb', line 97

def delete_template(id:)
  config = @request_config && @request_config["template_delete"]
  raise JPSClient::ExceptionError, "Missing config for template_delete" unless config && config["url"]

  path = config["url"]

  # POST请求,ID作为body参数
  params = { id: id }

  return request_with_auth(:post, path, body: params)
end

#get_template_detail(id:) ⇒ Hash

获取模板详情

Parameters:

  • id (String, Integer)

    模板ID(必需)

Returns:

  • (Hash)

    API响应

Raises:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jpsclient/api/template.rb', line 35

def get_template_detail(id:)
  config = @request_config && @request_config["template_detail"]
  raise JPSClient::ExceptionError, "Missing config for template_detail" unless config && config["url"]

  path = config["url"]

  # GET请求,ID作为查询参数
  params = { id: id }

  return request_with_auth(:get, path, params: params)
end

#get_template_list(params: {}) ⇒ Hash

获取模板列表

Parameters:

  • params (Hash) (defaults to: {})

    请求参数

    • page: 页码(默认1)

    • pageSize: 每页数量(默认10)

    • type: 模板类型(可选)

    • name: 模板名称(可选,用于搜索)

Returns:

  • (Hash)

    API响应

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jpsclient/api/template.rb', line 15

def get_template_list(params: {})
  config = @request_config && @request_config["template_list"]
  raise JPSClient::ExceptionError, "Missing config for template_list" unless config && config["url"]

  path = config["url"]

  # 设置默认值
  default_params = {
    page: 1,
    pageSize: 10
  }
  params = default_params.merge(params)

  return request_with_auth(:post, path, body: params)
end

#get_templates_by_ids(ids: []) ⇒ Array<Hash>

批量获取模板(扩展方法)注意:此方法会逐个调用 API,返回结果数组

Parameters:

  • ids (Array<String, Integer>) (defaults to: [])

    模板ID列表

Returns:

  • (Array<Hash>)

    每个请求的响应结果



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jpsclient/api/template.rb', line 114

def get_templates_by_ids(ids: [])
  return [] if ids.empty?

  results = []
  ids.each do |id|
    result = get_template_detail(id: id)
    results << result
  end

  results
end

#get_templates_by_type(type:, params: {}) ⇒ Hash

按类型获取模板列表(便捷方法)

Parameters:

  • type (String)

    模板类型(message, document, email, report, other)

  • params (Hash) (defaults to: {})

    其他查询参数

Returns:

  • (Hash)

    API响应



131
132
133
134
# File 'lib/jpsclient/api/template.rb', line 131

def get_templates_by_type(type:, params: {})
  params[:type] = type
  get_template_list(params: params)
end

#search_templates(keyword:, params: {}) ⇒ Hash

搜索模板(便捷方法)

Parameters:

  • keyword (String)

    搜索关键词

  • params (Hash) (defaults to: {})

    其他查询参数

Returns:

  • (Hash)

    API响应



141
142
143
144
# File 'lib/jpsclient/api/template.rb', line 141

def search_templates(keyword:, params: {})
  params[:name] = keyword
  get_template_list(params: params)
end

#update_template(params: {}) ⇒ Hash

更新模板

Parameters:

  • params (Hash) (defaults to: {})

    更新参数

    • id: 模板ID(必需)

    • name: 模板名称

    • type: 模板类型

    • content: 模板内容

    • description: 模板描述

Returns:

  • (Hash)

    API响应

Raises:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jpsclient/api/template.rb', line 81

def update_template(params: {})
  config = @request_config && @request_config["template_update"]
  raise JPSClient::ExceptionError, "Missing config for template_update" unless config && config["url"]

  # ID 是必需的
  raise JPSClient::ExceptionError, "Missing required field: id" unless params[:id]

  path = config["url"]

  return request_with_auth(:post, path, body: params)
end