spy-vs-spy 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
File without changes
data/lib/spy-vs-spy.rb ADDED
@@ -0,0 +1,140 @@
1
+ =begin
2
+ Copyright notice
3
+ =============================================================================
4
+ Copyright (c) 2008-2009 Soldier Of Code
5
+
6
+ Material published by Soldier Of Code in this file is copyright Soldier Of Code
7
+ and may not be reproduced without permission. Copyright exists in all other
8
+ original material published on the internet by employees of Soldier Of Code and
9
+ may belong to the author or to Soldier Of Code. depending on the circumstances of
10
+ publication.
11
+ ==============================================================================
12
+
13
+ This middleware is based on work initialy done by Jackson Miller with his
14
+ parse-user-agent project (http://github.com/jaxn/parse-user-agent). Jackson
15
+ can be reached at jackson.h.miller@gmail.com - parse-user-agent uses a
16
+ MIT/X Consortium License
17
+
18
+ ==============================================================================
19
+ LICENSE
20
+ ==============================================================================
21
+ see LICENSE file for details
22
+
23
+ =end
24
+ module SoldierOfCode
25
+ class SpyVsSpy
26
+
27
+ def initialize(app)
28
+ @app = app
29
+ end
30
+
31
+ def call(env)
32
+
33
+ http_user_agent = env['HTTP_USER_AGENT']
34
+
35
+ env['soldierofcode.spy-vs-spy'] = deconstruct(http_user_agent)
36
+
37
+ @app.call(env)
38
+ end
39
+
40
+ def deconstruct(agent)
41
+ ParseUserAgent.new(agent)
42
+ end
43
+
44
+ end
45
+
46
+ class ParseUserAgent
47
+
48
+ attr_reader :ostype, :browser, :os_version, :browser_version_major
49
+
50
+ def parse(user_agent)
51
+ if '-' == user_agent
52
+ raise 'Invalid User Agent'
53
+ end
54
+
55
+ @user_agent = user_agent
56
+
57
+ # fix Opera
58
+ #useragent =~ s/Opera (\d)/Opera\/$1/i;
59
+ useragent = @user_agent.gsub(/(Opera [\d])/, 'Opera\1')
60
+
61
+ # grab all Agent/version strings as 'agents'
62
+ @agents = Array.new
63
+ @user_agent.split(/\s+/).each {|string|
64
+ if string =~ /\//
65
+ @agents<< string
66
+ end
67
+ }
68
+
69
+ # cycle through the agents to set browser and version (MSIE is set later)
70
+ if @agents && @agents.length > 0
71
+ @agents.each {|agent|
72
+ parts = agent.split('/')
73
+ @browser = parts[0]
74
+ @browser_version = parts[1]
75
+ if @browser == 'Firefox'
76
+ @browser_version_major = parts[1].slice(0, 3)
77
+ @browser_version_minor = parts[1].sub(@browser_version_major, '').sub('.', '')
78
+ elsif @browser == 'Safari'
79
+ if parts[1].slice(0, 3).to_f < 400
80
+ @browser_version_major = '1'
81
+ else
82
+ @browser_version_major = '2'
83
+ end
84
+ else
85
+ @browser_version_major = parts[1].slice(0, 1)
86
+ end
87
+ }
88
+ end
89
+
90
+ # grab all of the properties (within parens)
91
+ # should be in relation to the agent if possible
92
+ @detail = @user_agent
93
+ @user_agent.gsub(/\((.*)\)/, '').split(/\s/).each {|part| @detail = @detail.gsub(part, '')}
94
+ @detail = @detail.gsub('(', '').gsub(')', '').lstrip
95
+ @properties = @detail.split(/;\s+/)
96
+
97
+ # cycle through the properties to set known quantities
98
+ @properties.each {|property|
99
+ if property =~ /^Win/
100
+ @ostype = 'Windows'
101
+ @os = property
102
+ if parts = property.split(/ /, 2)
103
+ if parts[1] =~ /^NT/
104
+ @ostype = 'Windows'
105
+ subparts = parts[1].split(/ /, 2)
106
+ if subparts[1] == '5'
107
+ @os_version = '2000'
108
+ elsif subparts[1] == '5.1'
109
+ @os_version = 'XP'
110
+ else
111
+ @os_version = subparts[1]
112
+ end
113
+ end
114
+ end
115
+ end
116
+ if property == 'Macintosh'
117
+ @ostype = 'Macintosh'
118
+ @os = property
119
+ end
120
+ if property =~ /OS X/
121
+ @ostype = 'Macintosh'
122
+ @os_version = 'OS X'
123
+ @os = property
124
+ end
125
+ if property =~ /^Linux/
126
+ @ostype = 'Linux'
127
+ @os = property
128
+ end
129
+ if property =~ /^MSIE/
130
+ @browser = 'MSIE'
131
+ @browser_version = property.gsub('MSIE ', '').lstrip
132
+ @browser_version_major, @browser_version_minor = @browser_version.split('.')
133
+ end
134
+ }
135
+ self
136
+ end
137
+
138
+
139
+ end
140
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "spy-vs-spy"
3
+ s.version = "0.0.1"
4
+ s.date = "2009-11-19"
5
+ s.summary = "UserAgent Detection Middleware for Rack."
6
+ s.email = "kaptiankrispy@soldierofcode.com"
7
+ s.homepage = "http://github.com/kuccello/Spy-Vs-Spy"
8
+ s.description = ""
9
+ s.has_rdoc = true
10
+ s.authors = ["Kristan 'Krispy' Uccello"]
11
+ s.files = ["README",
12
+ "spy-vs-spy.gemspec",
13
+ "lib/spy-vs-spy.rb"]
14
+ s.test_files = ["test/ss-test.rb"]
15
+ s.rdoc_options = ["--main", "README"]
16
+ s.extra_rdoc_files = ["README"]
17
+ end
data/test/ss-test.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "rack/test"
2
+
3
+ class SpyVsSpyTest < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ SoldierOfCode::SpyVsSpy.new
8
+ end
9
+
10
+ def test_nothing
11
+
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spy-vs-spy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kristan 'Krispy' Uccello
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-19 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: kaptiankrispy@soldierofcode.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - spy-vs-spy.gemspec
27
+ - lib/spy-vs-spy.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/kuccello/Spy-Vs-Spy
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --main
35
+ - README
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: UserAgent Detection Middleware for Rack.
57
+ test_files:
58
+ - test/ss-test.rb