Ruby
一个 Ruby 脚本,使用标准库中的 net/http 和 json 调用 API:无需安装任何 gem。它列出模板中带标签的变量,用给定的值测量所生成的图纸,并将其导出为 PDF。
下载:box.rb。
# box.rb: list a template's inputs, measure it and export it as a PDF with the PiQ SaaS API.
# Ruby 3.0 or later; net/http and json from the standard library.
require "json"
require "net/http"
KEY = ENV["PIQ_API_KEY"]
BASE = (ENV["PIQ_URL"] || "https://saas.parametriq.co.uk").chomp("/")
storage = ENV["PIQ_STORAGE"] # optional: a registered storage's name
abort "Set PIQ_API_KEY to your API key." if KEY.to_s.empty?
TEMPLATE = "L=400,\"Length\"\nW=300,\"Width\"\nP1=[L,0]\nP2=[L,W]\nP3=[0,W]\nPOLYLINE @1,[P0,P1,P2,P3],TRUE,0"
# the template itself, or (optional) where it is kept in your storage
template = storage ? { source: { storage: storage, key: "templates/box.piq" } } : { template: TEMPLATE }
values = { L: 500, W: 250 }
def post(path, body)
uri = URI(BASE + path)
response = Net::HTTP.post(uri, JSON.generate(body), "X-API-KEY" => KEY, "Content-Type" => "application/json")
return response if response.is_a?(Net::HTTPSuccess)
warn "Error #{response.code} #{response.message}"
unless response.body.to_s.empty? # a 401 has no body
problem = JSON.parse(response.body) rescue { "detail" => response.body }
warn problem["title"] if problem["title"]
warn problem["detail"] if problem["detail"]
(problem["errors"] || {}).each { |field, messages| warn " #{field}: #{messages.join(" ")}" }
end
exit 1
end
# 1. The template's labelled variables
JSON.parse(post("/inputs", template).body)["inputs"].each do |i|
puts "#{i["name"]}: #{i["label"]} (#{i["kind"]}, #{i["value"]})"
end
# 2. The drawing measured, with the values given
m = JSON.parse(post("/measure", template.merge(values: values)).body)
cut = m["layers"].find { |layer| layer["layer"] == "Cut" }&.fetch("length") || 0
puts "Size: #{m["width"]} x #{m["height"]}, cut length: #{cut}"
# 3. The drawing as a PDF
pdf = post("/export/pdf", template.merge(values: values))
File.binwrite("box.pdf", pdf.body)
puts "Saved box.pdf (#{pdf["X-PiQ-Warnings"]} warnings)"
# 4. Optional: the PDF saved to your storage instead of downloaded
if storage
destination = { storage: storage, key: "drawings/box.pdf" }
saved = JSON.parse(post("/save/pdf", template.merge(values: values, destination: destination, overwrite: true)).body)
puts "Saved #{saved["key"]} to #{saved["storage"]} (#{saved["size"]} bytes)"
end
工作原理
- 设置。 密钥来自
PIQ_API_KEY,地址来自PIQ_URL(未设置时为服务本身的地址),还可以选择从PIQ_STORAGE读取存储名称。没有密钥时,脚本会立即停止。 - 模板。
template是每个请求的起始正文:模板的文本作为template,或者在设置了PIQ_STORAGE时,以source给出模板在您存储中的保存位置。values按名称给出L和W。每个请求都用merge在其基础上添加内容。 post将正文以 JSON 形式发送到某个操作,并在X-API-KEY标头中携带密钥(对于https地址,Net::HTTP.post使用 HTTPS)。成功的响应会交回以供读取。- 错误。 任何其他响应会先打印其状态,然后打印其问题描述:其
title、detail,以及errors中的每一项,即字段及其问题。401没有正文,因此没有更多内容可打印;不是 JSON 的正文按原样打印。然后脚本以1退出。 - Inputs。
POST /inputs只需要模板。其每个inputs都有name、label、kind和value。 - Measure。 使用这些值调用
POST /measure,可得到图纸的width和height,以及在layers中按图层统计的线长;切割长度即Cut图层上的长度。 - Export。
POST /export/pdf返回 PDF 本身而不是 JSON:使用File.binwrite将其正文写入box.pdf。X-PiQ-Warnings标头表示有多少内容无法按原样导出。 - Save(可选)。 指定了存储时,
POST /save/pdf会导出同一图纸,并将其保存到您存储中的destination,overwrite: true表示替换已存在的文件。响应会说明保存位置及文件大小。
运行
将脚本保存为 box.rb,然后:
export PIQ_API_KEY=XXXX-XXXX-XXXX-XXXXX
ruby box.rb
它会打印模板中带标签的变量、图纸的尺寸和切割长度,并保存 box.pdf:
L: Length (Number, 400)
W: Width (Number, 300)
D: Depth (Number, 300)
Size: 500 x 250, cut length: 1500
Saved box.pdf (0 warnings)
如果给出模板无法使用的值(例如 "L": "long"),它会打印问题并停止:
Error 400 Bad Request
One or more validation errors occurred.
Some of the values given can't be used.
L: L is a number.
如果密钥不正确,它只会打印 Error 401 Unauthorized:401 没有问题描述。
要试用存储部分,请注册一个存储(请参阅注册存储),将模板以 templates/box.piq
放入其中,并将 PIQ_STORAGE 设置为该存储的名称。这样,模板将从您的存储中读取,
PDF 也会以 drawings/box.pdf 保存到其中,替换已存在的任何文件。