Class: Wovnrb::Interceptor
- Inherits:
-
Object
- Object
- Wovnrb::Interceptor
- Defined in:
- lib/wovnrb.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Interceptor
constructor
A new instance of Interceptor.
- #switch_lang(body, values, url, lang = , headers) ⇒ Object
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 81 82 |
# File 'lib/wovnrb.rb', line 30 def call(env) @store.settings.clear_dynamic_settings! 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) unless res_headers["Content-Type"] =~ /html/ return output(headers, status, res_headers, body) end request = Rack::Request.new(env) if request.params['wovn_disable'] == true return output(headers, status, res_headers, body) end @store.settings.update_dynamic_settings!(request.params) if @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)} return output(headers, status, res_headers, body) 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 output(headers, status, res_headers, body) end |
#switch_lang(body, values, url, lang = , headers) ⇒ Object
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 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wovnrb.rb', line 84 def switch_lang(body, values, url, lang=@store.settings['default_lang'], headers) lang = Lang.new(lang) ignore_all = false new_body = [] # generate full_body to intercept full_body = '' body.each { |b| full_body += b } [full_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 if have_data?(values) output = lang.switch_dom_lang(d, @store, values, url, headers) else ScriptReplacer.new(@store).replace(d, lang) output = d.to_html end put_back_noscripts!(output, noscripts) new_body.push(output) end body.close if body.respond_to?(:close) new_body end |