divining_rod 0.6.2 → 0.6.3

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.markdown CHANGED
@@ -8,12 +8,12 @@ A tool to profile web requests. Especially useful for mobile site development
8
8
  gem install divining_rod
9
9
 
10
10
  ## Example
11
-
11
+
12
12
  Using the example configuration (found in [example_config.rb](http://github.com/markpercival/divining_rod/blob/master/example_config.rb))
13
-
13
+
14
14
  # For a request with the user agent
15
15
  # "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20"
16
-
16
+
17
17
  profile = DiviningRod::Profile.new(request)
18
18
  profile.iphone? #=> true
19
19
  profile.name #=> 'iPhone'
@@ -31,9 +31,9 @@ Matches happen in the order they are defined, and then proceed down to the subse
31
31
  iphone.ua /iPhone/, :tags => :iphone, :name => 'iPhone'
32
32
  end
33
33
  end
34
-
34
+
35
35
  Will match "Apple iPad" first with the /Apple/ matcher, then with the /iPad/ matcher, and the tags will be
36
-
36
+
37
37
  [:apple, :iphone_os, :ipad] # Notice tags get appended, *not* overridden.
38
38
 
39
39
  And _:format_ will be set to _nil_
@@ -43,7 +43,7 @@ Why _nil_? Because when :format is set to _nil_ and you ask for it, DiviningRod
43
43
  ## Usage
44
44
 
45
45
  _initializers/divining\_rod.rb_
46
-
46
+
47
47
  DiviningRod::Mappings.define do |map|
48
48
  # Android based phones
49
49
  map.ua /Android/, :format => :webkit, :name => 'Android', :tags => [:android, :youtube_capable, :google_gears]
@@ -58,16 +58,16 @@ _initializers/divining\_rod.rb_
58
58
  #Blackberry, needs more detail here
59
59
  map.ua /BlackBerry/, :tags => :blackberry, :name => 'BlackBerry'
60
60
  map.subdomain /wap/, :format => :wap, :tags => [:crappy_old_phone]
61
-
61
+
62
62
  # Enable this to forces a default format if unmatched
63
63
  # otherwise it will return the request.format
64
- # map.default :format => :html
64
+ # map.default :format => :html
65
65
  end
66
66
 
67
67
  _initializers/mime\_types.rb_
68
-
68
+
69
69
  Mime::Type.register_alias "text/html", :webkit
70
-
70
+
71
71
  _app/controllers/mobile\_controller.rb_
72
72
 
73
73
  class MobileController < ApplicationController
@@ -84,7 +84,7 @@ _app/controllers/mobile\_controller.rb_
84
84
  end
85
85
 
86
86
  end
87
-
87
+
88
88
  _app/views/mobile/show.webkit.html_
89
89
 
90
90
  <%- if @profile.iphone? %>
@@ -93,11 +93,25 @@ _app/views/mobile/show.webkit.html_
93
93
  <%= link_to "Direct download", @android_app_url %>
94
94
  <% end %>
95
95
 
96
-
97
- ## Note on the development
96
+ ## Writing your own custom profiler
97
+
98
+ You can also include the DiviningRod::Profiler mixin in your own custom class
99
+
100
+ _lib/browser_profile.rb_
101
+
102
+ class BrowserProfile
103
+ include DivingingRod::Profiler
104
+
105
+ def has_an_app_store?
106
+ android? || iphone? || windows_phone?
107
+ end
108
+
109
+ end
110
+
111
+ Usage
98
112
 
99
- In version 0.3.* it was assumed you always passed in _format_. In 0.4 on, we require _format_ to
100
- be passed in explicitly with the rest of the options hash.
113
+ prof = BrowserProfile.new(request)
114
+ prof.has_an_app_store?
101
115
 
102
116
  ## Todo
103
117
 
data/example_config.rb CHANGED
@@ -19,6 +19,7 @@ DiviningRod::Mappings.define(:format => :html) do |map|
19
19
  desktop.ua /Safari/, :tags => :safari, :name => 'Safari'
20
20
  desktop.ua /Opera/, :tags => :opera, :name => 'Opera'
21
21
  desktop.ua /MSIE/, :tags => :ie, :name => 'Internet Explorer' do |msie|
22
+ msie.ua /MSIE 5\.5/
22
23
  msie.ua /MSIE 6/, :version => 6
23
24
  msie.ua /MSIE 7/, :version => 7
24
25
  msie.ua /MSIE 8/, :version => 8
@@ -21,6 +21,10 @@ module DiviningRod
21
21
 
22
22
  def initialize(parent, default_opts = {})
23
23
  @parent = parent
24
+ unless default_opts == {}
25
+ p "Passed in default_opts"
26
+ p default_opts
27
+ end
24
28
  @default_opts = Mash.new(default_opts)
25
29
  end
26
30
 
@@ -46,7 +50,8 @@ module DiviningRod
46
50
  def method_missing(meth, *args, &blk)
47
51
  # Lets us use map.ua instead of map.pattern :ua
48
52
  if Matchers.respond_to?(meth.to_sym)
49
- self.pattern(meth, args[0], args[1,], &blk)
53
+ opts = args[1,] || {}
54
+ self.pattern(meth, args[0], opts, &blk)
50
55
  else
51
56
  super
52
57
  end
@@ -55,8 +60,8 @@ module DiviningRod
55
60
  private
56
61
 
57
62
  def merged_opts(opts)
58
- opts = parent.opts.merge(opts)
59
63
  opts = default_opts.merge(opts)
64
+ opts = parent.opts.merge(opts)
60
65
  opts
61
66
  end
62
67
 
@@ -65,6 +70,5 @@ module DiviningRod
65
70
  definition
66
71
  end
67
72
 
68
-
69
73
  end
70
74
  end
@@ -5,8 +5,8 @@ module DiviningRod
5
5
  self.replace(hsh || {})
6
6
  end
7
7
 
8
- def merge(opts = {})
9
- return self if opts.nil?
8
+ def merge(opts)
9
+ opts = opts.dup
10
10
  if self[:tags] || opts[:tags]
11
11
  tags = Array(self[:tags]) | Array(opts[:tags])
12
12
  opts[:tags] = tags
@@ -1,3 +1,3 @@
1
1
  module DiviningRod
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -1,69 +1,76 @@
1
1
  require 'test_helper'
2
- require 'example_config'
2
+
3
+ DiviningRod::Mappings.define(:format => :html, :tags => [:foo]) do |map|
4
+ map.ua /First/, :name => 'First', :tags => [:first]
5
+ map.ua /SecondMatch/, :name => 'Second', :tags => [:second]
6
+ map.with_options :tags => :grouping1 do |with_options|
7
+ with_options.ua /ThirdMatch/, :name => "ThirdMatch", :tags => [:third_match]
8
+ with_options.ua /FourthMatch/, :name => "FourthMatch", :tags => [:fourth_match]
9
+ end
10
+ map.ua /FifthMatch/, :name => 'FifthMatch', :tags => [:fifth_match, :and_more]
11
+ map.ua /ThirdMatch/, :name => "Failure", :tags => [:failed] # Shouldn't get here
12
+
13
+ map.ua /Stuffed Crust/, :name => 'Stuffed Crust', :tags => [:woo_hoo] do |sc|
14
+ sc.ua /Vegan/, :name => 'Bleh'
15
+ sc.with_options :name => "Meat and Cheese" do |mac|
16
+ mac.ua /With Bacon/, :tags => :bacon
17
+ mac.ua /With Ham/, :tags => :round_bacon
18
+ mac.ua /With Tofu/, :tags => :tofu, :name => "NotMeat and Cheese"
19
+ end
20
+ end
21
+
22
+ map.with_options :tags => :second_group do |second_group|
23
+ second_group.ua /ThirdMatch/
24
+ second_group.ua /SixthMatch/
25
+ end
26
+
27
+ map.ua /Last/, :tags => :last
28
+
29
+ map.default :name => "Unknown", :tags => :unknown
30
+ end
3
31
 
4
32
  def profile_ua(ua)
5
33
  DiviningRod::Profile.new(request_mock(:ua => ua))
6
34
  end
7
35
 
8
-
9
36
  class ExampleConfigTest < Test::Unit::TestCase
10
37
  context 'the example config' do
11
38
 
12
- context "recognizing Apple devices" do
13
-
14
- should "recognize an iPad" do
15
- profile = profile_ua("Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10")
16
- assert profile.ipad?
17
- end
18
-
19
- should "recognize an iPod touch 2.2.1" do
20
- profile = profile_ua("Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20")
21
- assert profile.ipod?
22
- end
23
-
24
- should "recognize an iPod touch 3.1.2" do
25
- profile = profile_ua("Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16")
26
- assert profile.ipod?
27
- end
28
-
29
- should "recognize an iPhone 3.1.2" do
30
- profile = profile_ua("Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16")
31
- assert profile.iphone?
32
- end
33
-
34
- should "recognize an iPhone 2.1.2" do
35
- profile = profile_ua("Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20")
36
- assert profile.iphone?
39
+ context "with root definition options" do
40
+ should "carry through" do
41
+ profile = profile_ua("Unknown phone")
42
+ assert_equal "Unknown", profile.name
43
+ assert_equal :html, profile.format
37
44
  end
38
-
39
45
  end
40
46
 
41
- context "recognizing Android devices" do
42
-
43
- should "recognize a basic android" do
44
- profile = profile_ua("Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2")
45
- assert profile.android?
47
+ context "after a with_options block" do
48
+ should "not affect subsequest matches" do
49
+ profile = profile_ua("Last")
50
+ assert_equal [:foo, :last], profile.tags
46
51
  end
47
-
48
52
  end
49
53
 
50
- context "recognizing desktop browsers" do
51
-
52
- should "recognize Internet Explorer 10" do
53
- profile = profile_ua("Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 7.0)")
54
- assert profile.desktop?
55
- assert_equal profile.version, 10
56
- assert profile.html5?
54
+ context "matching a sub options group" do
55
+ should "overide the parent" do
56
+ profile = profile_ua("Stuffed Crust With Tofu")
57
+ assert_equal "NotMeat and Cheese", profile.name
58
+ profile = profile_ua("Stuffed Crust With Bacon")
59
+ assert_equal "Meat and Cheese", profile.name
57
60
  end
61
+ end
58
62
 
63
+ should "match with tags" do
64
+ profile = profile_ua("FifthMatch")
65
+ assert_equal "FifthMatch", profile.name
66
+ assert profile.and_more?
59
67
  end
60
68
 
61
- context "with root definition options" do
62
- should "carry through" do
63
- profile = profile_ua("Unknown phone")
64
- assert_equal "Unknown", profile.name
65
- assert_equal :html, profile.format
66
- end
69
+ should "stop at the first match" do
70
+ profile = profile_ua("ThirdMatch")
71
+ assert profile.third_match?
72
+ assert profile.grouping1?
73
+ assert !profile.failed?
67
74
  end
68
75
 
69
76
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: divining_rod
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.2
5
+ version: 0.6.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mark Percival
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-25 00:00:00 Z
13
+ date: 2011-05-12 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: A DSL for writing user agent profiles
@@ -76,4 +76,3 @@ specification_version: 3
76
76
  summary: A mobile request profiler
77
77
  test_files: []
78
78
 
79
- has_rdoc: