Sha256: 17690d9ab851b6bb90ccc8265ada70f9953dac1fcc5e5a02f59203cc60332753

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# DetectMobiles

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

ActionController::Base.class_eval do
  include DetectMobiles
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aaalex-detect_mobiles-0.0.2 lib/detect_mobiles.rb