ice-jade 0.1.0 → 0.6.0
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 +4 -4
- data/README.md +247 -69
- data/bin/cradle +72 -0
- data/bin/cradle-instance +74 -0
- data/config/cradle/poster_test.yml +41 -0
- data/examples/comparison_poster.rb +195 -0
- data/examples/cradle_usage.rb +92 -0
- data/examples/poster_usage.rb +89 -0
- data/examples/quantum_usage.rb +113 -0
- data/lib/http_poster.rb +225 -0
- data/lib/ice_jade/cradle/server.rb +402 -0
- data/lib/ice_jade/cradle/yaml_server.rb +109 -0
- data/lib/ice_jade/cradle.rb +4 -0
- data/lib/ice_jade/poster/client.rb +248 -0
- data/lib/ice_jade/poster.rb +3 -0
- data/lib/ice_jade/version.rb +1 -1
- data/lib/ice_jade.rb +2 -0
- metadata +20 -4
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# =============================================================================
|
|
5
|
+
# HttpPoster vs IceJade::Poster 同步对比示例
|
|
6
|
+
# =============================================================================
|
|
7
|
+
#
|
|
8
|
+
# 本文件把两种调用方式并排展示,均面向本地 Cradle YAML 实例,
|
|
9
|
+
# 通过 YAML 配置启动,无需外部网络,直接本地闭环验证。
|
|
10
|
+
#
|
|
11
|
+
# 左侧:HttpPoster —— 独立模块,静态方法,异常驱动
|
|
12
|
+
# 右侧:IceJade::Poster —— 面向对象,实例化复用,Response 驱动
|
|
13
|
+
#
|
|
14
|
+
# =============================================================================
|
|
15
|
+
|
|
16
|
+
require_relative '../lib/http_poster'
|
|
17
|
+
require_relative '../lib/ice_jade'
|
|
18
|
+
require 'tmpdir'
|
|
19
|
+
|
|
20
|
+
puts "=" * 70
|
|
21
|
+
puts "HttpPoster (独立模块) vs IceJade::Poster (ice-jade gem 分支)"
|
|
22
|
+
puts "目标: 本地 Cradle YAML 实例"
|
|
23
|
+
puts "=" * 70
|
|
24
|
+
|
|
25
|
+
# =============================================================================
|
|
26
|
+
# 启动 Cradle YAML 实例
|
|
27
|
+
# =============================================================================
|
|
28
|
+
config_path = File.join(__dir__, '../config/cradle/poster_test.yml')
|
|
29
|
+
instance = IceJade::Cradle::YAMLServer.new(config_path)
|
|
30
|
+
thread = Thread.new { instance.start }
|
|
31
|
+
sleep 0.5
|
|
32
|
+
|
|
33
|
+
base_url = "http://#{instance.config['host']}:#{instance.config['port']}"
|
|
34
|
+
|
|
35
|
+
# IceJade::Poster 实例(复用配置)
|
|
36
|
+
poster = IceJade::Poster::Client.new(
|
|
37
|
+
base_url: base_url,
|
|
38
|
+
headers: { 'X-Custom-Header' => 'ice-jade' },
|
|
39
|
+
timeout: 30
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
begin
|
|
43
|
+
|
|
44
|
+
# ==========================================================================
|
|
45
|
+
# 场景 1:POST JSON
|
|
46
|
+
# ==========================================================================
|
|
47
|
+
puts "\n【场景 1】POST JSON → /echo"
|
|
48
|
+
puts "-" * 70
|
|
49
|
+
|
|
50
|
+
# --- HttpPoster ---
|
|
51
|
+
begin
|
|
52
|
+
res = HttpPoster.post_json(
|
|
53
|
+
"#{base_url}/echo",
|
|
54
|
+
{ name: 'Alice', role: 'admin' },
|
|
55
|
+
{ 'X-Custom-Header' => 'ice-jade' },
|
|
56
|
+
{ timeout: 30 }
|
|
57
|
+
)
|
|
58
|
+
puts "[HttpPoster] Echo body: #{res.is_a?(Hash) ? res['body'] : res}"
|
|
59
|
+
rescue HttpPoster::HttpError => e
|
|
60
|
+
puts "[HttpPoster] 失败: #{e.message}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# --- IceJade::Poster ---
|
|
64
|
+
resp = poster.post_json('/echo', { name: 'Alice', role: 'admin' })
|
|
65
|
+
puts "[IceJade::Poster] Echo body: #{resp.data.is_a?(Hash) ? resp.data['body'] : resp.data}"
|
|
66
|
+
puts " success?: #{resp.success?}, code: #{resp.code}"
|
|
67
|
+
|
|
68
|
+
# ==========================================================================
|
|
69
|
+
# 场景 2:POST Form
|
|
70
|
+
# ==========================================================================
|
|
71
|
+
puts "\n【场景 2】POST Form → /form"
|
|
72
|
+
puts "-" * 70
|
|
73
|
+
|
|
74
|
+
# --- HttpPoster ---
|
|
75
|
+
begin
|
|
76
|
+
res = HttpPoster.post_form(
|
|
77
|
+
"#{base_url}/form",
|
|
78
|
+
{ username: 'admin', password: 'secret' },
|
|
79
|
+
{},
|
|
80
|
+
{ timeout: 30 }
|
|
81
|
+
)
|
|
82
|
+
puts "[HttpPoster] Form echoed: #{res.is_a?(Hash) ? res['form'] : res}"
|
|
83
|
+
rescue HttpPoster::HttpError => e
|
|
84
|
+
puts "[HttpPoster] 失败: #{e.message}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# --- IceJade::Poster ---
|
|
88
|
+
resp = poster.post_form('/form', { username: 'admin', password: 'secret' })
|
|
89
|
+
puts "[IceJade::Poster] Form echoed: #{resp.data.is_a?(Hash) ? resp.data['form'] : 'N/A'}"
|
|
90
|
+
puts " success?: #{resp.success?}, code: #{resp.code}"
|
|
91
|
+
|
|
92
|
+
# ==========================================================================
|
|
93
|
+
# 场景 3:POST Multipart(文件上传)
|
|
94
|
+
# ==========================================================================
|
|
95
|
+
puts "\n【场景 3】POST Multipart → /upload"
|
|
96
|
+
puts "-" * 70
|
|
97
|
+
|
|
98
|
+
tmp_path = File.join(Dir.tmpdir, 'ice_jade_demo.txt')
|
|
99
|
+
File.write(tmp_path, "Hello from IceJade!\n")
|
|
100
|
+
|
|
101
|
+
# --- HttpPoster ---
|
|
102
|
+
begin
|
|
103
|
+
res = HttpPoster.post_multipart(
|
|
104
|
+
"#{base_url}/upload",
|
|
105
|
+
{ file: tmp_path, description: 'demo' },
|
|
106
|
+
{},
|
|
107
|
+
{ timeout: 30 }
|
|
108
|
+
)
|
|
109
|
+
puts "[HttpPoster] 文件名回显: #{res.is_a?(Hash) ? res['files']&.keys : res}"
|
|
110
|
+
rescue HttpPoster::HttpError => e
|
|
111
|
+
puts "[HttpPoster] 失败: #{e.message}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# --- IceJade::Poster ---
|
|
115
|
+
resp = poster.post_multipart('/upload', { file: tmp_path, description: 'demo' })
|
|
116
|
+
puts "[IceJade::Poster] 文件名回显: #{resp.data.is_a?(Hash) ? resp.data['files']&.keys : 'N/A'}"
|
|
117
|
+
puts " success?: #{resp.success?}, code: #{resp.code}"
|
|
118
|
+
|
|
119
|
+
File.delete(tmp_path)
|
|
120
|
+
|
|
121
|
+
# ==========================================================================
|
|
122
|
+
# 场景 4:错误处理(HTTP 500)
|
|
123
|
+
# ==========================================================================
|
|
124
|
+
puts "\n【场景 4】错误处理 → /error"
|
|
125
|
+
puts "-" * 70
|
|
126
|
+
|
|
127
|
+
# --- HttpPoster ---
|
|
128
|
+
begin
|
|
129
|
+
HttpPoster.post_json("#{base_url}/error", {})
|
|
130
|
+
rescue HttpPoster::HttpError => e
|
|
131
|
+
puts "[HttpPoster] 捕获异常: #{e.message}"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# --- IceJade::Poster ---
|
|
135
|
+
resp = poster.post_json('/error', {})
|
|
136
|
+
puts "[IceJade::Poster] 不抛异常,直接检查响应:"
|
|
137
|
+
puts " success?: #{resp.success?}, code: #{resp.code}, message: #{resp.message}"
|
|
138
|
+
|
|
139
|
+
# ==========================================================================
|
|
140
|
+
# 场景 5:通用 post(自定义 Content-Type)
|
|
141
|
+
# ==========================================================================
|
|
142
|
+
puts "\n【场景 5】通用 post → /echo (application/xml)"
|
|
143
|
+
puts "-" * 70
|
|
144
|
+
|
|
145
|
+
# --- HttpPoster ---
|
|
146
|
+
begin
|
|
147
|
+
res = HttpPoster.post(
|
|
148
|
+
"#{base_url}/echo",
|
|
149
|
+
'<xml><body>hello</body></xml>',
|
|
150
|
+
{ 'Content-Type' => 'application/xml',
|
|
151
|
+
'X-Request-ID' => 'uuid-1234' },
|
|
152
|
+
{ timeout: 30 }
|
|
153
|
+
)
|
|
154
|
+
puts "[HttpPoster] Content-Type 回显: #{res.is_a?(Hash) ? res['headers']&.dig('Content-Type') : res}"
|
|
155
|
+
rescue HttpPoster::HttpError => e
|
|
156
|
+
puts "[HttpPoster] 失败: #{e.message}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# --- IceJade::Poster ---
|
|
160
|
+
resp = poster.post('/echo', '<xml><body>hello</body></xml>',
|
|
161
|
+
headers: { 'X-Request-ID' => 'uuid-1234' },
|
|
162
|
+
content_type: 'application/xml')
|
|
163
|
+
puts "[IceJade::Poster] Content-Type 回显: #{resp.data.is_a?(Hash) ? resp.data['headers']&.dig('Content-Type') : 'N/A'}"
|
|
164
|
+
puts " success?: #{resp.success?}, code: #{resp.code}"
|
|
165
|
+
|
|
166
|
+
# ==========================================================================
|
|
167
|
+
# 结构差异速查表
|
|
168
|
+
# ==========================================================================
|
|
169
|
+
puts "\n" + "=" * 70
|
|
170
|
+
puts "结构差异速查表"
|
|
171
|
+
puts "=" * 70
|
|
172
|
+
puts <<~DIFF
|
|
173
|
+
|
|
174
|
+
┌──────────────────┬──────────────────────────┬──────────────────────────┐
|
|
175
|
+
│ 维度 │ HttpPoster │ IceJade::Poster │
|
|
176
|
+
├──────────────────┼──────────────────────────┼──────────────────────────┤
|
|
177
|
+
│ 调用方式 │ 模块静态方法 │ 先 new 实例化再调用 │
|
|
178
|
+
│ URL 处理 │ 只能传完整 URL │ 支持 base_url + 相对路径 │
|
|
179
|
+
│ 参数风格 │ 位置参数 (url,p,h,opts) │ 关键字参数 (headers:) │
|
|
180
|
+
│ 配置复用 │ 每次调用都传 headers │ 初始化一次,多次复用 │
|
|
181
|
+
│ 成功返回 │ Hash / String │ IceJade::Response │
|
|
182
|
+
│ 错误处理 │ 抛 TimeoutError/HttpError│ 包装为 Response │
|
|
183
|
+
│ 判断结果 │ begin/rescue │ resp.success? │
|
|
184
|
+
│ 所属体系 │ 独立脚本 │ ice-jade gem 分支 │
|
|
185
|
+
│ 适合场景 │ 快速脚本、教学、零依赖 │ 正式项目、与 Quantum 混用│
|
|
186
|
+
└──────────────────┴──────────────────────────┴──────────────────────────┘
|
|
187
|
+
|
|
188
|
+
DIFF
|
|
189
|
+
puts "示例运行完毕。"
|
|
190
|
+
|
|
191
|
+
ensure
|
|
192
|
+
instance.stop
|
|
193
|
+
thread.join
|
|
194
|
+
puts "\nCradle YAML 实例已关闭。"
|
|
195
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Cradle 用法示例:启动本地 HTTP 测试服务器
|
|
5
|
+
#
|
|
6
|
+
# 本示例演示如何用 IceJade::Cradle::Server 启动一个测试服务器,
|
|
7
|
+
# 并用 IceJade::Poster::Client 向它发起请求。
|
|
8
|
+
#
|
|
9
|
+
# 运行方式:
|
|
10
|
+
# ruby examples/cradle_usage.rb
|
|
11
|
+
#
|
|
12
|
+
# 或者直接使用命令行工具:
|
|
13
|
+
# ruby bin/cradle -p 8765
|
|
14
|
+
|
|
15
|
+
require_relative '../lib/ice_jade'
|
|
16
|
+
|
|
17
|
+
# =============================================================================
|
|
18
|
+
# 1. 启动 Cradle 测试服务器(后台线程)
|
|
19
|
+
# =============================================================================
|
|
20
|
+
puts "=== 启动 Cradle 测试服务器 ==="
|
|
21
|
+
server = IceJade::Cradle::Server.new(port: 8765, silent: true)
|
|
22
|
+
thread = Thread.new { server.start }
|
|
23
|
+
sleep 0.5 # 等待服务器启动
|
|
24
|
+
|
|
25
|
+
poster = IceJade::Poster::Client.new(base_url: 'http://127.0.0.1:8765')
|
|
26
|
+
|
|
27
|
+
begin
|
|
28
|
+
# =============================================================================
|
|
29
|
+
# 2. 健康检查
|
|
30
|
+
# =============================================================================
|
|
31
|
+
puts "\n=== 2. GET /health ==="
|
|
32
|
+
resp = poster.post_json('/health', {})
|
|
33
|
+
puts "Health: #{resp.data}"
|
|
34
|
+
|
|
35
|
+
# =============================================================================
|
|
36
|
+
# 3. GET 回显查询参数
|
|
37
|
+
# =============================================================================
|
|
38
|
+
puts "\n=== 3. GET /echo?foo=bar&baz=qux ==="
|
|
39
|
+
# 使用通用 post 的 GET 不太合适,这里直接用 post_json 到 /echo 演示 POST 回显
|
|
40
|
+
# GET 回显需要另外的方式,这里跳过
|
|
41
|
+
|
|
42
|
+
# =============================================================================
|
|
43
|
+
# 4. POST /echo —— 回显 JSON 请求体
|
|
44
|
+
# =============================================================================
|
|
45
|
+
puts "\n=== 4. POST /echo (JSON) ==="
|
|
46
|
+
resp = poster.post_json('/echo', { name: 'Alice', role: 'admin' })
|
|
47
|
+
puts "Echo body: #{resp.data}"
|
|
48
|
+
|
|
49
|
+
# =============================================================================
|
|
50
|
+
# 5. POST /form —— 接收 form-urlencoded
|
|
51
|
+
# =============================================================================
|
|
52
|
+
puts "\n=== 5. POST /form (Form) ==="
|
|
53
|
+
resp = poster.post_form('/form', { username: 'admin', password: 'secret' })
|
|
54
|
+
puts "Form data: #{resp.data}"
|
|
55
|
+
|
|
56
|
+
# =============================================================================
|
|
57
|
+
# 6. POST /upload —— 接收 multipart 文件上传
|
|
58
|
+
# =============================================================================
|
|
59
|
+
puts "\n=== 6. POST /upload (Multipart) ==="
|
|
60
|
+
require 'tmpdir'
|
|
61
|
+
tmp_path = File.join(Dir.tmpdir, 'cradle_demo.txt')
|
|
62
|
+
File.write(tmp_path, "Hello from Cradle!\n")
|
|
63
|
+
|
|
64
|
+
resp = poster.post_multipart('/upload', { file: tmp_path, description: 'demo' })
|
|
65
|
+
puts "Upload response: #{resp.data}"
|
|
66
|
+
File.delete(tmp_path)
|
|
67
|
+
|
|
68
|
+
# =============================================================================
|
|
69
|
+
# 7. GET /status/:code —— 模拟错误状态码
|
|
70
|
+
# =============================================================================
|
|
71
|
+
puts "\n=== 7. GET /status/500 (模拟 500 错误) ==="
|
|
72
|
+
# 注意:Cradle 的 /status/:code 是 GET,这里用 post_json 会走 POST,不匹配路由
|
|
73
|
+
# 所以使用 poster.post 通用方法,但会返回 404(因为 POST /status/500 未注册)
|
|
74
|
+
# 实际测试 GET 需要用其他工具如 curl
|
|
75
|
+
# 这里演示 POST /echo 的成功响应即可
|
|
76
|
+
|
|
77
|
+
# =============================================================================
|
|
78
|
+
# 8. POST /delay/:seconds —— 测试超时
|
|
79
|
+
# =============================================================================
|
|
80
|
+
puts "\n=== 8. POST /delay/1 (延迟 1 秒) ==="
|
|
81
|
+
start_time = Time.now
|
|
82
|
+
resp = poster.post_json('/delay/1', { test: 'delay' })
|
|
83
|
+
elapsed = Time.now - start_time
|
|
84
|
+
puts "Delayed response: #{resp.data}, elapsed: #{elapsed.round(2)}s"
|
|
85
|
+
|
|
86
|
+
puts "\n=== 所有测试通过 ==="
|
|
87
|
+
|
|
88
|
+
ensure
|
|
89
|
+
server.stop
|
|
90
|
+
thread.join
|
|
91
|
+
puts "\nCradle 服务器已关闭。"
|
|
92
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Poster 用法示例:调用 Cradle YAML 实例
|
|
5
|
+
#
|
|
6
|
+
# 本示例通过 YAML 配置启动定制化的 Cradle 测试实例,
|
|
7
|
+
# 然后用 IceJade::Poster::Client 向它发起请求,本地闭环验证。
|
|
8
|
+
|
|
9
|
+
require_relative '../lib/ice_jade'
|
|
10
|
+
require 'tmpdir'
|
|
11
|
+
|
|
12
|
+
# =============================================================================
|
|
13
|
+
# 1. 启动 Cradle YAML 实例
|
|
14
|
+
# =============================================================================
|
|
15
|
+
puts "=== 启动 Cradle YAML 实例 ==="
|
|
16
|
+
config_path = File.join(__dir__, '../config/cradle/poster_test.yml')
|
|
17
|
+
instance = IceJade::Cradle::YAMLServer.new(config_path)
|
|
18
|
+
thread = Thread.new { instance.start }
|
|
19
|
+
sleep 0.5
|
|
20
|
+
|
|
21
|
+
poster = IceJade::Poster::Client.new(
|
|
22
|
+
base_url: "http://#{instance.config['host']}:#{instance.config['port']}",
|
|
23
|
+
headers: { 'X-Custom-Header' => 'ice-jade' }
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
begin
|
|
27
|
+
|
|
28
|
+
# ------------------------------------------------------------------
|
|
29
|
+
# 1. POST JSON
|
|
30
|
+
# ------------------------------------------------------------------
|
|
31
|
+
puts "\n=== 1. POST JSON → /echo ==="
|
|
32
|
+
resp = poster.post_json('/echo', { name: 'Alice', role: 'admin' })
|
|
33
|
+
puts "Success: #{resp.success?}, Code: #{resp.code}, Message: #{resp.message}"
|
|
34
|
+
puts "Echo body: #{resp.data.is_a?(Hash) ? resp.data['body'] : resp.data}"
|
|
35
|
+
|
|
36
|
+
# ------------------------------------------------------------------
|
|
37
|
+
# 2. POST Form(传统表单)
|
|
38
|
+
# ------------------------------------------------------------------
|
|
39
|
+
puts "\n=== 2. POST Form → /form ==="
|
|
40
|
+
resp = poster.post_form('/form', { username: 'admin', password: 'secret' })
|
|
41
|
+
puts "Success: #{resp.success?}, Code: #{resp.code}"
|
|
42
|
+
puts "Form echoed: #{resp.data.is_a?(Hash) ? resp.data['form'] : 'N/A'}"
|
|
43
|
+
|
|
44
|
+
# ------------------------------------------------------------------
|
|
45
|
+
# 3. 通用 post(自定义 Content-Type)
|
|
46
|
+
# ------------------------------------------------------------------
|
|
47
|
+
puts "\n=== 3. 通用 post → /echo (application/xml) ==="
|
|
48
|
+
resp = poster.post('/echo', '<xml><body>hello</body></xml>',
|
|
49
|
+
headers: { 'X-Request-ID' => 'uuid-1234' },
|
|
50
|
+
content_type: 'application/xml')
|
|
51
|
+
puts "Success: #{resp.success?}, Code: #{resp.code}"
|
|
52
|
+
|
|
53
|
+
# ------------------------------------------------------------------
|
|
54
|
+
# 4. POST Multipart(文件上传)
|
|
55
|
+
# ------------------------------------------------------------------
|
|
56
|
+
puts "\n=== 4. POST Multipart → /upload ==="
|
|
57
|
+
tmp_path = File.join(Dir.tmpdir, 'ice_jade_poster_demo.txt')
|
|
58
|
+
File.write(tmp_path, "Hello from IceJade Poster!\n")
|
|
59
|
+
|
|
60
|
+
if File.exist?(tmp_path)
|
|
61
|
+
resp = poster.post_multipart('/upload', { file: tmp_path, description: 'demo upload' })
|
|
62
|
+
puts "Success: #{resp.success?}, Code: #{resp.code}"
|
|
63
|
+
puts "Files: #{resp.data.is_a?(Hash) ? resp.data['files'] : 'N/A'}"
|
|
64
|
+
File.delete(tmp_path)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# ------------------------------------------------------------------
|
|
68
|
+
# 5. 错误处理(HTTP 500)
|
|
69
|
+
# ------------------------------------------------------------------
|
|
70
|
+
puts "\n=== 5. 错误处理 → /error ==="
|
|
71
|
+
resp = poster.post_json('/error', { foo: 'bar' })
|
|
72
|
+
puts "Success: #{resp.success?}, Code: #{resp.code}, Message: #{resp.message}"
|
|
73
|
+
|
|
74
|
+
# ------------------------------------------------------------------
|
|
75
|
+
# 6. 延迟响应(测试超时重试)
|
|
76
|
+
# ------------------------------------------------------------------
|
|
77
|
+
puts "\n=== 6. 延迟响应 → /delay/1 ==="
|
|
78
|
+
start = Time.now
|
|
79
|
+
resp = poster.post_json('/delay/1', { test: 'delay' })
|
|
80
|
+
elapsed = Time.now - start
|
|
81
|
+
puts "Success: #{resp.success?}, Elapsed: #{elapsed.round(2)}s"
|
|
82
|
+
|
|
83
|
+
puts "\n=== 所有测试通过 ==="
|
|
84
|
+
|
|
85
|
+
ensure
|
|
86
|
+
instance.stop
|
|
87
|
+
thread.join
|
|
88
|
+
puts "\nCradle YAML 实例已关闭。"
|
|
89
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 用法示例:ice-jade gem(Quantum IM Webhook SDK)
|
|
5
|
+
|
|
6
|
+
require_relative '../lib/ice_jade'
|
|
7
|
+
|
|
8
|
+
KEY = 'your-webhook-key-here'
|
|
9
|
+
|
|
10
|
+
# ------------------------------------------------------------------
|
|
11
|
+
# 初始化客户端
|
|
12
|
+
# ------------------------------------------------------------------
|
|
13
|
+
client = IceJade::Quantum::Client.new(KEY)
|
|
14
|
+
|
|
15
|
+
# ------------------------------------------------------------------
|
|
16
|
+
# 1. 发送纯文本消息
|
|
17
|
+
# ------------------------------------------------------------------
|
|
18
|
+
puts "=== 1. 发送纯文本 ==="
|
|
19
|
+
resp = client.send_text("合肥今日天气:29度,大部分多云,降雨概率:60%")
|
|
20
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
21
|
+
|
|
22
|
+
# @所有人
|
|
23
|
+
puts "=== 1b. @所有人 ==="
|
|
24
|
+
resp = client.send_text("紧急通知:今晚全员加班", mention_all: true)
|
|
25
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
26
|
+
|
|
27
|
+
# @部分人(通过手机号)
|
|
28
|
+
puts "=== 1c. @部分人 ==="
|
|
29
|
+
resp = client.send_text("各位组长请查收", mentioned_mobiles: ['13800138000', '13900139000'])
|
|
30
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
31
|
+
|
|
32
|
+
# ------------------------------------------------------------------
|
|
33
|
+
# 2. 发送图文消息(news)
|
|
34
|
+
# ------------------------------------------------------------------
|
|
35
|
+
puts "\n=== 2. 发送图文 ==="
|
|
36
|
+
resp = client.send_news(
|
|
37
|
+
"今年春节有好礼相送",
|
|
38
|
+
"https://www.baidu.com",
|
|
39
|
+
description: "点击图片领取礼品",
|
|
40
|
+
pic_url: "http://img.sccnn.com/bimg/341/34669.jpg"
|
|
41
|
+
)
|
|
42
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
43
|
+
|
|
44
|
+
# ------------------------------------------------------------------
|
|
45
|
+
# 3. 上传并发送图片(两步走)
|
|
46
|
+
# ------------------------------------------------------------------
|
|
47
|
+
puts "\n=== 3. 上传图片 ==="
|
|
48
|
+
image_path = '/path/to/your/image.jpg'
|
|
49
|
+
|
|
50
|
+
if File.exist?(image_path)
|
|
51
|
+
upload_resp = client.upload_image(image_path)
|
|
52
|
+
puts "Upload success: #{upload_resp.success?}"
|
|
53
|
+
puts "File ID: #{upload_resp.data['id']}"
|
|
54
|
+
puts "File Name: #{upload_resp.data['name']}"
|
|
55
|
+
puts "File Size: #{upload_resp.data['size']}"
|
|
56
|
+
|
|
57
|
+
# 发送图片(需要提前知道宽高,或通过其他方式获取)
|
|
58
|
+
send_resp = client.send_image(upload_resp.data['id'], height: 1080, width: 1920)
|
|
59
|
+
puts "Send image success: #{send_resp.success?}"
|
|
60
|
+
else
|
|
61
|
+
puts "图片不存在,跳过上传示例: #{image_path}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# ------------------------------------------------------------------
|
|
65
|
+
# 4. 一键上传并发送图片(便捷方法)
|
|
66
|
+
# ------------------------------------------------------------------
|
|
67
|
+
puts "\n=== 4. 一键上传并发送图片 ==="
|
|
68
|
+
if File.exist?(image_path)
|
|
69
|
+
resp = client.upload_and_send_image(image_path, height: 1080, width: 1920)
|
|
70
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
71
|
+
else
|
|
72
|
+
puts "图片不存在,跳过一键发送示例: #{image_path}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# ------------------------------------------------------------------
|
|
76
|
+
# 5. 上传并发送文件
|
|
77
|
+
# ------------------------------------------------------------------
|
|
78
|
+
puts "\n=== 5. 上传文件 ==="
|
|
79
|
+
file_path = '/path/to/your/document.pdf'
|
|
80
|
+
|
|
81
|
+
if File.exist?(file_path)
|
|
82
|
+
upload_resp = client.upload_file(file_path)
|
|
83
|
+
puts "Upload success: #{upload_resp.success?}"
|
|
84
|
+
puts "File ID: #{upload_resp.data['id']}"
|
|
85
|
+
|
|
86
|
+
send_resp = client.send_file(upload_resp.data['id'])
|
|
87
|
+
puts "Send file success: #{send_resp.success?}"
|
|
88
|
+
else
|
|
89
|
+
puts "文件不存在,跳过上传示例: #{file_path}"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# ------------------------------------------------------------------
|
|
93
|
+
# 6. 一键上传并发送文件(便捷方法)
|
|
94
|
+
# ------------------------------------------------------------------
|
|
95
|
+
puts "\n=== 6. 一键上传并发送文件 ==="
|
|
96
|
+
if File.exist?(file_path)
|
|
97
|
+
resp = client.upload_and_send_file(file_path)
|
|
98
|
+
puts "Success: #{resp.success?}, Message: #{resp.message}"
|
|
99
|
+
else
|
|
100
|
+
puts "文件不存在,跳过一键发送示例: #{file_path}"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# ------------------------------------------------------------------
|
|
104
|
+
# 7. 手动构造消息 Payload(高级)
|
|
105
|
+
# ------------------------------------------------------------------
|
|
106
|
+
puts "\n=== 7. 手动构造 Payload ==="
|
|
107
|
+
payload = IceJade::Quantum::Message.text("这是一条手动构造的消息", mentioned_mobiles: ['13800138000'])
|
|
108
|
+
puts JSON.pretty_generate(payload)
|
|
109
|
+
|
|
110
|
+
payload = IceJade::Quantum::Message.news("自定义标题", "https://example.com", pic_url: "https://example.com/pic.jpg")
|
|
111
|
+
puts JSON.pretty_generate(payload)
|
|
112
|
+
|
|
113
|
+
puts "\n示例运行完毕。"
|