contentfree-is_it_mobile 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +4 -0
- data/lib/is_it_mobile/rails.rb +65 -0
- data/lib/is_it_mobile.rb +42 -0
- data/spec/is_it_mobile_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +59 -0
data/VERSION.yml
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'is_it_mobile'
|
2
|
+
|
3
|
+
Mime::Type.register_alias "text/html", :mobile
|
4
|
+
|
5
|
+
# When IsItMobile::ForRails is included in a controller, a before filter is
|
6
|
+
# added that will reset the request format to the value of
|
7
|
+
# +base.mobile_format+ which defaults to :mobile. To change it to
|
8
|
+
# something else, simply add a line after the inclusion that says
|
9
|
+
# +self.mobile_format = :handheld+ or whatever you'd like it to be.
|
10
|
+
#
|
11
|
+
# It also exposes the +request_is_from_mobile?+ method to the views if
|
12
|
+
# you don't want to create multiple erb views for every page
|
13
|
+
|
14
|
+
module IsItMobile::ForRails
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
include ::IsItMobile::ForRails::InstanceMethods
|
18
|
+
|
19
|
+
class_inheritable_accessor :mobile_format
|
20
|
+
self.mobile_format = :mobile
|
21
|
+
|
22
|
+
before_filter :change_request_format_to_mobile, :if => :request_is_from_mobile?
|
23
|
+
|
24
|
+
helper_method :request_is_from_mobile?, :mobile?
|
25
|
+
%w(iphone android blackberry windows_mobile symbian).each do |device|
|
26
|
+
helper_method "request_is_from_#{device}?".to_sym, "#{device}?".to_sym
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
protected
|
33
|
+
# Checks if the request.format is *already* @@mobile_format (set by the
|
34
|
+
# query string or using an extension of 'mobile' with resourceful routes)
|
35
|
+
# and if not, uses IsItMobile.mobile? to determine whether or not the
|
36
|
+
# request is from a mobile device
|
37
|
+
def request_is_from_mobile?
|
38
|
+
@request_is_from_mobile ||=
|
39
|
+
request.format.to_sym == self.class.mobile_format ||
|
40
|
+
IsItMobile.mobile?( request.env['HTTP_USER_AGENT'] || '', request.env['HTTP_ACCEPT'] || '' )
|
41
|
+
end
|
42
|
+
alias :mobile? :request_is_from_mobile?
|
43
|
+
|
44
|
+
# Sets the request format to @@mobile_format when the request is from a mobile device
|
45
|
+
def change_request_format_to_mobile
|
46
|
+
request.format = self.class.mobile_format
|
47
|
+
end
|
48
|
+
|
49
|
+
# Some helpers for popular devices - these don't (yet) wrangle the request format
|
50
|
+
# This will create methods named "request_is_from_(device)?" which are also aliased to
|
51
|
+
# just "(device)?"
|
52
|
+
%w(iphone android blackberry windows_mobile symbian).each do |device|
|
53
|
+
module_eval <<-EOD
|
54
|
+
def request_is_from_#{device}?
|
55
|
+
@request_is_from_#{device} ||= IsItMobile.#{device}?( request.env['HTTP_USER_AGENT'] )
|
56
|
+
end
|
57
|
+
alias :#{device}? :request_is_from_#{device}?
|
58
|
+
EOD
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class ActionController::Base
|
64
|
+
include ::IsItMobile::ForRails
|
65
|
+
end
|
data/lib/is_it_mobile.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class IsItMobile
|
2
|
+
POPULAR_MOBILE_USER_AGENT_BEGINNINGS = %w(
|
3
|
+
w3c acs- alav alca amoi audi avan benq bird blac blaz
|
4
|
+
brew cell cldc cmd- dang doco eric hipt inno ipaq java
|
5
|
+
jigs kddi keji leno lg-c lg-d lg-g lge- maui maxo midp
|
6
|
+
mits mmef mobi mot- moto mwbp nec- newt noki oper palm
|
7
|
+
pana pant phil play port prox qwap sage sams sany sch-
|
8
|
+
sec- send seri sgh- shar sie- siem smal smar sony sph-
|
9
|
+
symb t-mo teli tim- tosh tsm- upg1 upsi vk-v voda wap-
|
10
|
+
wapa wapi wapp wapr webc winw winw xda xda-
|
11
|
+
)
|
12
|
+
|
13
|
+
POPULAR_MOBILE_USER_AGENT_REGEX = /(mobile|up.browser|up.link|mmp|symbian|phone|midp|wap|mini|ppc;|playstation|palm|wii|nitro)/i
|
14
|
+
|
15
|
+
# Performs a few lightweight checks on the user agent to determine if it's a mobile device.
|
16
|
+
def self.mobile?( user_agent, accepts = '' )
|
17
|
+
!!( user_agent =~ POPULAR_MOBILE_USER_AGENT_REGEX ||
|
18
|
+
accepts.index('application/vnd.wap.xhtml+xml') ||
|
19
|
+
POPULAR_MOBILE_USER_AGENT_BEGINNINGS.include?(user_agent[0,4]))
|
20
|
+
end
|
21
|
+
|
22
|
+
# Some checks for some specific, popular mobile devices
|
23
|
+
def self.iphone?( user_agent )
|
24
|
+
!!(user_agent =~ /(mobile\/.+safari)/i)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.android?( user_agent )
|
28
|
+
!!(user_agent =~ /android/i)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.blackberry?( user_agent )
|
32
|
+
!!(user_agent =~ /blackberry/i)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.windows_mobile?( user_agent )
|
36
|
+
!!(user_agent =~ /windows ce/i)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.symbian?( user_agent )
|
40
|
+
!!(user_agent =~ /symbian/i)
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: contentfree-is_it_mobile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Myron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: dave.myron@contentfree.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/is_it_mobile
|
27
|
+
- lib/is_it_mobile/rails.rb
|
28
|
+
- lib/is_it_mobile.rb
|
29
|
+
- spec/is_it_mobile_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/contentfree/is_it_mobile
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --inline-source
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: TODO
|
58
|
+
test_files: []
|
59
|
+
|