spy-vs-spy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/spy-vs-spy.rb +21 -204
- data/spy-vs-spy.gemspec +1 -1
- data/test/ss-test.rb +3 -3
- metadata +1 -1
data/lib/spy-vs-spy.rb
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
=begin
|
2
2
|
Copyright notice
|
3
3
|
=============================================================================
|
4
|
-
Copyright (c)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
Copyright (c) 2009 Kristan 'Krispy' Uccello <krispy@soldierofcode.com> - Soldier Of Code
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
11
23
|
==============================================================================
|
12
24
|
|
13
25
|
This middleware is based on work initialy done by Jackson Miller with his
|
@@ -33,7 +45,7 @@ module SoldierOfCode
|
|
33
45
|
|
34
46
|
class SpyVsSpy
|
35
47
|
|
36
|
-
def initialize(app)
|
48
|
+
def initialize(app=nil)
|
37
49
|
@app = app
|
38
50
|
end
|
39
51
|
|
@@ -50,9 +62,7 @@ module SoldierOfCode
|
|
50
62
|
ParseUserAgent.new.parse(agent)
|
51
63
|
end
|
52
64
|
|
53
|
-
|
54
|
-
|
55
|
-
class ParseUserAgent
|
65
|
+
|
56
66
|
|
57
67
|
@@safari = {
|
58
68
|
"1.0" => ["85.5", "85.6", "85.7"],
|
@@ -409,198 +419,5 @@ module SoldierOfCode
|
|
409
419
|
def process_netscape(agent)
|
410
420
|
|
411
421
|
end
|
412
|
-
|
413
|
-
=begin
|
414
|
-
|
415
|
-
def known_browser(browser)
|
416
|
-
|
417
|
-
browsers = %w[ Firefox Safari MSIE ]
|
418
|
-
flag = false
|
419
|
-
browsers.each do |b|
|
420
|
-
flag = true if b == browser
|
421
|
-
end
|
422
|
-
|
423
|
-
flag
|
424
|
-
end
|
425
|
-
|
426
|
-
def known_safari_agent_string(string)
|
427
|
-
agents = []
|
428
|
-
agents << "AppleWebKit"
|
429
|
-
agents << "iPhone"
|
430
|
-
|
431
|
-
agents.each do |agent|
|
432
|
-
return true if string.include?(agent)
|
433
|
-
end
|
434
|
-
false
|
435
|
-
end
|
436
|
-
|
437
|
-
def mobile_browser?(string)
|
438
|
-
agents = []
|
439
|
-
agents << "iPhone"
|
440
|
-
|
441
|
-
agents.each do |agent|
|
442
|
-
if string.include?(agent)
|
443
|
-
@mobile_browser = agent
|
444
|
-
return true
|
445
|
-
end
|
446
|
-
end
|
447
|
-
false
|
448
|
-
end
|
449
|
-
|
450
|
-
def console_browser?(string)
|
451
|
-
agents = []
|
452
|
-
|
453
|
-
agents.each do |agent|
|
454
|
-
if string.include?(agent)
|
455
|
-
@console_browser = agent
|
456
|
-
return true
|
457
|
-
end
|
458
|
-
end
|
459
|
-
false
|
460
|
-
end
|
461
|
-
|
462
|
-
def is_safari?(str)
|
463
|
-
s = []
|
464
|
-
s << "iPhone"
|
465
|
-
s << "Safari"
|
466
|
-
|
467
|
-
flag = false
|
468
|
-
s.each do |st|
|
469
|
-
flag = true if str.include?(st)
|
470
|
-
end
|
471
|
-
flag
|
472
|
-
end
|
473
|
-
|
474
|
-
def parse(user_agent)
|
475
|
-
if '-' == user_agent
|
476
|
-
raise 'Invalid User Agent'
|
477
|
-
end
|
478
|
-
|
479
|
-
@user_agent = user_agent
|
480
|
-
|
481
|
-
# fix Opera
|
482
|
-
#useragent =~ s/Opera (\d)/Opera\/$1/i;
|
483
|
-
useragent = @user_agent.gsub(/(Opera [\d])/, 'Opera\1')
|
484
|
-
|
485
|
-
# grab all Agent/version strings as 'agents'
|
486
|
-
@agents = Array.new
|
487
|
-
@user_agent.split(/\s+/).each {|string|
|
488
|
-
if string =~ /\//
|
489
|
-
@agents<< string
|
490
|
-
end
|
491
|
-
}
|
492
|
-
|
493
|
-
# cycle through the agents to set browser and version (MSIE is set later)
|
494
|
-
if @agents && @agents.length > 0
|
495
|
-
@agents.each {|agent|
|
496
|
-
|
497
|
-
parts = agent.split('/')
|
498
|
-
@browser = parts[0]
|
499
|
-
@browser_version = parts[1]
|
500
|
-
|
501
|
-
if @browser == 'Firefox'
|
502
|
-
@browser_version_major = parts[1].slice(0, 3)
|
503
|
-
@browser_version_minor = parts[1].sub(@browser_version_major, '').sub('.', '')
|
504
|
-
|
505
|
-
elsif @browser == 'Safari'
|
506
|
-
rx = Regexp.new("Version/([0-9]+)\.([0-9]+)\.([0-9]+)")
|
507
|
-
rx.match(user_agent)
|
508
|
-
|
509
|
-
@browser_version_major = $1
|
510
|
-
@browser_version_minor = $2
|
511
|
-
@browser_version_sub = $3
|
512
|
-
|
513
|
-
unless $1
|
514
|
-
|
515
|
-
v1_0_3 = []
|
516
|
-
v1_0_3 << "85.8.1"
|
517
|
-
v1_0_3 << "85"
|
518
|
-
v1_0_3 << "85.8"
|
519
|
-
|
520
|
-
|
521
|
-
vs = parts[1].split("\.")
|
522
|
-
vs1 = vs[0].to_i
|
523
|
-
if vs1 < 126
|
524
|
-
@browser_version_major = '1'
|
525
|
-
|
526
|
-
if vs[1].to_i < 8
|
527
|
-
@browser_version_minor = '0'
|
528
|
-
@browser_version_sub = '0'
|
529
|
-
elsif vs[1].to_i >= 8
|
530
|
-
@browser_version_minor = '0'
|
531
|
-
@browser_version_sub = '3'
|
532
|
-
end
|
533
|
-
|
534
|
-
end
|
535
|
-
|
536
|
-
end
|
537
|
-
|
538
|
-
else
|
539
|
-
# puts "#{__FILE__}:#{__LINE__} #{__method__} here---"
|
540
|
-
@browser_version_major = parts[1].slice(0, 1)
|
541
|
-
end
|
542
|
-
}
|
543
|
-
end
|
544
|
-
|
545
|
-
|
546
|
-
if !known_browser(@browser) then
|
547
|
-
|
548
|
-
@browser = 'Safari' if is_safari?(user_agent) || known_safari_agent_string(user_agent)
|
549
|
-
|
550
|
-
end
|
551
|
-
|
552
|
-
# I'm thinking of making this a lazy call....
|
553
|
-
mobile_browser?(user_agent)
|
554
|
-
console_browser?(user_agent)
|
555
|
-
|
556
|
-
# grab all of the properties (within parens)
|
557
|
-
# should be in relation to the agent if possible
|
558
|
-
@detail = @user_agent
|
559
|
-
@user_agent.gsub(/\((.*)\)/, '').split(/\s/).each {|part| @detail = @detail.gsub(part, '')}
|
560
|
-
@detail = @detail.gsub('(', '').gsub(')', '').lstrip
|
561
|
-
@properties = @detail.split(/;\s+/)
|
562
|
-
|
563
|
-
# cycle through the properties to set known quantities
|
564
|
-
@properties.each {|property|
|
565
|
-
if property =~ /^Win/
|
566
|
-
@ostype = 'Windows'
|
567
|
-
@os = property
|
568
|
-
if parts = property.split(/ /, 2)
|
569
|
-
if parts[1] =~ /^NT/
|
570
|
-
@ostype = 'Windows'
|
571
|
-
subparts = parts[1].split(/ /, 2)
|
572
|
-
if subparts[1] == '5'
|
573
|
-
@os_version = '2000'
|
574
|
-
elsif subparts[1] == '5.1'
|
575
|
-
@os_version = 'XP'
|
576
|
-
else
|
577
|
-
@os_version = subparts[1]
|
578
|
-
end
|
579
|
-
end
|
580
|
-
end
|
581
|
-
end
|
582
|
-
if property == 'Macintosh'
|
583
|
-
@ostype = 'Macintosh'
|
584
|
-
@os = property
|
585
|
-
end
|
586
|
-
if property =~ /OS X/
|
587
|
-
@ostype = 'Macintosh'
|
588
|
-
@os_version = 'OS X'
|
589
|
-
@os = property
|
590
|
-
end
|
591
|
-
if property =~ /^Linux/
|
592
|
-
@ostype = 'Linux'
|
593
|
-
@os = property
|
594
|
-
end
|
595
|
-
if property =~ /^MSIE/
|
596
|
-
@browser = 'MSIE'
|
597
|
-
@browser_version = property.gsub('MSIE ', '').lstrip
|
598
|
-
@browser_version_major, @browser_version_minor = @browser_version.split('.')
|
599
|
-
end
|
600
|
-
}
|
601
|
-
self
|
602
|
-
end
|
603
|
-
|
604
|
-
=end
|
605
422
|
end
|
606
423
|
end
|
data/spy-vs-spy.gemspec
CHANGED
data/test/ss-test.rb
CHANGED
@@ -35,7 +35,7 @@ class SpyVsSpyTest < Test::Unit::TestCase
|
|
35
35
|
while (line = file.gets)
|
36
36
|
|
37
37
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} #{line}"
|
38
|
-
r = SoldierOfCode::
|
38
|
+
r = SoldierOfCode::SpyVsSpy.new.parse(line)
|
39
39
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} BROWSER: #{r.browser}"
|
40
40
|
assert r.browser == 'Safari'
|
41
41
|
|
@@ -82,7 +82,7 @@ class SpyVsSpyTest < Test::Unit::TestCase
|
|
82
82
|
while (line = file.gets)
|
83
83
|
|
84
84
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} #{line}"
|
85
|
-
r = SoldierOfCode::
|
85
|
+
r = SoldierOfCode::SpyVsSpy.new.parse(line)
|
86
86
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} BROWSER: #{r.browser}"
|
87
87
|
assert r.browser == 'Firefox'
|
88
88
|
|
@@ -129,7 +129,7 @@ class SpyVsSpyTest < Test::Unit::TestCase
|
|
129
129
|
while (line = file.gets)
|
130
130
|
|
131
131
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} #{line}"
|
132
|
-
r = SoldierOfCode::
|
132
|
+
r = SoldierOfCode::SpyVsSpy.new.parse(line)
|
133
133
|
# puts "#{__FILE__}:#{__LINE__} #{__method__} BROWSER: #{r.browser}"
|
134
134
|
assert r.browser == 'MSIE'
|
135
135
|
|