Sha256: 331eb1973ce779261734f4e070f6a8a44c12b4d5317441b396f7cf8ff44b9723

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module TranslationEngine
  class CatcherMiddleware
    include Request

    REMOVE_QUERY = /\?.*/
    REPLACE_IDS = /\d+/

    def initialize(app)
      @app = app
    end

    def call(env)
      if TranslationEngine.use_catcher
        call_catcher(env)
      else
        @app.call(env)
      end
    end

    private

    def call_catcher(env)
      TranslationEngine::Translation.clear_catched

      if env['QUERY_STRING'].include?('translation_release')
        I18n.backend.release = params(env)['translation_release']
      end

      update_translations unless assets_request?(env)

      response = @app.call(env)

      send_translations(env)

      response
    end

    def assets_request?(env)
      env['PATH_INFO'] =~ /\/assets/
    end

    def params(env)
      Rack::Utils.parse_query(env['QUERY_STRING'], '&')
    end

    def update_translations
      translation_downloader.update
    end

    def send_translations(env)
      return if TranslationEngine::Translation.catched.empty?

      location = env['PATH_INFO'].gsub(REMOVE_QUERY, '').gsub(REPLACE_IDS, ':id')

      data = {
        location:     location,
        locale:       I18n.locale,
        translations: TranslationEngine::Translation.catched.uniq
      }

      Thread.new { TranslationEngine::Connection.new.send_translations(data, remote_ip(env)) }
    end

    def translation_downloader
      @translation_downloader ||= TranslationEngine::Downloader.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
translation_engine-0.0.4 app/middlewares/translation_engine/catcher_middleware.rb
translation_engine-0.0.3 app/middlewares/translation_engine/catcher_middleware.rb