SDKs
Ruby
Thunderbit Open API のための Ruby イディオムなパターン
Net::HTTP(組み込み)か、ミドルウェア風のリトライ・タイムアウトが欲しいなら Faraday を使います。どちらでも動きますが、以下では Net::HTTP を使います。
Configure
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)
endページを Distill する
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 ワーカーで 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
endRails コントローラ側で Webhook 署名を検証してください —— Webhooks を参照。
公式 Ruby SDK は開発中です —— もう少しお待ちください。