Lingara Lingara 문서 학습 가이드 API 라이브러리 앱 만들기 웹 앱
언어: 한국어

Ruby

이 페이지는 영어에서 번역되었습니다. 두 내용이 다를 경우 영어 페이지가 정확합니다. 영어 페이지 읽기

Ruby 라이브러리는 RubyGems의 lingara gem입니다. Ruby 3.3 이상이 필요하며, 표준 라이브러리의 Net::HTTP를 사용하고 다른 gem은 사용하지 않습니다.

사용 방식

  • 호출은 동기식입니다. 하나의 클라이언트를 여러 스레드에서 공유할 수 있으며, fork 이후에도 올바르게 동작합니다.
  • 스트리밍 호출에 블록을 넘기면 각 이벤트가 그 블록으로 yield됩니다. 블록이 없으면 대신 Enumerable 스트림을 받는데, 이는 지연 실행되어 첫 each 또는 next에서 요청이 전송됩니다. next로 읽는 스트림은 명시적으로 close해야 합니다.
  • 이벤트는 case와 in으로 구분합니다.
  • 스트림을 일찍 멈추려면 break로 블록을 빠져나오거나 close를 호출하세요.
  • 오류는 Lingara::Error를 상속합니다. retry_after는 초 단위의 숫자이며, API가 대기 시간을 알려 주지 않았다면 nil입니다.
  • 클라이언트는 Lingara::Client.new에 키워드 인자를 넘겨 만듭니다. Net::HTTP 옵션은 net_http_options에 넣으며, 그 읽기 제한 시간이 스트림의 유휴 제한 시간이기도 합니다.

아래 샘플은 gem을 설치하고, 클라이언트를 만들고, 어휘 목록을 스트리밍하고, 오류를 처리합니다.

Ruby

gem install lingara -v 0.1.0-alpha.6

Ruby

require "lingara"

client = Lingara::Client.new(client_id: client_id, client_secret: client_secret)

Ruby

client.generate_vocabulary(level: 2, source_lang: "en", target_lang: "zh", count: 8) do |event|
  case event
  in Lingara::GenerateVocabularyEventItem => item then puts "#{item.data.word}: #{item.data.translation}"
  else nil
  end
end

Ruby

begin
  client.get_usage
rescue Lingara::ApiError => e
  # A refusal from the API: status, code (stable) and message (localised).
  puts "#{e.status} #{e.code}: #{e.message}"
  puts "retry after #{e.retry_after}s" if e.retry_after
rescue Lingara::OAuthError => e
  # The token endpoint refused the credentials or the scopes.
  puts "#{e.status} #{e.error}: #{e.description}"
rescue Lingara::MaintenanceError => e
  puts "under maintenance"
  puts "retry after #{e.retry_after}s" if e.retry_after
rescue Lingara::TransportError => e
  # No usable answer: :connect, :tls, :reset, :timeout, and so on.
  puts "transport: #{e.kind}"
end

이 페이지가 감싸는 것