rack_detect_platform 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +33 -0
- data/README +3 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/lib/rack/detect_platform.rb +30 -0
- data/rack_detect_platform.gemspec +55 -0
- data/spec/detect_platform_spec.rb +95 -0
- data/spec/spec_helper.rb +7 -0
- metadata +107 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
gemcutter (0.5.0)
|
6
|
+
json_pure
|
7
|
+
git (1.2.5)
|
8
|
+
jeweler (1.4.0)
|
9
|
+
gemcutter (>= 0.1.0)
|
10
|
+
git (>= 1.2.5)
|
11
|
+
rubyforge (>= 2.0.0)
|
12
|
+
json_pure (1.4.3)
|
13
|
+
rack (1.2.1)
|
14
|
+
rake (0.8.7)
|
15
|
+
rspec (2.0.0.beta.20)
|
16
|
+
rspec-core (= 2.0.0.beta.20)
|
17
|
+
rspec-expectations (= 2.0.0.beta.20)
|
18
|
+
rspec-mocks (= 2.0.0.beta.20)
|
19
|
+
rspec-core (2.0.0.beta.20)
|
20
|
+
rspec-expectations (2.0.0.beta.20)
|
21
|
+
diff-lcs (>= 1.1.2)
|
22
|
+
rspec-mocks (2.0.0.beta.20)
|
23
|
+
rubyforge (2.0.4)
|
24
|
+
json_pure (>= 1.1.7)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
jeweler
|
31
|
+
rack
|
32
|
+
rake
|
33
|
+
rspec (= 2.0.0.beta.20)
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run specs.'
|
6
|
+
task :default => :noop
|
7
|
+
|
8
|
+
desc 'Generate documentation for the spec plugin.'
|
9
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Rack::DetectPlatform'
|
12
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
13
|
+
rdoc.rdoc_files.include('README')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'jeweler'
|
19
|
+
Jeweler::Tasks.new do |gemspec|
|
20
|
+
gemspec.name = "rack_detect_platform"
|
21
|
+
gemspec.summary = "Very simple Rack middleware to set an env variable based on user agent"
|
22
|
+
gemspec.description = "Very simple Rack middleware to set an env variable based on user agent"
|
23
|
+
gemspec.email = "brian@heimidal.net"
|
24
|
+
gemspec.homepage = "http://github.com/heimidal/rack_detect_platform"
|
25
|
+
gemspec.authors = ["Brian Rose"]
|
26
|
+
gemspec.add_development_dependency('rspec', '2.0.0.beta.20')
|
27
|
+
gemspec.add_dependency('rack')
|
28
|
+
end
|
29
|
+
rescue LoadError
|
30
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
31
|
+
end
|
32
|
+
|
33
|
+
Jeweler::GemcutterTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class DetectPlatform
|
5
|
+
def initialize(app, options = {})
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
user_agent = env.fetch('HTTP_USER_AGENT', '')
|
11
|
+
|
12
|
+
platforms.each do |re, platform|
|
13
|
+
if user_agent =~ re
|
14
|
+
env['X_PLATFORM'] = platform
|
15
|
+
break
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@app.call(env)
|
20
|
+
end
|
21
|
+
|
22
|
+
# For the time being, Android is being identified as an iPhone. We'll split this out later.
|
23
|
+
def platforms
|
24
|
+
{
|
25
|
+
/(iPhone|Android)/ => 'iphone',
|
26
|
+
/iPad/ => 'ipad'
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rack_detect_platform}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brian Rose"]
|
12
|
+
s.date = %q{2010-08-31}
|
13
|
+
s.description = %q{Very simple Rack middleware to set an env variable based on user agent}
|
14
|
+
s.email = %q{brian@heimidal.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"Gemfile",
|
20
|
+
"Gemfile.lock",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/rack/detect_platform.rb",
|
25
|
+
"rack_detect_platform.gemspec",
|
26
|
+
"spec/detect_platform_spec.rb",
|
27
|
+
"spec/spec_helper.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/heimidal/rack_detect_platform}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.7}
|
33
|
+
s.summary = %q{Very simple Rack middleware to set an env variable based on user agent}
|
34
|
+
s.test_files = [
|
35
|
+
"spec/detect_platform_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_development_dependency(%q<rspec>, ["= 2.0.0.beta.20"])
|
45
|
+
s.add_runtime_dependency(%q<rack>, [">= 0"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<rspec>, ["= 2.0.0.beta.20"])
|
48
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rspec>, ["= 2.0.0.beta.20"])
|
52
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Rack::DetectPlatform do
|
4
|
+
before do
|
5
|
+
@app = test_app
|
6
|
+
@rack = Rack::DetectPlatform.new(@app)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'sets platform to iphone' do
|
10
|
+
env = test_env('HTTP_USER_AGENT' => iphone)
|
11
|
+
@rack.call(env)
|
12
|
+
|
13
|
+
env['X_PLATFORM'].should == "iphone"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sets platform to iphone when the device is running Android' do
|
17
|
+
env = test_env('HTTP_USER_AGENT' => android)
|
18
|
+
@rack.call(env)
|
19
|
+
|
20
|
+
env['X_PLATFORM'].should == "iphone"
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sets platform to ipad' do
|
24
|
+
env = test_env('HTTP_USER_AGENT' => ipad)
|
25
|
+
@rack.call(env)
|
26
|
+
|
27
|
+
env['X_PLATFORM'].should == "ipad"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "doesn't set the platform if it can't find it" do
|
31
|
+
env = test_env
|
32
|
+
@rack.call(env)
|
33
|
+
|
34
|
+
env.key?('X_PLATFORM').should be_false
|
35
|
+
end
|
36
|
+
|
37
|
+
it "calls the app" do
|
38
|
+
env = test_env
|
39
|
+
@app.should_receive(:call).with(env)
|
40
|
+
|
41
|
+
@rack.call(env)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_env(overwrite = {})
|
46
|
+
{
|
47
|
+
'GATEWAY_INTERFACE'=> 'CGI/1.2',
|
48
|
+
'HTTP_ACCEPT'=> 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
49
|
+
'HTTP_ACCEPT_CHARSET'=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
|
50
|
+
'HTTP_ACCEPT_ENCODING'=> 'gzip,deflate',
|
51
|
+
'HTTP_ACCEPT_LANGUAGE'=> 'en-us,en;q=0.5',
|
52
|
+
'HTTP_CONNECTION'=> 'keep-alive',
|
53
|
+
'HTTP_HOST'=> 'localhost:4567',
|
54
|
+
'HTTP_KEEP_ALIVE'=> 300,
|
55
|
+
'HTTP_USER_AGENT'=> 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090920 Firefox/3.5.3 (Swiftfox)',
|
56
|
+
'HTTP_VERSION'=> 'HTTP/1.1',
|
57
|
+
'PATH_INFO'=> '/',
|
58
|
+
'QUERY_STRING'=> '',
|
59
|
+
'REMOTE_ADDR'=> '127.0.0.1',
|
60
|
+
'REQUEST_METHOD'=> 'GET',
|
61
|
+
'REQUEST_PATH'=> '/',
|
62
|
+
'REQUEST_URI'=> '/',
|
63
|
+
'SCRIPT_NAME'=> '',
|
64
|
+
'SERVER_NAME'=> 'localhost',
|
65
|
+
'SERVER_PORT'=> '4567',
|
66
|
+
'SERVER_PROTOCOL'=> 'HTTP/1.1',
|
67
|
+
'SERVER_SOFTWARE'=> 'Mongrel 1.1.5',
|
68
|
+
'rack.multiprocess'=> false,
|
69
|
+
'rack.multithread'=> true,
|
70
|
+
'rack.request.form_hash'=> '',
|
71
|
+
'rack.request.form_vars'=> '',
|
72
|
+
'rack.request.query_hash'=> '',
|
73
|
+
'rack.request.query_string'=> '',
|
74
|
+
'rack.run_once'=> false,
|
75
|
+
'rack.url_scheme'=> 'http',
|
76
|
+
'rack.version'=> '1: 0'
|
77
|
+
}.merge(overwrite)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_app
|
81
|
+
Class.new { def call(app); true; end }.new
|
82
|
+
end
|
83
|
+
|
84
|
+
# User agents for testing
|
85
|
+
def ipad
|
86
|
+
'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10'
|
87
|
+
end
|
88
|
+
|
89
|
+
def iphone
|
90
|
+
'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3'
|
91
|
+
end
|
92
|
+
|
93
|
+
def android
|
94
|
+
'Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2'
|
95
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_detect_platform
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brian Rose
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-31 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 62196427
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta
|
35
|
+
- 20
|
36
|
+
version: 2.0.0.beta.20
|
37
|
+
type: :development
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: rack
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Very simple Rack middleware to set an env variable based on user agent
|
54
|
+
email: brian@heimidal.net
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- README
|
65
|
+
- Rakefile
|
66
|
+
- VERSION
|
67
|
+
- lib/rack/detect_platform.rb
|
68
|
+
- rack_detect_platform.gemspec
|
69
|
+
- spec/detect_platform_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/heimidal/rack_detect_platform
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Very simple Rack middleware to set an env variable based on user agent
|
105
|
+
test_files:
|
106
|
+
- spec/detect_platform_spec.rb
|
107
|
+
- spec/spec_helper.rb
|