Sha256: a365375565779df965bdde02f1d82695049e587ceb6d913ff343ee75d24b7c3b

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

Contents

# encoding: utf-8

require 'rack'
require 'browser'
require 'mobileesp_converted'
require 'rack/body_classes/version'

module Rack
  class BodyClasses
    def initialize(app)
      @app = app
    end

    def call(env)
      browser = Browser.new(ua: env["HTTP_USER_AGENT"], accept_language: env['HTTP_ACCEPT_LANGUAGE']).meta
      device = mobile_esp(env["HTTP_USER_AGENT"], env['HTTP_ACCEPT'])
      env['rack.body_classes'] = [device, browser].flatten.compact.join(" ")
      status, headers, body = @app.call(env)
      [status, headers, body]
    end

    private

    def mobile_esp(user_agent, http_accept)
      return 'unknown-device' if user_agent.nil? || http_accept.nil?
      mobileesp = MobileESPConverted::UserAgentInfo.new(user_agent, http_accept)
      return :mobile if mobileesp.is_tier_generic_mobile || mobileesp.is_tier_iphone || mobileesp.is_tier_rich_css
      return :tablet if mobileesp.is_tier_tablet
      return :desktop
    end      
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-body_classes-1.0.2 lib/rack/body_classes.rb