SDKs

Ruby

Thunderbit Open API를 위한 Ruby의 관용적인 패턴

Net::HTTP(내장) 또는 미들웨어 스타일의 retry 와 timeout 을 원한다면 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)
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
end

Rails 컨트롤러에서 Webhook 시그니처를 검증하세요 —— Webhooks 를 참고하세요.

공식 Ruby SDK 가 개발 중입니다 —— 곧 다시 확인해 주세요.