Sha256: 7672407f79f892a4a1fe5c72f978d5b0e84f1ca0864c66af7260b3189b8485e4

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Aaalex
  module DetectMobiles
    def detect_mobiles
      case
      when is_request_from_iphone?
        request.format = :iphone
        logger.debug "detected iPhone -> request.format = :iphone"
      when is_request_from_mobile?
        request.format = :mobile
        logger.debug "detected mobile browser -> request.format = :mobile"
      end
    end

    def is_request_from_iphone?
      request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/]
    end

    def is_request_from_mobile?
      return true if request.env["HTTP_X_WAP_PROFILE"]
      return true if request.env["HTTP_ACCEPT"] && /wap\.|\.wap/ =~ request.env["HTTP_ACCEPT"]
      reg_arr =      %w{midp j2me avantg docomo novarra palmos palmsource 240x320 opwv chtml pda windows\ ce mmp\/ blackberry}
      reg_arr.concat %w{mib\/ symbian wireless nokia hand mobi phone cdm up\.b audio SIE\- SEC\- samsung HTC mot\- mitsu sagem}
      reg_arr.concat %w{sony alcatel lg eric vx NEC philips mmm xx panasonic sharp wap sch rover pocket benq java pt pg vox}
      reg_arr.concat %w{amoi bird compal kg voda sany kdd dbt sendo sgh gradi jb \d\d\di moto"}
      reg_arr.each do |reg_exp|
        return true if Regexp.new(reg_exp) =~ request.env["HTTP_USER_AGENT"]
      end
      return false
    end
  end
end

ActionController::Base.send(:include, Aaalex::DetectMobiles)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aaalex-detect_mobiles-0.0.3 lib/detect_mobiles.rb
aaalex-detect_mobiles-0.0.4 lib/detect_mobiles.rb