Class: Natsukantou::ChatGpt

Inherits:
Object
  • Object
show all
Includes:
UtilityBase
Defined in:
lib/natsukantou/chat_gpt.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, access_token:, translate_by_section:) ⇒ ChatGpt



17
18
19
20
21
22
23
24
25
26
# File 'lib/natsukantou/chat_gpt.rb', line 17

def initialize(
  app, access_token:,
  translate_by_section:
)
  @app = app
  @access_token = access_token

  ### Non request related setting
  @translate_by_section = translate_by_section
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



29
30
31
# File 'lib/natsukantou/chat_gpt.rb', line 29

def access_token
  @access_token
end

#envObject (readonly)

Returns the value of attribute env.



28
29
30
# File 'lib/natsukantou/chat_gpt.rb', line 28

def env
  @env
end

#translate_by_sectionObject (readonly)

Returns the value of attribute translate_by_section.



30
31
32
# File 'lib/natsukantou/chat_gpt.rb', line 30

def translate_by_section
  @translate_by_section
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/natsukantou/chat_gpt.rb', line 32

def call(env)
  @env = env

  if translate_by_section
    env[:dom].css(translate_by_section).each do |node|
      next if node.text.empty?

      translated_xml = translate(node.to_xml)
      node.replace(dom_node(translated_xml))
    end
  else
    # TODO
  end

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

  @app.call(env)
end