useragent 0.0.4

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.
@@ -0,0 +1,336 @@
1
+ require 'user_agent'
2
+
3
+ describe UserAgent do
4
+ it "should require a product" do
5
+ lambda { UserAgent.new(nil) }.should raise_error(ArgumentError, "expected a value for product")
6
+ end
7
+
8
+ it "should split comment to any array if a string is passed in" do
9
+ useragent = UserAgent.new("Mozilla", "5.0", "Macintosh; U; Intel Mac OS X 10_5_3; en-us")
10
+ useragent.comment.should == ["Macintosh", "U", "Intel Mac OS X 10_5_3", "en-us"]
11
+ end
12
+
13
+ it "should set version to nil if it is blank" do
14
+ UserAgent.new("Mozilla", "").version.should be_nil
15
+ end
16
+
17
+ it "should only output product when coerced to a string" do
18
+ UserAgent.new("Mozilla").to_str.should == "Mozilla"
19
+ end
20
+
21
+ it "should output product and version when coerced to a string" do
22
+ UserAgent.new("Mozilla", "5.0").to_str.should == "Mozilla/5.0"
23
+ end
24
+
25
+ it "should output product, version and comment when coerced to a string" do
26
+ useragent = UserAgent.new("Mozilla", "5.0", ["Macintosh", "U", "Intel Mac OS X 10_5_3", "en-us"])
27
+ useragent.to_str.should == "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us)"
28
+ end
29
+
30
+ it "should output product and comment when coerced to a string" do
31
+ useragent = UserAgent.new("Mozilla", nil, ["Macintosh"])
32
+ useragent.to_str.should == "Mozilla (Macintosh)"
33
+ end
34
+
35
+ it "should be eql if both products are the same" do
36
+ UserAgent.new("Mozilla").should eql(UserAgent.new("Mozilla"))
37
+ end
38
+
39
+ it "should not be eql if both products are the same" do
40
+ UserAgent.new("Mozilla").should_not eql(UserAgent.new("Opera"))
41
+ end
42
+
43
+ it "should be eql if both products and versions are the same" do
44
+ UserAgent.new("Mozilla", "5.0").should eql(UserAgent.new("Mozilla", "5.0"))
45
+ end
46
+
47
+ it "should not be eql if both products and versions are not the same" do
48
+ UserAgent.new("Mozilla", "5.0").should_not eql(UserAgent.new("Mozilla", "4.0"))
49
+ end
50
+
51
+ it "should be eql if both products, versions and comments are the same" do
52
+ UserAgent.new("Mozilla", "5.0", ["Macintosh"]).should eql(UserAgent.new("Mozilla", "5.0", ["Macintosh"]))
53
+ end
54
+
55
+ it "should not be eql if both products, versions and comments are not the same" do
56
+ UserAgent.new("Mozilla", "5.0", ["Macintosh"]).should_not eql(UserAgent.new("Mozilla", "5.0", ["Windows"]))
57
+ end
58
+
59
+ it "should not be eql if both products, versions and comments are not the same" do
60
+ UserAgent.new("Mozilla", "5.0", ["Macintosh"]).should_not eql(UserAgent.new("Mozilla", "4.0", ["Macintosh"]))
61
+ end
62
+
63
+ it "should not be equal? if both products are the same" do
64
+ UserAgent.new("Mozilla").should_not equal(UserAgent.new("Mozilla"))
65
+ end
66
+
67
+ it "should be == if products are the same" do
68
+ UserAgent.new("Mozilla").should == UserAgent.new("Mozilla")
69
+ end
70
+
71
+ it "should be == if products and versions are the same" do
72
+ UserAgent.new("Mozilla", "5.0").should == UserAgent.new("Mozilla", "5.0")
73
+ end
74
+
75
+ it "should not be == if products and versions are the different" do
76
+ UserAgent.new("Mozilla", "5.0").should_not == UserAgent.new("Mozilla", "4.0")
77
+ end
78
+
79
+ it "should return false if comparing different products" do
80
+ UserAgent.new("Mozilla").should_not <= UserAgent.new("Opera")
81
+ end
82
+
83
+ it "should not be > if products are the same" do
84
+ UserAgent.new("Mozilla").should_not > UserAgent.new("Mozilla")
85
+ end
86
+
87
+ it "should not be < if products are the same" do
88
+ UserAgent.new("Mozilla").should_not < UserAgent.new("Mozilla")
89
+ end
90
+
91
+ it "should be >= if products are the same" do
92
+ UserAgent.new("Mozilla").should >= UserAgent.new("Mozilla")
93
+ end
94
+
95
+ it "should be <= if products are the same" do
96
+ UserAgent.new("Mozilla").should <= UserAgent.new("Mozilla")
97
+ end
98
+
99
+ it "should be > if products are the same and version is greater" do
100
+ UserAgent.new("Mozilla", "5.0").should > UserAgent.new("Mozilla", "4.0")
101
+ end
102
+
103
+ it "should not be > if products are the same and version is less" do
104
+ UserAgent.new("Mozilla", "4.0").should_not > UserAgent.new("Mozilla", "5.0")
105
+ end
106
+
107
+ it "should be < if products are the same and version is less" do
108
+ UserAgent.new("Mozilla", "4.0").should < UserAgent.new("Mozilla", "5.0")
109
+ end
110
+
111
+ it "should not be < if products are the same and version is greater" do
112
+ UserAgent.new("Mozilla", "5.0").should_not < UserAgent.new("Mozilla", "4.0")
113
+ end
114
+
115
+ it "should be >= if products are the same and version is greater" do
116
+ UserAgent.new("Mozilla", "5.0").should >= UserAgent.new("Mozilla", "4.0")
117
+ end
118
+
119
+ it "should not be >= if products are the same and version is less" do
120
+ UserAgent.new("Mozilla", "4.0").should_not >= UserAgent.new("Mozilla", "5.0")
121
+ end
122
+
123
+ it "should be <= if products are the same and version is less" do
124
+ UserAgent.new("Mozilla", "4.0").should <= UserAgent.new("Mozilla", "5.0")
125
+ end
126
+
127
+ it "should not be <= if products are the same and version is greater" do
128
+ UserAgent.new("Mozilla", "5.0").should_not <= UserAgent.new("Mozilla", "4.0")
129
+ end
130
+
131
+ it "should be >= if products are the same and version is the same" do
132
+ UserAgent.new("Mozilla", "5.0").should >= UserAgent.new("Mozilla", "5.0")
133
+ end
134
+
135
+ it "should be <= if products are the same and version is the same" do
136
+ UserAgent.new("Mozilla", "5.0").should <= UserAgent.new("Mozilla", "5.0")
137
+ end
138
+ end
139
+
140
+ describe UserAgent, "::MATCHER" do
141
+ it "should not match a blank line" do
142
+ UserAgent::MATCHER.should_not =~ ""
143
+ end
144
+
145
+ it "should match a single product" do
146
+ UserAgent::MATCHER.should =~ "Mozilla"
147
+ end
148
+
149
+ it "should match a product and version" do
150
+ UserAgent::MATCHER.should =~ "Mozilla/5.0"
151
+ end
152
+
153
+ it "should match a product, version, and comment" do
154
+ UserAgent::MATCHER.should =~ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us)"
155
+ end
156
+
157
+ it "should match a product, and comment" do
158
+ UserAgent::MATCHER.should =~ "Mozilla (Macintosh; U; Intel Mac OS X 10_5_3; en-us)"
159
+ end
160
+ end
161
+
162
+ describe UserAgent, ".parse" do
163
+ it "should concatenate user agents when coerced to a string" do
164
+ string = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18")
165
+ string.to_str.should == "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18"
166
+ end
167
+
168
+ it "should parse an empty string" do
169
+ UserAgent.parse("").should be_empty
170
+ end
171
+
172
+ it "should parse a single product" do
173
+ useragent = UserAgent.new("Mozilla")
174
+ UserAgent.parse("Mozilla").application.should == useragent
175
+ end
176
+
177
+ it "should parse a single product with version" do
178
+ useragent = UserAgent.new("Mozilla", "5.0")
179
+ UserAgent.parse("Mozilla/5.0").application.should == useragent
180
+ end
181
+
182
+ it "should parse a single product, version, and comment" do
183
+ useragent = UserAgent.new("Mozilla", "5.0", ["Macintosh", "U", "Intel Mac OS X 10_5_3", "en-us"])
184
+ UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us)").application.should == useragent
185
+ end
186
+
187
+ it "should parse a single product, version, and comment, with space-padded semicolons" do
188
+ useragent = UserAgent.new("Mozilla", "5.0", ["Macintosh", "U", "Intel Mac OS X 10_5_3", "en-us"])
189
+ UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3 ; en-us; )").application.should == useragent
190
+ end
191
+
192
+ it "should parse a single product and comment" do
193
+ useragent = UserAgent.new("Mozilla", nil, ["Macintosh"])
194
+ UserAgent.parse("Mozilla (Macintosh)").application.should == useragent
195
+ end
196
+ end
197
+
198
+ describe UserAgent::Browsers::All, "#<" do
199
+ before do
200
+ @ie_7 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
201
+ @ie_6 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
202
+ @firefox = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
203
+ end
204
+
205
+ it "should not be < if user agent does not have a browser" do
206
+ @ie_7.should_not < "Mozilla"
207
+ end
208
+
209
+ it "should not be < if user agent does not have the same browser" do
210
+ @ie_7.should_not < @firefox
211
+ end
212
+
213
+ it "should be < if version is less than its version" do
214
+ @ie_6.should < @ie_7
215
+ end
216
+
217
+ it "should not be < if version is the same as its version" do
218
+ @ie_6.should_not < @ie_6
219
+ end
220
+
221
+ it "should not be < if version is greater than its version" do
222
+ @ie_7.should_not < @ie_6
223
+ end
224
+ end
225
+
226
+ describe UserAgent::Browsers::All, "#<=" do
227
+ before do
228
+ @ie_7 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
229
+ @ie_6 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
230
+ @firefox = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
231
+ end
232
+
233
+ it "should not be <= if user agent does not have a browser" do
234
+ @ie_7.should_not <= "Mozilla"
235
+ end
236
+
237
+ it "should not be <= if user agent does not have the same browser" do
238
+ @ie_7.should_not <= @firefox
239
+ end
240
+
241
+ it "should be <= if version is less than its version" do
242
+ @ie_6.should <= @ie_7
243
+ end
244
+
245
+ it "should be <= if version is the same as its version" do
246
+ @ie_6.should <= @ie_6
247
+ end
248
+
249
+ it "should not be <= if version is greater than its version" do
250
+ @ie_7.should_not <= @ie_6
251
+ end
252
+ end
253
+
254
+ describe UserAgent::Browsers::All, "#==" do
255
+ before do
256
+ @ie_7 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
257
+ @ie_6 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
258
+ @firefox = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
259
+ end
260
+
261
+ it "should not be == if user agent does not have a browser" do
262
+ @ie_7.should_not == "Mozilla"
263
+ end
264
+
265
+ it "should not be == if user agent does not have the same browser" do
266
+ @ie_7.should_not == @firefox
267
+ end
268
+
269
+ it "should not be == if version is less than its version" do
270
+ @ie_6.should_not == @ie_7
271
+ end
272
+
273
+ it "should be == if version is the same as its version" do
274
+ @ie_6.should == @ie_6
275
+ end
276
+
277
+ it "should not be == if version is greater than its version" do
278
+ @ie_7.should_not == @ie_6
279
+ end
280
+ end
281
+
282
+ describe UserAgent::Browsers::All, "#>" do
283
+ before do
284
+ @ie_7 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
285
+ @ie_6 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
286
+ @firefox = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
287
+ end
288
+
289
+ it "should not be > if user agent does not have a browser" do
290
+ @ie_7.should_not > "Mozilla"
291
+ end
292
+
293
+ it "should not be > if user agent does not have the same browser" do
294
+ @ie_7.should_not > @firefox
295
+ end
296
+
297
+ it "should not be > if version is less than its version" do
298
+ @ie_6.should_not > @ie_7
299
+ end
300
+
301
+ it "should not be > if version is the same as its version" do
302
+ @ie_6.should_not > @ie_6
303
+ end
304
+
305
+ it "should be > if version is greater than its version" do
306
+ @ie_7.should > @ie_6
307
+ end
308
+ end
309
+
310
+ describe UserAgent::Browsers::All, "#>=" do
311
+ before do
312
+ @ie_7 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
313
+ @ie_6 = UserAgent.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
314
+ @firefox = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
315
+ end
316
+
317
+ it "should not be >= if user agent does not have a browser" do
318
+ @ie_7.should_not >= "Mozilla"
319
+ end
320
+
321
+ it "should not be >= if user agent does not have the same browser" do
322
+ @ie_7.should_not >= @firefox
323
+ end
324
+
325
+ it "should not be >= if version is less than its version" do
326
+ @ie_6.should_not >= @ie_7
327
+ end
328
+
329
+ it "should be >= if version is the same as its version" do
330
+ @ie_6.should >= @ie_6
331
+ end
332
+
333
+ it "should be >= if version is greater than its version" do
334
+ @ie_7.should >= @ie_6
335
+ end
336
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: useragent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Peek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-01 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: UserAgent is a Ruby library that parses and compares HTTP User Agents.
17
+ email: josh@joshpeek.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - lib/user_agent.rb
28
+ - lib/user_agent/browsers.rb
29
+ - lib/user_agent/browsers/all.rb
30
+ - lib/user_agent/browsers/gecko.rb
31
+ - lib/user_agent/browsers/internet_explorer.rb
32
+ - lib/user_agent/browsers/opera.rb
33
+ - lib/user_agent/browsers/webkit.rb
34
+ - lib/user_agent/comparable.rb
35
+ - lib/user_agent/operating_systems.rb
36
+ - lib/useragent.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/josh/useragent
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: HTTP User Agent parser
65
+ test_files:
66
+ - spec/browsers/gecko_user_agent_spec.rb
67
+ - spec/browsers/internet_explorer_user_agent_spec.rb
68
+ - spec/browsers/opera_user_agent_spec.rb
69
+ - spec/browsers/other_user_agent_spec.rb
70
+ - spec/browsers/webkit_user_agent_spec.rb
71
+ - spec/user_agent_spec.rb