uagent 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +56 -19
- data/VERSION +1 -1
- data/lib/uagent.rb +3 -2
- data/rakefile +22 -24
- data/{test/parser_test.rb → spec/parser_spec.rb} +1 -0
- metadata +14 -9
data/README.rdoc
CHANGED
@@ -6,48 +6,85 @@ Helps you to develop web applications for desktop or mobile user agents.
|
|
6
6
|
|
7
7
|
$ gem install uagent
|
8
8
|
|
9
|
-
==
|
9
|
+
== Usage
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
When a browser sends a request to your web application the request has
|
12
|
+
an HTTP_USER_AGENT attribute that identifies the device an
|
13
|
+
browser. For example, the next string identifies an particular model
|
14
|
+
of iPhone, that uses an specific mobile version for Safari.
|
13
15
|
|
14
|
-
|
16
|
+
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us)
|
17
|
+
AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11
|
18
|
+
Safari/528.16
|
15
19
|
|
16
|
-
|
20
|
+
Each mobile phone model will send you a particular user agent string,
|
21
|
+
thus is a good idea classify the devices in groups an develop a
|
22
|
+
specific interface for each group. For each group we define a key, so
|
23
|
+
the user agent parser receive the user agent string and returns that
|
24
|
+
key. For example, the answer for the string above could be :mobile or
|
25
|
+
:iphone, depending on the parser configuration.
|
17
26
|
|
18
|
-
|
19
|
-
|
27
|
+
If you are developing web applications on top of rack, I recommend
|
28
|
+
you to look the uagent_rack[http://github.com/danielhz/uagent_rack]
|
29
|
+
gem.
|
20
30
|
|
21
|
-
|
31
|
+
=== Code example
|
22
32
|
|
23
|
-
|
24
|
-
|
33
|
+
require 'rubygmes'
|
34
|
+
require 'uagent'
|
25
35
|
|
36
|
+
# A parser distinguishes between disjoin user agent sets
|
37
|
+
|
38
|
+
parser = UAgent::Parser.new
|
39
|
+
|
40
|
+
env = {
|
41
|
+
...
|
42
|
+
"HTTP_USER_AGENT" => "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
|
43
|
+
"AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16",
|
44
|
+
...
|
45
|
+
}
|
46
|
+
|
47
|
+
parser.call(env) # gets :mobile
|
48
|
+
|
49
|
+
env = {
|
50
|
+
...
|
51
|
+
"HTTP_USER_AGENT" => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) " +
|
52
|
+
"Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10"
|
53
|
+
...
|
54
|
+
}
|
55
|
+
|
26
56
|
parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :desktop
|
27
57
|
|
28
|
-
|
29
|
-
classification.
|
58
|
+
=== Using specific user agent groups.
|
30
59
|
|
31
|
-
|
60
|
+
The basic user agent groups are :desktop and :mobile. You can add more
|
61
|
+
groups adding keys in the constructor function.
|
62
|
+
|
63
|
+
parser = UAgent::Parser.new(:iphone)
|
32
64
|
|
33
65
|
ua = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) " +
|
34
66
|
"AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
|
35
67
|
|
36
68
|
parser.call({ 'HTTP_USER_AGENT' => 'ua }) # gets :iphone
|
37
69
|
|
38
|
-
|
39
|
-
|
70
|
+
The keys order in the constructor is relevant. Put the most specific
|
71
|
+
keys first to indicate the parser to start for them.
|
72
|
+
|
73
|
+
=== Using a custom device database.
|
40
74
|
|
41
|
-
|
75
|
+
The parser uses a hash that associates terms to the user agent group
|
76
|
+
keyword. That terms are searched in the HTTP_USER_AGENT string to
|
77
|
+
analyze if a user agent is into a group. Now we support only the
|
78
|
+
keywords :iphone and :blackberry, thus change the database if you need
|
79
|
+
a more accurate parser.
|
42
80
|
|
43
|
-
|
44
|
-
:blackberry. Look the tests for more information.
|
81
|
+
parser.set_database my_custom_database
|
45
82
|
|
46
83
|
== Install from code
|
47
84
|
|
48
85
|
First download the code from the repository:
|
49
86
|
|
50
|
-
$ git clone git
|
87
|
+
$ git clone git://github.com/danielhz/uagent.git
|
51
88
|
|
52
89
|
This project uses jeweler to build the gem, so you can use this commands:
|
53
90
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/uagent.rb
CHANGED
@@ -11,9 +11,10 @@ module UAgent
|
|
11
11
|
'BlackBerry', 'CDM', 'Ericsson', 'LG', 'LGE', 'Motorola', 'MOT',
|
12
12
|
'NEC', 'Nokia', 'Panasonic', 'QCI', 'SAGEM', 'SAMSUNG', 'SEC',
|
13
13
|
'Sanyo', 'Sendo', 'SHARP', 'SonyEricsson', 'Telit',
|
14
|
-
'Telit_mobile_Terminals', 'TSM', 'Palm'],
|
14
|
+
'Telit_mobile_Terminals', 'TSM', 'Palm', 'ViewPad'],
|
15
15
|
:iphone => ['iPhone'],
|
16
|
-
:blackberry => ['BlackBerry']
|
16
|
+
:blackberry => ['BlackBerry'],
|
17
|
+
:android => ['ViewPad7']
|
17
18
|
}
|
18
19
|
|
19
20
|
def initialize(*specific_keys)
|
data/rakefile
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
require 'rubygems'
|
2
3
|
require 'rake'
|
3
|
-
require '
|
4
|
-
require '
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'yard'
|
7
|
+
require 'rcov'
|
5
8
|
|
6
9
|
desc 'Default: run rspec tests.'
|
7
10
|
task :default => :spec
|
8
11
|
|
9
12
|
desc 'Run all tests'
|
10
|
-
|
11
|
-
t.
|
12
|
-
|
13
|
+
RSpec::Core::RakeTask.new(:specs) do |t|
|
14
|
+
t.rspec_opts = ["--colour", "--format", "documentation"]
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Coverage for all specs'
|
18
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
13
19
|
t.rcov = true
|
20
|
+
t.rcov_opts = %q[--exclude "spec"]
|
21
|
+
t.verbose = true
|
14
22
|
end
|
15
23
|
|
16
24
|
begin
|
@@ -27,25 +35,15 @@ begin
|
|
27
35
|
rescue LoadError
|
28
36
|
end
|
29
37
|
|
30
|
-
|
31
|
-
|
32
|
-
DARKFISH_ENABLED = true
|
33
|
-
rescue LoadError => ex
|
34
|
-
DARKFISH_ENABLED = false
|
38
|
+
YARD::Rake::YardocTask.new do |t|
|
39
|
+
t.options += ['--title', "Uagent documentation"]
|
35
40
|
end
|
36
41
|
|
37
|
-
|
38
|
-
|
39
|
-
'
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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')
|
42
|
+
desc 'Build and upload the last gem file'
|
43
|
+
task :gem_push => :build do
|
44
|
+
gem = Dir['pkg/*.gem'].map { |f|
|
45
|
+
[f, File.basename(f).gsub(/.gem$/, '').gsub(/^uagent-/, '').split('.').map{ |x| x.to_i}]
|
46
|
+
}.sort.last
|
47
|
+
puts "gem push #{gem[0]}"
|
48
|
+
system "gem push #{gem[0]}"
|
51
49
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uagent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- "Daniel Hern\xC3\xA1ndez"
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-09-05 00:00:00 -03:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -31,36 +32,40 @@ files:
|
|
31
32
|
- VERSION
|
32
33
|
- lib/uagent.rb
|
33
34
|
- rakefile
|
34
|
-
-
|
35
|
+
- spec/parser_spec.rb
|
35
36
|
has_rdoc: true
|
36
37
|
homepage: http://github.com/danielhz/uagent
|
37
38
|
licenses: []
|
38
39
|
|
39
40
|
post_install_message:
|
40
|
-
rdoc_options:
|
41
|
-
|
41
|
+
rdoc_options: []
|
42
|
+
|
42
43
|
require_paths:
|
43
44
|
- lib
|
44
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
45
47
|
requirements:
|
46
48
|
- - ">="
|
47
49
|
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
48
51
|
segments:
|
49
52
|
- 0
|
50
53
|
version: "0"
|
51
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
52
56
|
requirements:
|
53
57
|
- - ">="
|
54
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
55
60
|
segments:
|
56
61
|
- 0
|
57
62
|
version: "0"
|
58
63
|
requirements: []
|
59
64
|
|
60
65
|
rubyforge_project: uagent
|
61
|
-
rubygems_version: 1.
|
66
|
+
rubygems_version: 1.5.0
|
62
67
|
signing_key:
|
63
68
|
specification_version: 3
|
64
69
|
summary: Helps you to develop web applications for diferent user agents
|
65
70
|
test_files:
|
66
|
-
-
|
71
|
+
- spec/parser_spec.rb
|