Skip to content

Commit

Permalink
Refactored api_data.cr into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Apr 9, 2024
1 parent a2720a3 commit c9dc390
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 80 deletions.
64 changes: 0 additions & 64 deletions src/deepl/api_data.cr

This file was deleted.

11 changes: 11 additions & 0 deletions src/deepl/document_handle.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module DeepL
class DocumentHandle
include JSON::Serializable

@[JSON::Field(key: "document_id")]
property id : String

@[JSON::Field(key: "document_key")]
property key : String
end
end
22 changes: 22 additions & 0 deletions src/deepl/document_status.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module DeepL
class DocumentStatus
include JSON::Serializable

@[JSON::Field(key: "document_id")]
property id : String
property status : String
property seconds_remaining : Int32?
property billed_characters : UInt64?
property error_message : String?

def summary : String
String.build do |s|
s << "(i) #{id}"
s << " (s) #{status}"
s << " (t) #{seconds_remaining}" if seconds_remaining
s << " (c) #{billed_characters}" if billed_characters
s << " (e) #{error_message}" if error_message
end
end
end
end
16 changes: 16 additions & 0 deletions src/deepl/glossary_info.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DeepL
class GlossaryInfo
include JSON::Serializable

property glossary_id : String
property name : String
property ready : Bool
property source_lang : String
property target_lang : String
property creation_time : String # FIXME
property entry_count : UInt32

def initialize(@glossary_id, @name, @ready, @source_lang, @target_lang, @creation_time, @entry_count)
end
end
end
8 changes: 8 additions & 0 deletions src/deepl/glossary_language_pair.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module DeepL
class GlossaryLanguagePair
include JSON::Serializable

property source_lang : String
property target_lang : String
end
end
9 changes: 9 additions & 0 deletions src/deepl/language_info.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module DeepL
class LanguageInfo
include JSON::Serializable

property language : String
property name : String
property supports_formality : Bool?
end
end
3 changes: 3 additions & 0 deletions src/deepl/text_result.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DeepL
record TextResult, text : String, detected_source_language : String
end
33 changes: 17 additions & 16 deletions src/deepl/translator.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require "json"
require "crest"
require "./exceptions"
require "./api_data"
require "./version"

# API Data

require "./text_result"
require "./document_handle"
require "./document_status"
require "./glossary_info"
require "./glossary_language_pair"
require "./language_info"

module DeepL
class Translator
DEEPL_SERVER_URL = "https://api.deepl.com/v2"
Expand Down Expand Up @@ -151,7 +159,7 @@ module DeepL
end

private def parse_translate_text_response(response) : TextResult
parse_translate_xml_response(response).first
parse_translate_xml_response(response).first # FIXME
end

private def parse_translate_xml_response(response) : Array(TextResult)
Expand Down Expand Up @@ -262,29 +270,22 @@ module DeepL
interval = 5.0,
block : (DocumentStatus ->)? = nil
)
url = "#{api_url_document}/#{handle.id}"
data = {"document_key" => handle.key}

loop do
sleep interval
document_status = translate_document_get_status(handle)

block.try &.call(document_status)

case document_status.status
when "done"
break
when "error"
raise DocumentTranslationError.new(document_status.error_message)
when "done" then break
when "error" then raise DocumentTranslationError.new(document_status.error_message)
end
end
end

def translate_document_get_status(handle : DocumentHandle) : DocumentStatus
document_id = handle.id
document_key = handle.key
url = "#{api_url_document}/#{document_id}"
data = {"document_key" => document_key}
url = "#{api_url_document}/#{handle.id}"
data = {"document_key" => handle.key}
response = Crest.post(url, form: data, headers: http_headers_json)
handle_response(response)
DocumentStatus.from_json(response.body)
Expand Down Expand Up @@ -334,14 +335,14 @@ module DeepL
entries,
entry_format = "tsv"
) : GlossaryInfo
url = "#{server_url}/glossaries"
data = {
"name" => name,
"source_lang" => source_lang,
"target_lang" => target_lang,
"entries" => entries,
"entries_format" => entry_format,
}
url = "#{server_url}/glossaries"
response = Crest.post(url, form: data, headers: http_headers_json)
handle_response(response, glossary: true)
GlossaryInfo.from_json(response.body)
Expand Down Expand Up @@ -371,9 +372,9 @@ module DeepL
response.body # Do not parse because it is a TSV
end

def get_glossary_entries_from_name(glossary_name : String) : String
def get_glossary_entries_from_name(name : String) : String
glossaries = list_glossaries
glossary = glossaries.find { |g| g.name == glossary_name }
glossary = glossaries.find { |g| g.name == name }
raise DeepLError.new("Glossary not found") unless glossary
get_glossary_entries_from_id(glossary.glossary_id)
end
Expand Down

0 comments on commit c9dc390

Please sign in to comment.