rack-user_agent 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +97 -0
  7. data/Rakefile +7 -0
  8. data/lib/rack/user_agent.rb +25 -0
  9. data/lib/rack/user_agent/checker.rb +29 -0
  10. data/lib/rack/user_agent/detector.rb +49 -0
  11. data/lib/rack/user_agent/railtie.rb +9 -0
  12. data/lib/rack/user_agent/result.rb +35 -0
  13. data/lib/rack/user_agent/version.rb +5 -0
  14. data/rack-user_agent.gemspec +29 -0
  15. data/rails_sample/.gitignore +16 -0
  16. data/rails_sample/Gemfile +15 -0
  17. data/rails_sample/Gemfile.lock +142 -0
  18. data/rails_sample/README.rdoc +28 -0
  19. data/rails_sample/Rakefile +6 -0
  20. data/rails_sample/app/assets/images/.keep +0 -0
  21. data/rails_sample/app/assets/javascripts/application.js +16 -0
  22. data/rails_sample/app/assets/stylesheets/application.css +15 -0
  23. data/rails_sample/app/controllers/application_controller.rb +14 -0
  24. data/rails_sample/app/controllers/concerns/.keep +0 -0
  25. data/rails_sample/app/helpers/application_helper.rb +2 -0
  26. data/rails_sample/app/mailers/.keep +0 -0
  27. data/rails_sample/app/models/.keep +0 -0
  28. data/rails_sample/app/models/concerns/.keep +0 -0
  29. data/rails_sample/app/views/application/index.html.erb +4 -0
  30. data/rails_sample/app/views/layouts/application.html.erb +14 -0
  31. data/rails_sample/bin/bundle +3 -0
  32. data/rails_sample/bin/rails +8 -0
  33. data/rails_sample/bin/rake +8 -0
  34. data/rails_sample/bin/spring +18 -0
  35. data/rails_sample/config.ru +4 -0
  36. data/rails_sample/config/application.rb +24 -0
  37. data/rails_sample/config/boot.rb +4 -0
  38. data/rails_sample/config/database.yml +25 -0
  39. data/rails_sample/config/environment.rb +5 -0
  40. data/rails_sample/config/environments/development.rb +37 -0
  41. data/rails_sample/config/environments/production.rb +82 -0
  42. data/rails_sample/config/environments/test.rb +39 -0
  43. data/rails_sample/config/initializers/assets.rb +8 -0
  44. data/rails_sample/config/initializers/backtrace_silencers.rb +7 -0
  45. data/rails_sample/config/initializers/cookies_serializer.rb +3 -0
  46. data/rails_sample/config/initializers/filter_parameter_logging.rb +4 -0
  47. data/rails_sample/config/initializers/inflections.rb +16 -0
  48. data/rails_sample/config/initializers/mime_types.rb +4 -0
  49. data/rails_sample/config/initializers/session_store.rb +3 -0
  50. data/rails_sample/config/initializers/wrap_parameters.rb +14 -0
  51. data/rails_sample/config/locales/en.yml +23 -0
  52. data/rails_sample/config/routes.rb +3 -0
  53. data/rails_sample/config/secrets.yml +22 -0
  54. data/rails_sample/db/seeds.rb +7 -0
  55. data/rails_sample/lib/assets/.keep +0 -0
  56. data/rails_sample/lib/tasks/.keep +0 -0
  57. data/rails_sample/log/.keep +0 -0
  58. data/rails_sample/public/404.html +67 -0
  59. data/rails_sample/public/422.html +67 -0
  60. data/rails_sample/public/500.html +66 -0
  61. data/rails_sample/public/favicon.ico +0 -0
  62. data/rails_sample/public/robots.txt +5 -0
  63. data/rails_sample/test/controllers/.keep +0 -0
  64. data/rails_sample/test/fixtures/.keep +0 -0
  65. data/rails_sample/test/helpers/.keep +0 -0
  66. data/rails_sample/test/integration/.keep +0 -0
  67. data/rails_sample/test/mailers/.keep +0 -0
  68. data/rails_sample/test/models/.keep +0 -0
  69. data/rails_sample/test/test_helper.rb +10 -0
  70. data/rails_sample/vendor/assets/javascripts/.keep +0 -0
  71. data/rails_sample/vendor/assets/stylesheets/.keep +0 -0
  72. data/sinatra_sample/Gemfile +5 -0
  73. data/sinatra_sample/Gemfile.lock +33 -0
  74. data/sinatra_sample/app.rb +15 -0
  75. data/sinatra_sample/config.ru +3 -0
  76. data/spec/lib/rack/user_agent/checker_spec.rb +60 -0
  77. data/spec/lib/rack/user_agent/detector_spec.rb +165 -0
  78. data/spec/lib/rack/user_agent/result_spec.rb +31 -0
  79. data/spec/lib/rack/user_agent_spec.rb +18 -0
  80. data/spec/spec_helper.rb +6 -0
  81. metadata +226 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d29f9a9c7d2bf94c948b6a8af1f27a2d791a7544
