Class: Racknga::Middleware::PerUserAgentCache — racknga - Ranguba

Class: Racknga::Middleware::PerUserAgentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/racknga/middleware/cache.rb

Overview

This is a helper middleware for Racknga::Middleware::Cache.

If your Rack application provides different views to mobile user agent and PC user agent in the same URL, this middleware is useful. Your Rack application can has different caches for mobile user agent and PC user agent.

This middleware requires jpmobile.

Usage:

use Racnkga::Middleware::PerUserAgentCache
use Racnkga::Middleware::Cache, :database_path => "var/cache/db"
run YourApplication

See Also:

Instance Method Summary (collapse)

Constructor Details

- (PerUserAgentCache) initialize(application)

A new instance of PerUserAgentCache



44
45
46
# File 'lib/racknga/middleware/cache.rb', line 44

def initialize(application)
  @application = application
end

Instance Method Details

- (Object) call(environment)

For Rack.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/racknga/middleware/cache.rb', line 49

def call(environment)
  mobile = environment["rack.jpmobile"]
  if mobile
    last_component = mobile.class.name.split(/::/).last
    user_agent_key = "mobile:#{last_component.downcase}"
  else
    user_agent_key = "pc"
  end
  key = environment[Cache::KEY]
  environment[Cache::KEY] = [key, user_agent_key].join(":")
  @application.call(environment)
end