Class: Wovnrb::Interceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/wovnrb.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Interceptor

Returns a new instance of Interceptor.



18
19
20
21
22
23
24
# File 'lib/wovnrb.rb', line 18

def initialize(app, opts = {})
  @app = app
  @store = Store.instance
  opts = opts.transform_keys(&:to_s)
  @store.update_settings(opts)
  @url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(@store)
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wovnrb.rb', line 26

def call(env)
  # disabled by previous Rack middleware
  request = Rack::Request.new(env)
  return @app.call(env) if request.params['wovn_disable'] == true

  @store.settings.clear_dynamic_settings!
  return @app.call(env) unless Store.instance.valid_settings?

  @env = env
  headers = Headers.new(env, @store.settings, @url_lang_switcher)
  default_lang = @store.settings['default_lang']
  return @app.call(env) if @store.settings['test_mode'] && @store.settings['test_url'] != headers.url

  cookie_lang = request.cookies['wovn_selected_lang']
  request_lang = headers.lang_code
  is_get_request = request.get?

  if @store.settings['use_cookie_lang'] && cookie_lang.present? && request_lang != cookie_lang && request_lang == @store.default_lang && is_get_request
    redirect_headers = headers.redirect(cookie_lang)
    return [302, redirect_headers, ['']]
  end

  # redirect if the path is set to the default language (for SEO purposes)
  if explicit_default_lang?(headers) && is_get_request
    redirect_headers = headers.redirect(default_lang)
    return [307, redirect_headers, ['']]
  end

  # if path containing language code is ignored, do nothing
  if headers.lang_code != default_lang && ignore_path?(headers.unmasked_pathname_without_trailing_slash)
    status, res_headers, body = @app.call(env)

    return output(headers, status, res_headers, body)
  end
  # pass to application
  status, res_headers, body = @app.call(headers.request_out)

  # disabled by next Rack middleware
  return output(headers, status, res_headers, body) unless res_headers['Content-Type']&.include?('html')

  return output(headers, status, res_headers, body) if request.params['wovn_disable'] == true

  @store.settings.update_dynamic_settings!(request.params)
  return output(headers, status, res_headers, body) if ignore_path?(headers.pathname)

  body = switch_lang(headers, body) unless /^1|302/.match?(status.to_s)

  content_length = 0
  body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
  res_headers['Content-Length'] = content_length.to_s

  output(headers, status, res_headers, body)
end

#switch_lang(headers, body) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wovnrb.rb', line 80

def switch_lang(headers, body)
  translated_body = []

  # Must use `.each` for to support multiple-chunks in Sinatra
  string_body = ''
  body.each { |chunk| string_body += chunk }
  html_body = Helpers::NokogumboHelper.parse_html(string_body)

  if !wovn_ignored?(html_body) && !amp_page?(html_body)

    html_converter = HtmlConverter.new(html_body, @store, headers, @url_lang_switcher)

    if needs_api?(html_body, headers)
      converted_html, marker = html_converter.build_api_compatible_html
      translated_content = ApiTranslator.new(@store, headers, WovnLogger.uuid).translate(converted_html)
      translated_body.push(marker.revert(translated_content))
    else
      string_body = html_converter.build if html_body.html?
      translated_body.push(string_body)
    end
  else
    translated_body.push(string_body)
  end

  body.close if body.respond_to?(:close)
  translated_body
end