4
+ data.tar.gz: 858b3d9b30b8c877fb7b6d68e0d397f22c99d3c4
5
+ SHA512:
6
+ metadata.gz: cd37882adcb3e5479dcf607eb2afcc94702f9c312309d9f2edeaec30c9bdfcce96d90e11c52cbd99fb695b031895a41e8eb5ee57d83b854d20e3497e437b33ac
7
+ data.tar.gz: 3522df8a6a7c2dda33b29c1c6a144dde3c075e5c7ef34e741419f313a6b4b52c602c6fa9f17be6b8f6a9a1aeb94ce6aa96b05db8c484e31aa0f05a029c4d731b
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 2.1.5
3
+ branches:
4
+ only:
5
+ - master
6
+ script:
7
+ - "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-user_agent.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Takashi Kokubun
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.
@@ -0,0 +1,97 @@
1
+ # Rack::UserAgent [![Build Status](https://travis-ci.org/k0kubun/rack-user_agent.svg)](https://travis-ci.org/k0kubun/rack-user_agent)
2
+
3
+ Rack::Request extension for handling User-Agent.
4
+ Thanks to [woothee](https://github.com/woothee/woothee-ruby), Rack::UserAgent supports various User-Agents.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rack-user_agent'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ### Rails
17
+
18
+ If you bundle "rack-user\_agent", Rack::UserAgent will be automatically inserted when started.
19
+ Additional methods for `request` will be available with no cost.
20
+
21
+ ```ruby
22
+ class ApplicationController < ActionController::Base
23
+ before_action :set_request_variant
24
+
25
+ def index
26
+ request.user_agent #=> "Mozilla/5.0 (Macintosh; ..."
27
+ request.device_type #=> :pc
28
+ request.os #=> "Mac OSX"
29
+ request.browser #=> "Chrome"
30
+ request.from_pc? #=> true
31
+ request.from_smartphone? #=> false
32
+ end
33
+
34
+ private
35
+
36
+ # It is useful for Action Pack variants, which is new feature from Rails 4.1.
37
+ # You can switch view templates by +pc or +smartphone in file name.
38
+ # http://guides.rubyonrails.org/4_1_release_notes.html#action-pack-variants
39
+ def set_request_variant
40
+ request.variant = request.device_type # :pc, :smartphone
41
+ end
42
+ end
43
+ ```
44
+
45
+ ### Sinatra
46
+
47
+ You can also manually use Rack::UserAgent for any Rack apps.
48
+
49
+ ```ruby
50
+ require "sinatra"
51
+ require "rack/user_agent"
52
+
53
+ configure do
54
+ use Rack::UserAgent
55
+ end
56
+
57
+ get "/" do
58
+ request.user_agent #=> "Mozilla/5.0 (Linux; Android 4.3; Nexus 7 ..."
59
+ request.device_type #=> :smartphone
60
+ request.os #=> "Android"
61
+ request.os_version #=> "4.3"
62
+ request.browser #=> "Chrome"
63
+ request.browser_version #=> "29.0.1547.72"
64
+ request.from_smartphone? #=> true
65
+ end
66
+ ```
67
+
68
+ ## Available Methods
69
+
70
+ ### Attributes
71
+ - request.device\_type
72
+ - request.os
73
+ - request.os\_version
74
+ - request.browser
75
+ - request.browser\_version
76
+ - request.browser\_vendor
77
+
78
+ ### Helpers
79
+ - request.from\_pc?
80
+ - request.from\_smartphone?
81
+ - request.from\_mobilephone?
82
+ - request.from\_appliance?
83
+ - request.from\_crawler?
84
+
85
+ ### Detectors
86
+ APIs compatible with [rack-smartphone\_detector](https://github.com/ihara2525/rack-smartphone_detector)
87
+
88
+ - request.smartphone\_version
89
+ - request.from\_iphone?
90
+ - request.from\_ipad?
91
+ - request.from\_android?
92
+ - request.from\_android\_tablet?
93
+ - request.from\_windows\_phone?
94
+
95
+ ## License
96
+
97
+ MIT License
@@ -0,0 +1,7 @@
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
7
+
@@ -0,0 +1,25 @@
1
+ require "woothee"
2
+ require "rack/user_agent/checker"
3
+ require "rack/user_agent/detector"
4
+ require "rack/user_agent/result"
5
+ require "rack/user_agent/version"
6
+ require 'rack/user_agent/railtie' if defined?(Rails::Railtie)
7
+
8
+ module Rack
9
+ class UserAgent
10
+ def initialize(app, options = {})
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ env["rack.woothee"] = ::Woothee.parse(env["HTTP_USER_AGENT"] || "")
16
+ @app.call(env)
17
+ end
18
+ end
19
+
20
+ class Request
21
+ include Rack::UserAgent::Checker
22
+ include Rack::UserAgent::Detector
23
+ include Rack::UserAgent::Result
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require "rack/user_agent/result"
2
+
3
+ module Rack
4
+ class UserAgent
5
+ module Checker
6
+ include Result
7
+
8
+ def from_pc?
9
+ device_type == :pc
10
+ end
11
+
12
+ def from_smartphone?
13
+ device_type == :smartphone
14
+ end
15
+
16
+ def from_mobilephone?
17
+ device_type == :mobilephone
18
+ end
19
+
20
+ def from_appliance?
21
+ device_type == :appliance
22
+ end
23
+
24
+ def from_crawler?
25
+ device_type == :crawler
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require "rack/user_agent/result"
2
+
3
+ # This module aims to provide APIs compatible with:
4
+ # https://github.com/ihara2525/rack-smartphone_detector
5
+ module Rack
6
+ class UserAgent
7
+ module Detector
8
+ include Result
9
+
10
+ def smartphone_version
11
+ suppress_unknown(os_version)
12
+ end
13
+
14
+ def from_iphone?
15
+ os == "iPhone"
16
+ end
17
+
18
+ def from_ipad?
19
+ os == "iPad"
20
+ end
21
+
22
+ def from_android?
23
+ os == "Android" && android_mobile?
24
+ end
25
+
26
+ def from_android_tablet?
27
+ os == "Android" && !android_mobile?
28
+ end
29
+
30
+ def from_windows_phone?
31
+ os == "Windows Phone OS"
32
+ end
33
+
34
+ private
35
+
36
+ def android_mobile?
37
+ (user_agent =~ /Android.+Mobi(le)?/).nil?.!
38
+ end
39
+
40
+ def suppress_unknown(version)
41
+ if version == "UNKNOWN"
42
+ nil
43
+ else
44
+ version
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ class UserAgent
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "rack-user_agent.configure_rails_initialization" do |app|
5
+ app.config.middleware.insert_before(ActionDispatch::ParamsParser, Rack::UserAgent)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ module Rack
2
+ class UserAgent
3
+ module Result
4
+ def device_type
5
+ woothee_result[:category]
6
+ end
7
+
8
+ def os
9
+ woothee_result[:os]
10
+ end
11
+
12
+ def os_version
13
+ woothee_result[:os_version]
14
+ end
15
+
16
+ def browser
17
+ woothee_result[:name]
18
+ end
19
+
20
+ def browser_version
21
+ woothee_result[:version]
22
+ end
23
+
24
+ def browser_vendor
25
+ woothee_result[:vendor]
26
+ end
27
+
28
+ private
29
+
30
+ def woothee_result
31
+ env["rack.woothee"]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class UserAgent
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/user_agent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-user_agent"
8
+ spec.version = Rack::UserAgent::VERSION
9
+ spec.authors = ["Takashi Kokubun"]
10
+ spec.email = ["takashikkbn@gmail.com"]
11
+ spec.summary = %q{Rack::Request extension for handling User-Agent.}
12
+ spec.description = %q{Rack::Request extension for handling User-Agent.}
13
+ spec.homepage = "https://github.com/k0kubun/rack-user_agent"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 "rack", "~> 1.5"
22
+ spec.add_dependency "woothee", "~> 1.0.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "rack-test"
29
+ end
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '4.1.5'
4
+ gem 'sqlite3'
5
+ gem 'sass-rails', '~> 4.0.3'
6
+ gem 'uglifier', '>= 1.3.0'
7
+ gem 'coffee-rails', '~> 4.0.0'
8
+ gem 'jquery-rails'
9
+ gem 'turbolinks'
10
+ gem 'jbuilder', '~> 2.0'
11
+ gem 'sdoc', '~> 0.4.0', group: :doc
12
+ gem 'spring', group: :development
13
+ gem 'pry'
14
+
15
+ gem 'rack-user_agent', path: '..'
@@ -0,0 +1,142 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ rack-user_agent (0.0.1)
5
+ rack (~> 1.5)
6
+ woothee (~> 1.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.1.5)
12
+ actionpack (= 4.1.5)
13
+ actionview (= 4.1.5)
14
+ mail (~> 2.5.4)
15
+ actionpack (4.1.5)
16
+ actionview (= 4.1.5)
17
+ activesupport (= 4.1.5)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ actionview (4.1.5)
21
+ activesupport (= 4.1.5)
22
+ builder (~> 3.1)
23
+ erubis (~> 2.7.0)
24
+ activemodel (4.1.5)
25
+ activesupport (= 4.1.5)
26
+ builder (~> 3.1)
27
+ activerecord (4.1.5)
28
+ activemodel (= 4.1.5)
29
+ activesupport (= 4.1.5)
30
+ arel (~> 5.0.0)
31
+ activesupport (4.1.5)
32
+ i18n (~> 0.6, >= 0.6.9)
33
+ json (~> 1.7, >= 1.7.7)
34
+ minitest (~> 5.1)
35
+ thread_safe (~> 0.1)
36
+ tzinfo (~> 1.1)
37
+ arel (5.0.1.20140414130214)
38
+ builder (3.2.2)
39
+ coderay (1.1.0)
40
+ coffee-rails (4.0.1)
41
+ coffee-script (>= 2.2.0)
42
+ railties (>= 4.0.0, < 5.0)
43
+ coffee-script (2.3.0)
44
+ coffee-script-source
45
+ execjs
46
+ coffee-script-source (1.8.0)
47
+ erubis (2.7.0)
48
+ execjs (2.2.2)
49
+ hike (1.2.3)
50
+ i18n (0.6.11)
51
+ jbuilder (2.2.5)
52
+ activesupport (>= 3.0.0, < 5)
53
+ multi_json (~> 1.2)
54
+ jquery-rails (3.1.2)
55
+ railties (>= 3.0, < 5.0)
56
+ thor (>= 0.14, < 2.0)
57
+ json (1.8.1)
58
+ mail (2.5.4)
59
+ mime-types (~> 1.16)
60
+ treetop (~> 1.4.8)
61
+ method_source (0.8.2)
62
+ mime-types (1.25.1)
63
+ minitest (5.4.3)
64
+ multi_json (1.10.1)
65
+ polyglot (0.3.5)
66
+ pry (0.10.0)
67
+ coderay (~> 1.1.0)
68
+ method_source (~> 0.8.1)
69
+ slop (~> 3.4)
70
+ rack (1.5.2)
71
+ rack-test (0.6.2)
72
+ rack (>= 1.0)
73
+ rails (4.1.5)
74
+ actionmailer (= 4.1.5)
75
+ actionpack (= 4.1.5)
76
+ actionview (= 4.1.5)
77
+ activemodel (= 4.1.5)
78
+ activerecord (= 4.1.5)
79
+ activesupport (= 4.1.5)
80
+ bundler (>= 1.3.0, < 2.0)
81
+ railties (= 4.1.5)
82
+ sprockets-rails (~> 2.0)
83
+ railties (4.1.5)
84
+ actionpack (= 4.1.5)
85
+ activesupport (= 4.1.5)
86
+ rake (>= 0.8.7)
87
+ thor (>= 0.18.1, < 2.0)
88
+ rake (10.3.2)
89
+ rdoc (4.1.2)
90
+ json (~> 1.4)
91
+ sass (3.2.19)
92
+ sass-rails (4.0.4)
93
+ railties (>= 4.0.0, < 5.0)
94
+ sass (~> 3.2.2)
95
+ sprockets (~> 2.8, < 2.12)
96
+ sprockets-rails (~> 2.0)
97
+ sdoc (0.4.1)
98
+ json (~> 1.7, >= 1.7.7)
99
+ rdoc (~> 4.0)
100
+ slop (3.6.0)
101
+ spring (1.1.3)
102
+ sprockets (2.11.3)
103
+ hike (~> 1.2)
104
+ multi_json (~> 1.0)
105
+ rack (~> 1.0)
106
+ tilt (~> 1.1, != 1.3.0)
107
+ sprockets-rails (2.2.0)
108
+ actionpack (>= 3.0)
109
+ activesupport (>= 3.0)
110
+ sprockets (>= 2.8, < 4.0)
111
+ sqlite3 (1.3.10)
112
+ thor (0.19.1)
113
+ thread_safe (0.3.4)
114
+ tilt (1.4.1)
115
+ treetop (1.4.15)
116
+ polyglot
117
+ polyglot (>= 0.3.1)
118
+ turbolinks (2.5.2)
119
+ coffee-rails
120
+ tzinfo (1.2.2)
121
+ thread_safe (~> 0.1)
122
+ uglifier (2.5.3)
123
+ execjs (>= 0.3.0)
124
+ json (>= 1.8.0)
125
+ woothee (1.0.0)
126
+
127
+ PLATFORMS
128
+ ruby
129
+
130
+ DEPENDENCIES
131
+ coffee-rails (~> 4.0.0)
132
+ jbuilder (~> 2.0)
133
+ jquery-rails
134
+ pry
135
+ rack-user_agent!
136
+ rails (= 4.1.5)
137
+ sass-rails (~> 4.0.3)
138
+ sdoc (~> 0.4.0)
139
+ spring
140
+ sqlite3
141
+ turbolinks
142
+ uglifier (>= 1.3.0)