Module: JPSClient::API::Tag
- Included in:
- Client
- Defined in:
- lib/jpsclient/api/tag.rb
Overview
Tag 相关 API 处理标签管理相关接口
Instance Method Summary collapse
-
#batch_create_tags(tags: []) ⇒ Array<Hash>
便捷方法:批量创建标签 注意:此方法会逐个调用 API,返回结果数组.
-
#batch_update_weights(tag_weights: {}, type:) ⇒ Hash
批量更新标签权重(便捷方法).
-
#copy_tag(tagId:, new_name:, tag_type: nil) ⇒ Hash
复制标签 注意:此功能需要先获取标签详情,然后创建新标签.
-
#create_tag(params: {}) ⇒ Hash
创建标签.
-
#delete_tag(tagId:) ⇒ Hash
删除标签.
-
#get_tag_applications(tagId:) ⇒ Array
便捷方法:获取标签的应用列表.
-
#get_tag_ideas(tagId:) ⇒ Array
便捷方法:获取标签的创意列表.
-
#get_tag_resources(tagId:) ⇒ Hash
获取标签关联的资源.
-
#get_tags(params: {}) ⇒ Hash
获取标签列表.
-
#get_tags_by_color(type:, color:) ⇒ Array<Hash>
便捷方法:按颜色筛选标签 获取指定类型的标签,然后在客户端筛选颜色.
-
#get_tags_by_type(type:, page: nil) ⇒ Hash
便捷方法:按类型获取标签.
-
#get_tags_statistics(type:) ⇒ Hash
标签统计信息 获取标签并计算统计数据.
-
#search_tags(type:, keyword:) ⇒ Array<Hash>
便捷方法:搜索标签 获取指定类型的标签,然后在客户端搜索.
-
#update_tag(params: {}) ⇒ Hash
更新标签.
-
#update_tag_weights(weights: []) ⇒ Hash
更新标签权重.
Instance Method Details
#batch_create_tags(tags: []) ⇒ Array<Hash>
便捷方法:批量创建标签注意:此方法会逐个调用 API,返回结果数组
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/jpsclient/api/tag.rb', line 145 def (tags: []) return [] if .empty? results = [] .each do |tag_params| result = create_tag(params: tag_params) results << result end results end |
#batch_update_weights(tag_weights: {}, type:) ⇒ Hash
批量更新标签权重(便捷方法)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/jpsclient/api/tag.rb', line 227 def batch_update_weights(tag_weights: {}, type:) return update_tag_weights(weights: []) if tag_weights.empty? # 先获取所有标签信息 response = (params: { type: type }) # 如果获取标签失败,直接返回错误响应 return response unless response && response['data'] = response['data']['tags'] || response['data'][:tags] || [] weights_array = [] tag_weights.each do |tag_id, weight| tag = .find { |t| (t['id'] || t[:id]).to_s == tag_id.to_s } if tag weights_array << { id: tag['id'] || tag[:id], weight: weight, name: tag['name'] || tag[:name], type: tag['type'] || tag[:type], color: tag['color'] || tag[:color], description: tag['description'] || tag[:description] } end end # 如果没有找到任何有效标签,返回空权重数组的更新(将由API处理错误) update_tag_weights(weights: weights_array) end |
#copy_tag(tagId:, new_name:, tag_type: nil) ⇒ Hash
复制标签注意:此功能需要先获取标签详情,然后创建新标签
264 265 266 267 268 |
# File 'lib/jpsclient/api/tag.rb', line 264 def copy_tag(tagId:, new_name:, tag_type: nil) # 由于没有获取单个标签详情的API,此方法暂时无法实现 # 建议使用 get_tags 获取标签列表后在客户端查找并复制 raise JPSClient::ExceptionError, "Copy tag feature requires get_tag_detail API which is not available" end |
#create_tag(params: {}) ⇒ Hash
创建标签
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jpsclient/api/tag.rb', line 15 def create_tag(params: {}) config = @request_config && @request_config["tag_create"] raise JPSClient::ExceptionError, "Missing config for tag_create" unless config && config["url"] # 参数验证 required_fields = [:name, :type] 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_tag(tagId:) ⇒ Hash
删除标签
62 63 64 65 66 67 68 69 |
# File 'lib/jpsclient/api/tag.rb', line 62 def delete_tag(tagId:) config = @request_config && @request_config["tag_delete"] raise JPSClient::ExceptionError, "Missing config for tag_delete" unless config && config["url"] path = config["url"].gsub("{tagId}", tagId.to_s) return request_with_auth(:delete, path) end |
#get_tag_applications(tagId:) ⇒ Array
便捷方法:获取标签的应用列表
199 200 201 202 203 204 205 206 |
# File 'lib/jpsclient/api/tag.rb', line 199 def get_tag_applications(tagId:) response = get_tag_resources(tagId: tagId) # 如果请求失败,返回空数组 return [] unless response && response['data'] response['data']['applications'] || response['data'][:applications] || [] end |
#get_tag_ideas(tagId:) ⇒ Array
便捷方法:获取标签的创意列表
212 213 214 215 216 217 218 219 |
# File 'lib/jpsclient/api/tag.rb', line 212 def get_tag_ideas(tagId:) response = get_tag_resources(tagId: tagId) # 如果请求失败,返回空数组 return [] unless response && response['data'] response['data']['ideas'] || response['data'][:ideas] || [] end |
#get_tag_resources(tagId:) ⇒ Hash
获取标签关联的资源
120 121 122 123 124 125 126 127 |
# File 'lib/jpsclient/api/tag.rb', line 120 def get_tag_resources(tagId:) config = @request_config && @request_config["tag_resources"] raise JPSClient::ExceptionError, "Missing config for tag_resources" unless config && config["url"] path = config["url"].gsub("{tagId}", tagId.to_s) return request_with_auth(:get, path) end |
#get_tags(params: {}) ⇒ Hash
获取标签列表
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/jpsclient/api/tag.rb', line 102 def (params: {}) config = @request_config && @request_config["tag_list"] raise JPSClient::ExceptionError, "Missing config for tag_list" unless config && config["url"] # type 参数是必需的 unless params[:type] || params["type"] raise JPSClient::ExceptionError, "Missing required parameter: type" end path = config["url"] return request_with_auth(:get, path, params: params) end |
#get_tags_by_color(type:, color:) ⇒ Array<Hash>
便捷方法:按颜色筛选标签获取指定类型的标签,然后在客户端筛选颜色
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/jpsclient/api/tag.rb', line 163 def (type:, color:) response = (params: { type: type }) # 如果请求失败,返回空数组 return [] unless response && response['data'] = response['data']['tags'] || response['data'][:tags] || [] .select { |tag| (tag['color'] || tag[:color]) == color } end |
#get_tags_by_type(type:, page: nil) ⇒ Hash
便捷方法:按类型获取标签
134 135 136 137 138 |
# File 'lib/jpsclient/api/tag.rb', line 134 def (type:, page: nil) params = { type: type } params[:page] = page if page (params: params) end |
#get_tags_statistics(type:) ⇒ Hash
标签统计信息获取标签并计算统计数据
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/jpsclient/api/tag.rb', line 275 def (type:) response = (params: { type: type }) # 如果请求失败,返回空统计 unless response && response['data'] return { total_count: 0, tags_with_resources: 0, empty_tags: 0, color_distribution: {}, top_used: [] } end = response['data']['tags'] || response['data'][:tags] || [] { total_count: .size, tags_with_resources: .count { |t| (t['relatedCount'] || t[:relatedCount] || 0) > 0 }, empty_tags: .count { |t| (t['relatedCount'] || t[:relatedCount] || 0) == 0 }, color_distribution: .group_by { |t| t['color'] || t[:color] || "no_color" }.transform_values(&:size), top_used: .sort_by { |t| -(t['relatedCount'] || t[:relatedCount] || 0) }.first(10) } end |
#search_tags(type:, keyword:) ⇒ Array<Hash>
便捷方法:搜索标签获取指定类型的标签,然后在客户端搜索
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jpsclient/api/tag.rb', line 181 def (type:, keyword:) response = (params: { type: type }) # 如果请求失败,返回空数组 return [] unless response && response['data'] = response['data']['tags'] || response['data'][:tags] || [] .select { |tag| name = (tag['name'] || tag[:name] || "").to_s description = (tag['description'] || tag[:description] || "").to_s name.include?(keyword) || description.include?(keyword) } end |
#update_tag(params: {}) ⇒ Hash
更新标签
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jpsclient/api/tag.rb', line 41 def update_tag(params: {}) config = @request_config && @request_config["tag_update"] raise JPSClient::ExceptionError, "Missing config for tag_update" unless config && config["url"] # 参数验证 required_fields = [:tagId, :name, :type] 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(:put, path, body: params) end |
#update_tag_weights(weights: []) ⇒ Hash
更新标签权重
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jpsclient/api/tag.rb', line 82 def update_tag_weights(weights: []) config = @request_config && @request_config["tag_weights"] raise JPSClient::ExceptionError, "Missing config for tag_weights" unless config && config["url"] if weights.empty? raise JPSClient::ExceptionError, "Weights array cannot be empty" end path = config["url"] params = { weights: weights } return request_with_auth(:post, path, body: params) end |