SDKs

Ruby

Padrões idiomáticos de Ruby para a Thunderbit Open API

Use Net::HTTP (built-in) ou Faraday para retries e timeouts no estilo middleware. Ambos funcionam — Net::HTTP mostrado abaixo.

Configurar

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 de uma página

result = post('/distill', { url: 'https://thunderbit.com/playground' })
puts result['data']['markdown']

Extract de dados estruturados

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

Para fan-out assíncrono, enfileire as submissões e deixe os workers do Sidekiq lidarem com os callbacks de 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

Verifique a assinatura do Webhook no seu controller Rails — veja Webhooks.

Um SDK Ruby oficial está em desenvolvimento — volte em breve.