rack-smartphone_detector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+ pkg
3
+ .DS_Store
4
+ .swp
5
+ *~
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: 'rake test'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-smartphone-detector.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Masahiro Ihara
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,43 @@
1
+ # Rack::SmartphoneDetector [![Build Status](https://secure.travis-ci.org/ihara2525/rack-smartphone_detector.png?branch=master)](http://travis-ci.org/ihara2525/rack-smartphone_detector)
2
+
3
+ It's a very simple Rack middleware which detect the request comes from smartphones (iPhone/iPad/Android/Windows Phone).
4
+
5
+ You can use #from_smartphone? to detect the request.
6
+
7
+ ```ruby
8
+ request.from_smartphone? # true if it comes from such devices
9
+ ```
10
+
11
+ What this middleware does is just matching HTTP_USER_AGENT with identifier of smartphones.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'rack-smartphone_detector'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```
24
+ $ bundle
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```
30
+ $ gem install rack-smartphone_detector
31
+ ```
32
+
33
+ ## Usage with Rails 3
34
+
35
+ If you are using Rails 3, you have no futher steps to do.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new { |t| t.pattern = 'spec/*_spec.rb' }
5
+
6
+ task default: :test
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require 'rack/smartphone_detector'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ require 'rack/smartphone_detector'
3
+ require 'rack/smartphone_detector/checker'
4
+ require 'rack/smartphone_detector/version'
5
+ require 'rack/smartphone_detector/railtie' if defined?(Rails::Railtie)
6
+
7
+ module Rack
8
+ class SmartphoneDetector
9
+ SMARTPHONES_REGEXP = /(iPhone|iPad|Android|Windows Phone)/
10
+
11
+ def initialize(app, options = {})
12
+ @app = app
13
+ end
14
+
15
+ def call(env)
16
+ if env['HTTP_USER_AGENT'] =~ SMARTPHONES_REGEXP
17
+ env['rack.smartphone_detector.device'] = $1
18
+ end
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ class Request
24
+ include Rack::SmartphoneDetector::Checker
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+ module Rack
3
+ class SmartphoneDetector
4
+ module Checker
5
+ def self.included(base)
6
+ def from_smartphone?
7
+ !env['rack.smartphone_detector.device'].nil?
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ module Rack
3
+ class SmartphoneDetector
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'rack-smartphone_detector.configure_rails_initialization' do |app|
6
+ app.config.middleware.insert_before('ActionDispatch::ParamsParser', 'Rack::SmartphoneDetector')
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ module Rack
3
+ class SmartphoneDetector
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/smartphone_detector/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'rack-smartphone_detector'
8
+ gem.version = Rack::SmartphoneDetector::VERSION
9
+ gem.authors = ['Masahiro Ihara']
10
+ gem.email = ['ihara2525@gmail.com']
11
+ gem.homepage = 'https://github.com/ihara2525/rack-smartphone_detector'
12
+ gem.description = 'Simple Rack middleware which detect the request comes from smartphone'
13
+ gem.summary = 'Simple Rack middleware which detect the request comes from smartphone'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'rack', '~> 1.4'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'minitest'
24
+ gem.add_development_dependency 'rack-test'
25
+ end
@@ -0,0 +1,47 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'rack/test'
4
+ require_relative '../lib/rack/smartphone_detector'
5
+
6
+ describe 'Rack::SmartphoneDetector' do
7
+ include Rack::Test::Methods
8
+
9
+ SMARTPHONES = [
10
+ { device: 'iPhone', user_agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' },
11
+ { device: 'iPad', user_agent: 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' },
12
+ { device: 'Android', user_agent: 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03S) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' },
13
+ { device: 'Windows Phone', user_agent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; FujitsuToshibaMobileCommun; IS12T; KDDI)' }
14
+ ]
15
+
16
+ let(:app) do
17
+ dummy_app = ->(env) { [200, {}, 'Hello World'] }
18
+ Rack::SmartphoneDetector.new(dummy_app)
19
+ end
20
+
21
+ it 'sets environment variable if the request comes from smartphone' do
22
+ SMARTPHONES.each do |smartphone|
23
+ header 'User-Agent', smartphone[:user_agent]
24
+ get '/'
25
+ last_request.env['rack.smartphone_detector.device'].must_equal smartphone[:device]
26
+ end
27
+ end
28
+
29
+ describe '#from_smartphone?' do
30
+ it 'returns true if the request comes from smartphone' do
31
+ header 'User-Agent', SMARTPHONES.first[:device]
32
+ get '/'
33
+ last_request.from_smartphone?.must_equal true
34
+ end
35
+
36
+ it 'returns false if the request does not come from smartphone' do
37
+ header 'User-Agent', 'DoCoMo/2.0 F01E(c500;TB;W24H16)'
38
+ get '/'
39
+ last_request.from_smartphone?.must_equal false
40
+ end
41
+
42
+ it 'returns false if the request comes from unknown device' do
43
+ get '/'
44
+ last_request.from_smartphone?.must_equal false
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-smartphone_detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Masahiro Ihara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
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: '1.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
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: rack-test
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
+ description: Simple Rack middleware which detect the request comes from smartphone
79
+ email:
80
+ - ihara2525@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/rack-smartphone_detector.rb
92
+ - lib/rack/smartphone_detector.rb
93
+ - lib/rack/smartphone_detector/checker.rb
94
+ - lib/rack/smartphone_detector/railtie.rb
95
+ - lib/rack/smartphone_detector/version.rb
96
+ - rack-smartphone_detector.gemspec
97
+ - spec/rack-smartphone_detector_spec.rb
98
+ homepage: https://github.com/ihara2525/rack-smartphone_detector
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.23
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Simple Rack middleware which detect the request comes from smartphone
122
+ test_files:
123
+ - spec/rack-smartphone_detector_spec.rb