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.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
  @store.update_settings(opts)
  CacheBase.set_single(@store.settings)
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
# File 'lib/wovnrb.rb', line 26

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

  @env = env
  headers = Headers.new(env, @store.settings)
  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 == @store.settings['default_lang']
    redirect_headers = headers.redirect(@store.settings['default_lang'])
    return [307, redirect_headers, ['']]
  end

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

  return output(headers, status, res_headers, body) unless res_headers['Content-Type'] =~ /html/

  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 @store.settings['ignore_globs'].any? { |g| g.match?(headers.pathname) }

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

  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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wovnrb.rb', line 61

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).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