is_it_iphone 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +70 -0
- data/lib/is_it_iphone.rb +24 -0
- data/test/test_is_it_iphone.rb +69 -0
- metadata +54 -0
data/README.txt
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= is_it_iphone
|
2
|
+
* http://rubyforge.org/projects/isitiphone/
|
3
|
+
|
4
|
+
== AUTHOR:
|
5
|
+
|
6
|
+
Damon Danieli (damondanieli@gmail.com)
|
7
|
+
|
8
|
+
== FEATURES/PROBLEMS:
|
9
|
+
|
10
|
+
* Fast & Lightweight
|
11
|
+
* Simple: I'm not smart enough to make something complex
|
12
|
+
|
13
|
+
== DESCRIPTION:
|
14
|
+
|
15
|
+
This gem was inspired by the IsItMobile gem done by Dave Myron.
|
16
|
+
|
17
|
+
The code to check for the iPhone user agent is from http://developer.apple.com. This doesn't have any dependencies.
|
18
|
+
|
19
|
+
- in app/controllers/application.rb
|
20
|
+
|
21
|
+
require 'is_it_iphone'
|
22
|
+
class ApplicationController < ActionController::Base
|
23
|
+
include IsItIPhone
|
24
|
+
before_filter :adjust_format_for_iphone # Always show iPhone views
|
25
|
+
end
|
26
|
+
|
27
|
+
You will have these functions:
|
28
|
+
|
29
|
+
iphone_user_agent?
|
30
|
+
Returns true if the user agent is an iPhone.
|
31
|
+
(as spec'ed on http://developer.apple.com)
|
32
|
+
|
33
|
+
iphone_request?
|
34
|
+
Returns true if the request came from an iPhone.
|
35
|
+
Override being an iPhone with ?format=xxxx in the URL.
|
36
|
+
|
37
|
+
adjust_format_for_iphone
|
38
|
+
Call when you want to show iPhone views to iPhone users.
|
39
|
+
Note: It is recommended by Apple that you default to showing
|
40
|
+
your "normal" html page to iPhone users and allow them to
|
41
|
+
choose if they want an iPhone version.
|
42
|
+
|
43
|
+
With Rails 2.0, you can use its multiview capabilities by simply adding this to your app:
|
44
|
+
|
45
|
+
- in config/initializers/mime_types.rb
|
46
|
+
|
47
|
+
Mime::Type.register_alias "text/html", :iphone
|
48
|
+
|
49
|
+
Then, just create your views using suffices of iphone.erb instead of html.erb:
|
50
|
+
|
51
|
+
index.iphone.erb
|
52
|
+
show.iphone.erb
|
53
|
+
etc.
|
54
|
+
|
55
|
+
Note: you will probably want to use a Web library specific for iPhone applications. FWIW, I use Dashcode (in the iPhone SDK) to write and debug the iPhone application and then integrate it with my Rails project.
|
56
|
+
|
57
|
+
== REQUIREMENTS:
|
58
|
+
|
59
|
+
None
|
60
|
+
|
61
|
+
== INSTALL:
|
62
|
+
|
63
|
+
sudo gem install is_it_iphone
|
64
|
+
|
65
|
+
== LICENSE:
|
66
|
+
|
67
|
+
Copyright (c) 2008
|
68
|
+
|
69
|
+
MIT Licence
|
70
|
+
|
data/lib/is_it_iphone.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Module to be included in your ApplicationController class
|
2
|
+
# See README.txt for description of how to use this
|
3
|
+
module IsItIPhone
|
4
|
+
# Returns true if the request USER AGENT came from an iPhone
|
5
|
+
# (specfications from http://developer.apple.com)
|
6
|
+
def iphone_user_agent?
|
7
|
+
!!(request.env['HTTP_USER_AGENT'] && request.env['HTTP_USER_AGENT'][/(Mobile\/.+Safari)/])
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns true if the request or format parameter came from an iPhone
|
11
|
+
def iphone_request?
|
12
|
+
params[:format] ? params[:format] == 'iphone' : iphone_user_agent?
|
13
|
+
end
|
14
|
+
|
15
|
+
# Call this as a before_filter if you always want to display
|
16
|
+
# the iPhone view (i.e., index.iphone.erb).
|
17
|
+
# Note: According to Apple iPhone Web Developer guidelines, websites
|
18
|
+
# should show the normal page and then give the user the option to see
|
19
|
+
# it formatted for their iPhone.
|
20
|
+
def adjust_format_for_iphone
|
21
|
+
request.format = :iphone if iphone_request?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '../lib/is_it_iphone.rb' )
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class IPhoneTestController
|
5
|
+
include IsItIPhone
|
6
|
+
class Request
|
7
|
+
attr_reader :env
|
8
|
+
def initialize(user_agent)
|
9
|
+
@env = { 'HTTP_USER_AGENT' => user_agent }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
def test(user_agent, format=nil)
|
13
|
+
# set up mock request object and format params
|
14
|
+
@request = Request.new(user_agent)
|
15
|
+
@params = { :format => format }
|
16
|
+
|
17
|
+
iphone_request?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def request
|
22
|
+
@request
|
23
|
+
end
|
24
|
+
def params
|
25
|
+
@params
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class TestIsItIPhone < Test::Unit::TestCase
|
30
|
+
PC_IE = "IE 7 Windows Vista: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
|
31
|
+
MAC_FIREFOX = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
|
32
|
+
MAC_SAFARI = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13"
|
33
|
+
IPHONE_1_4 = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3"
|
34
|
+
IPHONE_SIM = "Mozilla/5.0 (iPhone Simulator; U; iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.17 (KHTML, like Gecko) Version/3.1 Mobile/5A240d Safari/5525.7"
|
35
|
+
|
36
|
+
|
37
|
+
def setup
|
38
|
+
@controller = IPhoneTestController.new
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_pc
|
42
|
+
assert !@controller.test(PC_IE)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_mac_safari
|
46
|
+
assert !@controller.test(MAC_SAFARI)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_mac_firefox
|
50
|
+
assert !@controller.test(MAC_FIREFOX)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_iphone_1_4
|
54
|
+
assert @controller.test(IPHONE_1_4)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_iphone_simulator
|
58
|
+
assert @controller.test(IPHONE_SIM)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_iphone_override
|
62
|
+
assert !@controller.test(IPHONE_1_4, "json")
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_pc_override
|
66
|
+
assert @controller.test(MAC_FIREFOX, "iphone")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: is_it_iphone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damon Danieli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "The code to check for the iPhone user agent is from http://developer.apple.com. This doesn't have any dependencies. - in app/controllers/application.rb require 'is_it_iphone' class ApplicationController < ActionController::Base include IsItIPhone before_filter :adjust_format_for_iphone # Always show iPhone views end You will have these functions: iphone_user_agent? Returns true if the user agent is an iPhone. (as spec'ed on http://developer.apple.com) iphone_request? Returns true if the request came from an iPhone. Override being an iPhone with ?format=xxxx in the URL. adjust_format_for_iphone Call when you want to show iPhone views to iPhone users. Note: It is recommended by Apple that you default to showing your \"normal\" html page to iPhone users and allow them to choose if they want an iPhone version. With Rails 2.0, you can use its multiview capabilities by simply adding this to your app: - in config/initializers/mime_types.rb Mime::Type.register_alias \"text/html\", :iphone Then, just create your views using suffices of iphone.erb instead of html.erb: index.iphone.erb show.iphone.erb etc. Note: you will probably want to use a Web library specific for iPhone applications. FWIW, I use Da shcode (in the iPhone SDK) to write and debug the iPhone application and then integrate it with my Rails project."
|
17
|
+
email: damondanieli@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.txt
|
24
|
+
files:
|
25
|
+
- lib/is_it_iphone.rb
|
26
|
+
- README.txt
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://isitiphone.rubyforge.org
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.8.1
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements:
|
47
|
+
- none
|
48
|
+
rubyforge_project: Is It IPhone
|
49
|
+
rubygems_version: 1.0.1
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: This gem checks for the iPhone user agent
|
53
|
+
test_files:
|
54
|
+
- test/test_is_it_iphone.rb
|