Class: Wovnrb::Interceptor

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

Instance Method Summary collapse

Constructor Details

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



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

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

Instance Method Details

#call(env) ⇒ Object



24
25
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
# File 'lib/wovnrb.rb', line 24

def call(env)
  # disabled by previous Rack middleware
  return @app.call(env) if Rack::Request.new(env).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)
  default_lang = @store.settings['default_lang']
  return @app.call(env) if @store.settings['test_mode'] && @store.settings['test_url'] != headers.url

  # redirect if the path is set to the default language (for SEO purposes)
  if headers.path_lang == default_lang
    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 /html/.match?(res_headers['Content-Type'])

  request = Rack::Request.new(env)

  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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wovnrb.rb', line 70

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)

    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