Class: Natsukantou::DeepL

Inherits:
Object
  • Object
show all
Includes:
UtilityBase
Defined in:
lib/natsukantou/deep_l.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Methods included from ParseXml

#dom, #dom_node

Constructor Details

#initialize(app, auth_key:, host: "https://api-free.deepl.com", version: "v2", request_params: {}) ⇒ DeepL

Returns a new instance of DeepL.

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/natsukantou/deep_l.rb', line 18

def initialize(
  app,
  auth_key:,
  host: "https://api-free.deepl.com",
  version: "v2",
  request_params: {}
)
  @app = app
  @request_params = {
    tag_handling: 'xml',
    ignore_tags: 'skip',
  }.merge(request_params)

  ::DeepL.configure do |config|
    config.auth_key = auth_key
    config.host = host
    config.version = version
  end
end

Instance Attribute Details

#request_paramsObject (readonly)

Returns the value of attribute request_params.



38
39
40
# File 'lib/natsukantou/deep_l.rb', line 38

def request_params
  @request_params
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/natsukantou/deep_l.rb', line 40

def call(env)
  result = ::DeepL.translate(
    env.dom.to_xml,
    env.lang_from.code.upcase,
    env.lang_to.code.upcase,
    request_params,
  ).to_s

  env[:dom] = dom(result)

  env[:dom].lang = env[:lang_to].code

  @app.call(env)
end