Sha256: c7b4abbdf0164902073a83ca50ba95927d312f5e36ef58908876dac5929a2c67

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 KB

Contents

module Locomotive
  module Middlewares
    class Fonts

      def initialize(app, opts = {})
        @app          = app
        @path_regexp  = opts[:path] || %r{^/fonts/}
        @expires_in   = opts[:expires_in] || 24.hour # varnish
      end

      def call(env)
        if env["PATH_INFO"] =~ @path_regexp
          if Locomotive.config.multi_sites?
            site = fetch_site(env['SERVER_NAME'])
          else
            site = Site.first
          end

          if site.nil?
            @app.call(env)
          else
            body = ThemeAssetUploader.build(site, env['PATH_INFO']).read.to_s

            [200, { 'Cache-Control' => "public; max-age=#{@expires_in}" }, [body]]
          end
        else
          @app.call(env)
        end
      end

      protected

      def fetch_site(domain_name)
        site_id = Rails.cache.fetch(domain_name, :expires_in => @expires_in) do
          Site.match_domain(domain_name).only(:id).first._id.to_s rescue ''
        end

        site_id.blank? ? nil : Site.new(:id => site_id)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
tribeca_cms-0.1.1 lib/locomotive/middlewares/fonts.rb
tribeca_cms-2.0.0.rc12 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc12 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc11 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc10 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc9 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc8 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc7 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc6 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc5 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc4 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc2 lib/locomotive/middlewares/fonts.rb
locomotive_cms-2.0.0.rc1 lib/locomotive/middlewares/fonts.rb