uagent 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,12 +2,68 @@
2
2
 
3
3
  Helps you to develop web applications for desktop or mobile user agents.
4
4
 
5
+ == Install
6
+
7
+ $ gem install uagent
8
+
5
9
  == Use
6
10
 
11
+ require 'rubygmes'
12
+ require 'uagent'
13
+
14
+ # A parser distinguishes between disjoin user agent sets
15
+
16
+ parser = UAgent::Parser.new([:desktop, :mobile])
17
+
18
+ ua = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
19
+ "AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
20
+
21
+ parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :mobile
22
+
23
+ ua = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) " +
24
+ "Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10"
25
+
26
+ parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :desktop
27
+
28
+ Also you can add more keywords to get a more specific device
29
+ classification.
30
+
31
+ parser = UAgent::Parser.new([:desktop, :mobile, :iphone])
32
+
33
+ ua = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
34
+ "AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
35
+
36
+ parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :iphone
37
+
38
+ ua = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) " +
39
+ "AppleWebKit/530.17 (KHTML, like Gecko) Version/6.0.0.62 Mobile Safari/530.17"
40
+
41
+ parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :mobile
42
+
43
+ Now we are working with the keywords :desktop, :mobile, :iphone and
44
+ :blackberry. Look the tests for more information.
45
+
46
+ == Install from code
47
+
48
+ First download the code from the repository:
49
+
50
+ $ git clone git@github.com:danielhz/uagent.git
51
+
52
+ This project uses jeweler to build the gem, so you can use this commands:
53
+
54
+ $ rake build # to build the gem
55
+ $ rake install # to build and install the gem in one step
56
+
57
+ Also, if you want test the gem you can use the spec task:
58
+
59
+ $ rake spec
60
+
61
+ This project uses rcov so you can check the coverage opening the HTML
62
+ file in the coverage directory after running the spec.
63
+
7
64
  == Other Stuff
8
65
 
9
66
  Author:: Daniel Hernández, daniel@degu.cl
10
- Version:: 0.0
11
67
  License:: GPL V3
12
68
 
13
69
  == Warranty
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/uagent.rb CHANGED
@@ -1,9 +1,55 @@
1
+
2
+
1
3
  module UAgent
2
4
 
3
- class Selector
5
+ class Parser
6
+
7
+ @@keys = [['Mobile', :mobile],
8
+ ['Opera Mini', :mobile],
9
+ ['iPhone', :iphone, :mobile],
10
+ ['ACER', :mobile],
11
+ ['Alcatel', :mobile],
12
+ ['AUDIOVOX', :mobile],
13
+ ['BlackBerry', :blackberry, :mobile],
14
+ ['CDM', :mobile],
15
+ ['Ericsson', :mobile],
16
+ ['LG', :mobile],
17
+ ['LGE', :mobile],
18
+ ['Motorola', :mobile],
19
+ ['MOT', :mobile],
20
+ ['NEC', :mobile],
21
+ ['Nokia', :mobile],
22
+ ['Panasonic', :mobile],
23
+ ['QCI', :mobile],
24
+ ['SAGEM', :mobile],
25
+ ['SAMSUNG', :mobile],
26
+ ['SEC', :mobile],
27
+ ['Sanyo', :mobile],
28
+ ['Sendo', :mobile],
29
+ ['SHARP', :mobile],
30
+ ['SonyEricsson', :mobile],
31
+ ['Telit', :mobile],
32
+ ['Telit_mobile_Terminals', :mobile],
33
+ ['TSM', :mobile],
34
+ ['Palm', :mobile]]
35
+ @@priority = [:iphone, :blackberry, :mobile]
36
+
37
+ def initialize(*keys)
38
+ @keys = [:desktop, :mobile] + keys
39
+ end
4
40
 
5
- def initialize(keys)
6
- @keys = keys
41
+ def call(env)
42
+ # Check devices in http user agent
43
+ http_user_agent = env['HTTP_USER_AGENT']
44
+ @@priority.select{ |k| @keys.include? k }.each do |key|
45
+ @@keys.each do |sample|
46
+ if /#{sample[0]}/ === http_user_agent
47
+ return key if sample[(1...sample.size)].include? key
48
+ end
49
+ end
50
+ end
51
+ # As device is not found, return the default
52
+ return @keys[0]
7
53
  end
8
54
 
9
55
  end
data/rakefile CHANGED
@@ -1,16 +1,16 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
- require 'rake/testtask'
3
+ require 'spec/rake/spectask'
4
4
  require 'rake/rdoctask'
5
5
 
6
- desc 'Default: run unit tests.'
7
- task :default => :test
6
+ desc 'Default: run rspec tests.'
7
+ task :default => :spec
8
8
 
9
9
  desc 'Run all tests'
10
- Rake::TestTask.new(:test) do |t|
11
- t.libs << 'lib'
12
- t.pattern = 'test/**/*_test.rb'
13
- t.verbose = true
10
+ Spec::Rake::SpecTask.new(:spec) do |t|
11
+ t.spec_files = Dir.glob('test/*_test.rb')
12
+ t.spec_opts << '--format specdoc'
13
+ t.rcov = true
14
14
  end
