mobile_ninja 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +1 -0
- data/lib/mobile_ninja.rb +49 -0
- data/lib/mobile_ninja/version.rb +3 -0
- data/mobile_ninja.gemspec +27 -0
- data/spec/mobile_ninja_spec.rb +87 -0
- data/spec/spec_helper.rb +20 -0
- metadata +140 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 kwang
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# MobileNinja
|
2
|
+
|
3
|
+
Mobile Ninja makes it easy to detect mobile devices that access your rails applications. It allows you to write a different set of mobile-specific view templates and automatically serves the mobiles with those view templates.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mobile_ninja'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mobile_ninja
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To enable Mobile Ninja, add this line to your application controller:
|
22
|
+
|
23
|
+
```
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
enable_mobile_ninja
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Then you can write new view templates for mobile devies and place them in the following directory.
|
30
|
+
```
|
31
|
+
app/views_mobile/
|
32
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mobile_ninja.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'action_controller'
|
3
|
+
require "mobile_ninja/version"
|
4
|
+
|
5
|
+
module MobileNinja
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ControllerAdditions::ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ControllerAdditions
|
12
|
+
# Class Methods
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def enable_mobile_ninja
|
16
|
+
include MobileNinja::ControllerAdditions::InstanceMethods
|
17
|
+
|
18
|
+
before_filter :check_for_mobile
|
19
|
+
|
20
|
+
helper_method :mobile_device?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Instance Methods
|
25
|
+
module InstanceMethods
|
26
|
+
|
27
|
+
def check_for_mobile
|
28
|
+
# the use of mobile request parameter should be for testing purpose only
|
29
|
+
session[:mobile_override] = params[:mobile] if params[:mobile]
|
30
|
+
prepare_for_mobile if mobile_device?
|
31
|
+
end
|
32
|
+
|
33
|
+
def prepare_for_mobile
|
34
|
+
prepend_view_path Rails.root.join('app', 'views_mobile')
|
35
|
+
end
|
36
|
+
|
37
|
+
def mobile_device?
|
38
|
+
if session[:mobile_override]
|
39
|
+
!!session[:mobile_override].match(/^(true|t|yes|y|1)$/i)
|
40
|
+
else
|
41
|
+
# iPad is not considered as a mobile device
|
42
|
+
(request.user_agent =~ /Mobile|webOS/) && (request.user_agent !~ /iPad/)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
ActionController::Base.send(:include, MobileNinja)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mobile_ninja/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mobile_ninja"
|
8
|
+
spec.version = MobileNinja::VERSION
|
9
|
+
spec.authors = ["Kevin Wang"]
|
10
|
+
spec.email = ["kwanghz@gmail.com"]
|
11
|
+
spec.description = %q{A rails gem that makes detecting mobile devices easy}
|
12
|
+
spec.summary = %q{A rails gem that allows for detecting mobile devices that access the your Rails application}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rails", "~>3.2.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "rspec-rails"
|
27
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
IPHONE = "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25"
|
4
|
+
NEXUS_7 = "Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15R) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.92 Safari/537.36"
|
5
|
+
IPAD = "Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A501 Safari/9537.53"
|
6
|
+
MAC_CHROME = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
|
7
|
+
MAC_FIREFOX = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0"
|
8
|
+
|
9
|
+
|
10
|
+
describe MobileNinja do
|
11
|
+
it "should have version defined" do
|
12
|
+
MobileNinja::VERSION.should_not be_nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe MobileNinja::ControllerAdditions::ClassMethods do
|
17
|
+
let(:controller_class) { Class.new }
|
18
|
+
let(:controller) { controller_class.new }
|
19
|
+
|
20
|
+
describe "#enable_mobile_ninja" do
|
21
|
+
it "should set up controller" do
|
22
|
+
controller_class.send(:include, MobileNinja)
|
23
|
+
controller_class.should_receive(:include).with(MobileNinja::ControllerAdditions::InstanceMethods)
|
24
|
+
controller_class.should_receive(:before_filter).with(:check_for_mobile)
|
25
|
+
controller_class.should_receive(:helper_method).with(:mobile_device?)
|
26
|
+
|
27
|
+
controller_class.enable_mobile_ninja
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe MobileNinja::ControllerAdditions::InstanceMethods do
|
33
|
+
let(:controller_class) { Class.new }
|
34
|
+
let(:controller) { controller_class.new }
|
35
|
+
|
36
|
+
let(:params) { Hash.new }
|
37
|
+
let(:session) { Hash.new }
|
38
|
+
let(:request) { Object.new }
|
39
|
+
|
40
|
+
before :each do
|
41
|
+
controller_class.send(:include, MobileNinja::ControllerAdditions::InstanceMethods)
|
42
|
+
#controller_class.enable_mobile_ninja
|
43
|
+
controller.stub(:params) { params }
|
44
|
+
controller.stub(:session) { session }
|
45
|
+
controller.stub(:request) { request }
|
46
|
+
Rails.stub!(:root) { Pathname.new("/") }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".check_for_mobile" do
|
50
|
+
it "should change view template path for mobile devices" do
|
51
|
+
request.stub!(:user_agent).and_return(IPHONE)
|
52
|
+
controller.should_receive(:prepend_view_path).with(Rails.root.join('app', 'views_mobile'))
|
53
|
+
controller.check_for_mobile
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should change view template path for pc or tablet devices" do
|
57
|
+
request.stub!(:user_agent).and_return(MAC_CHROME)
|
58
|
+
controller.should_not_receive(:prepend_view_path).with(Rails.root.join('app', 'views_mobile'))
|
59
|
+
controller.check_for_mobile
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should change view template path when the mobile param is set" do
|
63
|
+
request.stub!(:user_agent).and_return(MAC_CHROME)
|
64
|
+
params[:mobile] = "true"
|
65
|
+
controller.should_receive(:prepend_view_path).with(Rails.root.join('app', 'views_mobile'))
|
66
|
+
controller.check_for_mobile
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe ".mobile_device?" do
|
71
|
+
it "should return true for mobile devices" do
|
72
|
+
request.stub!(:user_agent).and_return(IPHONE)
|
73
|
+
controller.mobile_device?.should be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return false for pc or tablet devices" do
|
77
|
+
request.stub!(:user_agent).and_return(MAC_CHROME)
|
78
|
+
controller.mobile_device?.should be_false
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should return true when the mobile param is set" do
|
82
|
+
request.stub!(:user_agent).and_return(MAC_CHROME)
|
83
|
+
session[:mobile_override] = "true"
|
84
|
+
controller.mobile_device?.should be_true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'mobile_ninja'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mobile_ninja
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kevin Wang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec-rails
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: A rails gem that makes detecting mobile devices easy
|
95
|
+
email:
|
96
|
+
- kwanghz@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/mobile_ninja.rb
|
108
|
+
- lib/mobile_ninja/version.rb
|
109
|
+
- mobile_ninja.gemspec
|
110
|
+
- spec/mobile_ninja_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
homepage: ''
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.23
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: A rails gem that allows for detecting mobile devices that access the your
|
137
|
+
Rails application
|
138
|
+
test_files:
|
139
|
+
- spec/mobile_ninja_spec.rb
|
140
|
+
- spec/spec_helper.rb
|