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.



22
23
24
25
26
27
28
# File 'lib/wovnrb.rb', line 22

def initialize(app, opts={})
  @app = app
  @store = Store.instance
  opts = opts.each_with_object({}){|(k,v),memo| memo[k.to_s]=v}
  @store.settings(opts)
  CacheBase.set_single(@store.settings)
end

Instance Method Details

#call(env) ⇒ Object



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
79
80
# File 'lib/wovnrb.rb', line 30

def call(env)
  unless Store.instance.valid_settings?
    return @app.call(env)
  end
  @env = env
  headers = Headers.new(env, @store.settings)
  if @store.settings['test_mode'] && @store.settings['test_url'] != headers.url
    return @app.call(env)
  end
  #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
  lang = headers.lang_code

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

  if res_headers["Content-Type"] =~ /html/

    request = Rack::Request.new(env)
    unless request.params['wovn_disable'] == true
      unless @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)}
        # If the user defines a token for this request, use it instead of the config token
        if @store.valid_token?(request.params['wovn_token'])
          @store.settings['project_token'] = request.params['wovn_token']
        end
        # ApiData creates request for external server, but cannot use async.
        # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
        api_data = ApiData.new(headers.redis_url, @store)
        values = api_data.get_data
        url = {
          :protocol => headers.protocol,
          :host => headers.host,
          :pathname => headers.pathname
        }
        body = switch_lang(body, values, url, lang, headers) 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
      end
    end

  end

  headers.out(res_headers)
  [status, res_headers, body]
  #[status, res_headers, d.transform()]
end

#switch_lang(body, values, url, lang = , headers) ⇒ Object



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
107
108
109
110
111
112
113
# File 'lib/wovnrb.rb', line 82

def switch_lang(body, values, url, lang=@store.settings['default_lang'], headers)
  lang = Lang.new(lang)
  ignore_all = false
  new_body = []
  body.each do |b|
    # temporarily remove noscripts
    noscripts = []
    b_without_noscripts = b
    b.scan /<noscript.*?>.*?<\/noscript>/m do |match|
      noscript_identifier = "<noscript wovn-id=\"#{noscripts.count}\"></noscript>"
      noscripts << match
      b_without_noscripts = b_without_noscripts.sub(match, noscript_identifier)
    end
    d = Nokogiri::HTML5(b_without_noscripts)
    d.encoding = "UTF-8"

    # If this page has wovn-ignore in the html tag, don't do anything
    if ignore_all || !d.xpath('//html[@wovn-ignore]').empty? || is_amp_page?(d)
      ignore_all = true
      output = d.to_html.gsub(/href="([^"]*)"/) { |m| "href=\"#{URI.decode($1)}\"" }
      put_back_noscripts!(output, noscripts)
      new_body.push(output)
      next
    end

    output = lang.switch_dom_lang(d, @store, values, url, headers)
    put_back_noscripts!(output, noscripts)
    new_body.push(output)
  end
  body.close if body.respond_to?(:close)
  new_body
end