SDKs
Ruby
Thunderbit Open API 的地道 Ruby 写法
用 Net::HTTP(内置)或 Faraday(中间件式重试和超时)都行 —— 下面用 Net::HTTP 演示。
配置
require 'net/http'
require 'json'
require 'uri'
API = 'https://openapi.thunderbit.com/openapi/v1'
HEADERS = {
'Authorization' => "Bearer #{ENV.fetch('THUNDERBIT_API_KEY')}",
'Content-Type' => 'application/json'
}.freeze
def post(path, body)
uri = URI("#{API}#{path}")
res = Net::HTTP.post(uri, body.to_json, HEADERS)
raise res.body if res.code.to_i >= 400
JSON.parse(res.body)
endDistill 一个页面
result = post('/distill', { url: 'https://thunderbit.com/playground' })
puts result['data']['markdown']Extract 结构化数据
result = post('/extract', {
url: 'https://example.com/product/iphone-15-pro',
schema: {
type: 'object',
properties: {
name: { type: 'string' },
price: { type: 'number' }
},
required: %w[name price]
}
})
puts result['data']Batch + Sidekiq
异步扇出场景,把提交任务排进队列,由 Sidekiq worker 处理 webhook 回调:
class DistillBatchJob
include Sidekiq::Worker
def perform(urls)
job = post('/batch/distill', {
urls: urls,
webhook: {
url: "#{ENV['APP_HOST']}/webhooks/distill",
secret: ENV['WEBHOOK_SECRET']
}
})
Rails.logger.info "Batch submitted: #{job['data']['id']}"
end
end在 Rails controller 里校验 webhook 签名 —— 详见 Webhooks。
官方 Ruby SDK 正在开发中,敬请期待。