15
15
 
16
16
  begin
@@ -21,7 +21,31 @@ begin
21
21
  gemspec.email = "daniel@degu.cl"
22
22
  gemspec.homepage = "http://github.com/danielhz/uagent"
23
23
  gemspec.summary = "Helps you to develop web applications for diferent user agents"
24
- gemspec.add_dependency('rack', '>= 0.9.1')
24
+ gemspec.rubyforge_project = "uagent"
25
+ gemspec.description = "Helps you to develop for diferent http user agents."
25
26
  end
26
27
  rescue LoadError
27
28
  end
29
+
30
+ begin
31
+ require 'darkfish-rdoc'
32
+ DARKFISH_ENABLED = true
33
+ rescue LoadError => ex
34
+ DARKFISH_ENABLED = false
35
+ end
36
+
37
+ BASE_RDOC_OPTIONS = [
38
+ '--line-numbers', '--inline-source',
39
+ '--main' , 'README.rdoc',
40
+ '--title', 'uagent'
41
+ ]
42
+
43
+ rd = Rake::RDocTask.new do |rdoc|
44
+ rdoc.rdoc_dir = 'html'
45
+ rdoc.title = "uagent"
46
+ rdoc.options = BASE_RDOC_OPTIONS.dup
47
+ rdoc.options << '-SHN' << '-f' << 'darkfish' if DARKFISH_ENABLED
48
+ rdoc.options << '--charset' << 'utf-8'
49
+ rdoc.rdoc_files.include('README.rdoc')
50
+ rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
51
+ end
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'uagent'
3
+
4
+ describe 'UAgent::Parser' do
5
+
6
+ before(:all) do
7
+ end
8
+
9
+ it 'distinguishes between basic user agent sets' do
10
+ parser = UAgent::Parser.new
11
+
12
+ ua = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
13
+ "AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
14
+ env = { 'HTTP_USER_AGENT' => ua }
15
+ parser.call(env).should == :mobile
16
+
17
+ ua = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) " +
18
+ "AppleWebKit/530.17 (KHTML, like Gecko) Version/6.0.0.62 Mobile Safari/530.17"
19
+ env = { 'HTTP_USER_AGENT' => ua }
20
+ parser.call(env).should == :mobile
21
+
22
+ ua = "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; DROIDX Build/VZW) " +
23
+ "AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 " +
24
+ "480X854 motorola DROIDX"
25
+ env = { 'HTTP_USER_AGENT' => ua }
26
+ parser.call(env).should == :mobile
27
+
28
+ ua = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) " +
29
+ "Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10"
30
+ env = { 'HTTP_USER_AGENT' => ua }
31
+ parser.call(env).should == :desktop
32
+ end
33
+
34
+ it 'distinguishes user agent subsets' do
35
+ parser = UAgent::Parser.new(:iphone, :blackberry)
36
+
37
+ ua = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
38
+ "AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
39
+ env = { 'HTTP_USER_AGENT' => ua }
40
+ parser.call(env).should == :iphone
41
+
42
+ ua = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) " +
43
+ "AppleWebKit/530.17 (KHTML, like Gecko) Version/6.0.0.62 Mobile Safari/530.17"
44
+ env = { 'HTTP_USER_AGENT' => ua }
45
+ parser.call(env).should == :blackberry
46
+
47
+ ua = "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; DROIDX Build/VZW) " +
48
+ "AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 " +
49
+ "480X854 motorola DROIDX"
50
+ env = { 'HTTP_USER_AGENT' => ua }
51
+ parser.call(env).should == :mobile
52
+
53
+ ua = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) " +
54
+ "Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10"
55
+ env = { 'HTTP_USER_AGENT' => ua }
56
+ parser.call(env).should == :desktop
57
+ end
58
+
59
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Daniel Hern\xC3\xA1ndez"
@@ -14,24 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-23 00:00:00 -04:00
17
+ date: 2010-09-24 00:00:00 -04:00
18
18
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rack
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- - 9
30
- - 1
31
- version: 0.9.1
32
- type: :runtime
33
- version_requirements: *id001
34
- description:
19
+ dependencies: []
20
+
21
+ description: Helps you to develop for diferent http user agents.
35
22
  email: daniel@degu.cl
36
23
  executables: []
37
24
 
@@ -44,7 +31,7 @@ files:
44
31
  - VERSION
45
32
  - lib/uagent.rb
46
33
  - rakefile
47
- - test/uagent_test.rb
34
+ - test/parser_test.rb
48
35
  has_rdoc: true
49
36
  homepage: http://github.com/danielhz/uagent
50
37
  licenses: []
@@ -70,10 +57,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
57
  version: "0"
71
58
  requirements: []
72
59
 
73
- rubyforge_project:
60
+ rubyforge_project: uagent
74
61
  rubygems_version: 1.3.6
75
62
  signing_key:
76
63
  specification_version: 3
77
64
  summary: Helps you to develop web applications for diferent user agents
78
65
  test_files:
79
- - test/uagent_test.rb
66
+ - test/parser_test.rb
data/test/uagent_test.rb DELETED
@@ -1 +0,0 @@
1
- # Rspec tests