mobvious 0.1.1 → 0.1.2

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 Jiri Stransky
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the Software
8
+ is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ IN THE SOFTWARE.
@@ -0,0 +1,142 @@
1
+ # Mobvious
2
+
3
+ Mobvious detects whether your app / website is being accessed by a phone, or by a tablet,
4
+ or by a personal computer.
5
+
6
+ ## Key Features
7
+
8
+ * **It's easy to get running.** Just a Rack middleware with almost no config. (See
9
+ section Get Started below.)
10
+
11
+ * **It supports multiple strategies for detection.** If you don't like the default ones,
12
+ you can easily write your own. (See section Detection Strategies below.)
13
+
14
+ * **Works with Rails, Sinatra, or whatever.** Does not depend on any application
15
+ framework, uses just Rack.
16
+
17
+ * **[Documentation](http://rdoc.info/github/jistr/mobvious/frames).**
18
+
19
+
20
+ ## Get Started
21
+
22
+ 1. **Include Mobvious in your Gemfile**: `gem 'mobvious'`
23
+
24
+ 2. **Tell your app to use Mobvious::Manager as Rack middleware.**
25
+ If you use Rails, simply add `config.middleware.use Mobvious::Manager` into your
26
+ `config/application.rb` file.
27
+
28
+ 3. **Tell Mobvious which strategies it should use.**
29
+ A good place to put this code for Rails is an initializer – create a file
30
+ `config/initializers/mobvious.rb` and put this in:
31
+
32
+ Mobvious.configure do
33
+ strategies = [ Mobvious::Strategies::MobileESP.new ]
34
+ end
35
+
36
+ 4. **Done! From now on, device type is detected for each request.**
37
+ The information is
38
+ in a Rack environment variable `env['mobvious.device_type']`, this variable will
39
+ have a value of `:desktop` or `:mobile` depending on the device type that issued
40
+ the request. In Rails, you can access it via `request.env['mobvious.device_type']`.
41
+
42
+ *This is just a very basic way of setting up Mobvious. If you want to detect
43
+ tablets separately, or let the user manually switch between interface versions of your
44
+ app, or do some funnier stuff, see sections Detection Process and Detection Strategies.*
45
+
46
+ ## Detection Process
47
+
48
+ Mobvious uses an array of strategies to detect the device type.
49
+ Strategies are evaluated in order of appearance in the array. Each strategy may or
50
+ may not be successful in determining the device type. The result of the first successful
51
+ strategy is used. If no strategy is successful, the implicit device type is used
52
+ (defaults to `:desktop`, but this is configurable via `default_device_type` attribute
53
+ in the `configure` block).
54
+
55
+
56
+ ## Detection Strategies
57
+
58
+ ### MobileESP (User-Agent sniffing)
59
+
60
+ `Mobvious::Strategies::MobileESP`
61
+
62
+ Selects the device type using information present in the User-Agent HTTP header.
63
+
64
+ Constructor takes a detection procedure.
65
+ Detection procedure decides what device type it should return based on the
66
+ information it can dig out of MobileESPConverted::UserAgentInfo instance.
67
+
68
+ There are two predefined detection procedures (and you can write your own):
69
+
70
+ * `DEVICE_TYPES_MOBILE_DESKTOP` (this is the default)
71
+ distinguishes between `:mobile` and `:desktop`. Tablets
72
+ are reported as `:desktop`, because their screens are usually large enough to handle
73
+ web interfaces meant for desktops.
74
+
75
+ * `DEVICE_TYPES_MOBILE_TABLET_DESKTOP` distinguishes between `:mobile`, `:tablet`
76
+ and `:desktop`.
77
+
78
+ ### URL (URL pattern matching)
79
+
80
+ `Mobvious::Strategies::URL`
81
+
82
+ Selects the device type by matching a pattern against the request's URL (whole URL,
83
+ including protocol information).
84
+
85
+ Constructor takes a hash of rules in format `/regular_expression/ => :device_type`.
86
+
87
+ There is one predefined rule set:
88
+
89
+ * `MOBILE_PATH_RULES` detects all URLs that begin with m. (e.g. `http://m.foo.com/`)
90
+ as `:mobile`. Doesn't make assumption about other URLs (the detection process
91
+ continues to the next strategy in order).
92
+
93
+ ### Cookie (remembering user's manual choice)
94
+
95
+ `Mobvious::Strategies::Cookie`
96
+
97
+ This strategy is useful when user should be able to make a manual switch between
98
+ interface versions and you want all the interface versions running on the exact same URL.
99
+
100
+ Call this anywhere in your app:
101
+
102
+ Mobvious.strategy('Cookie').set_device_type(response, :desktop)
103
+
104
+ … and Mobvious will report `:desktop` from now on for this particular user, regardless
105
+ of what is his/her real device type. (Response parameter is your Rack::Response instance.
106
+ In Rails it is accessible simply by writing `response`, as shown in the code example above.)
107
+ Make sure to put the Cookie strategy high enough in your strategies array
108
+ (the first entry?) so it does not get overriden by some other strategy.
109
+
110
+ Constructor takes array of allowed device types ("whitelist") that your
111
+ application supports. This is a countermeasure to users tampering with cookies. When
112
+ the device type read from cookie is not whitelisted, the strategy passes the detection
113
+ process to other strategies.
114
+
115
+ ### Writing Your Own Strategy
116
+
117
+ It's super-easy. A valid Mobvious strategy is any object that responds to this method:
118
+
119
+ def get_device_type(request)
120
+ # some code here
121
+ end
122
+
123
+ The request parameter is an object of type Rack::Request. The method must return either:
124
+
125
+ * **a symbol** denoting the device type detected (strategy was successful), or
126
+ * **nil** denoting that strategy was unsuccessful and detection process should continue
127
+ with other strategies (or return the implicit device type).
128
+
129
+
130
+ Optionally, you can also implement this method:
131
+
132
+ def response_callback(request, response)
133
+ # some code here
134
+ end
135
+
136
+ It gets called after a response is returned from the application and you can tweak the
137
+ response here if you want. The parameters are instances of Rack::Request
138
+ and Rack::Response, respectively. The method is not expected to return anything special.
139
+
140
+ ---
141
+
142
+ ***Everyone goes with the defaults anyway*** ![cereal guy](https://github.com/engina/9gagtension/raw/master/rages/cereal-guy.jpg)
@@ -40,4 +40,11 @@ module Mobvious
40
40
  matching_strategies
41
41
  end
42
42
  end
43
+
44
+
45
+ module Strategies
46
+ autoload :Cookie, 'mobvious/strategies/cookie'
47
+ autoload :MobileESP, 'mobvious/strategies/mobileesp'
48
+ autoload :URL, 'mobvious/strategies/url'
49
+ end
43
50
  end
@@ -4,7 +4,7 @@ module Mobvious
4
4
  module Strategies
5
5
  # Mobvious device detection strategy that uses user-agent sniffing provided by
6
6
  # the MobileESP library.
7
- class Mobileesp
7
+ class MobileESP
8
8
  # Detection procedure that classifies mobile phones as `:mobile` and anything
9
9
  # else as `:desktop`.
10
10
  DEVICE_TYPES_MOBILE_DESKTOP = lambda {|mobileesp|
@@ -23,7 +23,7 @@ module Mobvious
23
23
  # Creates a new instance of MobileESP strategy.
24
24
  #
25
25
  # @param detection_procedure
26
- # A lambda function that gets one parameter (`MobileESP::UserAgentInfo` instance)
26
+ # A lambda function that gets one parameter (`MobileESPConverted::UserAgentInfo` instance)
27
27
  # and returns device type symbol or nil.
28
28
  def initialize(detection_procedure = DEVICE_TYPES_MOBILE_DESKTOP)
29
29
  @detection_procedure = detection_procedure
@@ -11,7 +11,7 @@ module Mobvious
11
11
  # A hash containing regular expressions mapped to symbols. The regular expression
12
12
  # is evaluated against the whole URL of the request (including `http://`). If matching,
13
13
  # the corresponding symbol is returned as the device type.
14
- def initialize(rules = MOBILE_PATH_RULE)
14
+ def initialize(rules = MOBILE_PATH_RULES)
15
15
  @rules = rules
16
16
  end
17
17
 
@@ -1,3 +1,3 @@
1
1
  module Mobvious
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -8,7 +8,7 @@ describe Mobvious do
8
8
  require 'mobvious/strategies/mobileesp'
9
9
  @cookie = Mobvious::Strategies::Cookie.new [:mobile, :desktop]
10
10
  @cookie2 = Mobvious::Strategies::Cookie.new [:mobile, :desktop]
11
- @mobileesp = Mobvious::Strategies::Mobileesp.new
11
+ @mobileesp = Mobvious::Strategies::MobileESP.new
12
12
  Mobvious.config.strategies = [
13
13
  @cookie,
14
14
  @cookie2,
@@ -23,7 +23,7 @@ describe Mobvious do
23
23
  end
24
24
 
25
25
  it "gets the right strategy by class name" do
26
- Mobvious.strategy('Mobileesp').must_equal @mobileesp
26
+ Mobvious.strategy('MobileESP').must_equal @mobileesp
27
27
  end
28
28
 
29
29
  it "gets an array of strategies if there is more with the same name" do
@@ -1,5 +1,4 @@
1
1
  require_relative '../../spec_helper'
2
- require 'mobvious/strategies/cookie'
3
2
 
4
3
  module Mobvious::Strategies
5
4
  class CookieSpec < MiniTest::Spec
@@ -1,12 +1,11 @@
1
1
  require_relative '../../spec_helper'
2
- require 'mobvious/strategies/mobileesp'
3
2
 
4
3
  module Mobvious::Strategies
5
- class MobileespSpec < MiniTest::Spec
6
- describe Mobileesp do
4
+ class MobileESPSpec < MiniTest::Spec
5
+ describe MobileESP do
7
6
  describe "using mobile_desktop strategy" do
8
7
  before do
9
- @strategy = Mobvious::Strategies::Mobileesp.new
8
+ @strategy = Mobvious::Strategies::MobileESP.new
10
9
  @request = mock 'request'
11
10
  @env = mock 'env'
12
11
 
@@ -32,8 +31,8 @@ module Mobvious::Strategies
32
31
 
33
32
  describe "using mobile_tablet_desktop strategy" do
34
33
  before do
35
- @strategy = Mobvious::Strategies::Mobileesp.new(
36
- Mobvious::Strategies::Mobileesp::DEVICE_TYPES_MOBILE_TABLET_DESKTOP)
34
+ @strategy = Mobvious::Strategies::MobileESP.new(
35
+ Mobvious::Strategies::MobileESP::DEVICE_TYPES_MOBILE_TABLET_DESKTOP)
37
36
  @request = mock 'request'
38
37
  @env = mock 'env'
39
38
 
@@ -1,5 +1,4 @@
1
1
  require_relative '../../spec_helper'
2
- require 'mobvious/strategies/url'
3
2
 
4
3
  module Mobvious::Strategies
5
4
  class URLSpec < MiniTest::Spec
@@ -14,7 +13,7 @@ class URLSpec < MiniTest::Spec
14
13
  'PATH_INFO' => '/some_path'
15
14
  })
16
15
  @request = Rack::Request.new(@env)
17
- @strategy = URL.new(URL::MOBILE_PATH_RULES)
16
+ @strategy = URL.new
18
17
  end
19
18
 
20
19
  it "returns the right device type when matching rule found" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobvious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &13559960 !ruby/object:Gem::Requirement
16
+ requirement: &16340920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *13559960
24
+ version_requirements: *16340920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mobileesp_converted
27
- requirement: &13559160 !ruby/object:Gem::Requirement
27
+ requirement: &16340180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *13559160
35
+ version_requirements: *16340180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: pry
38
- requirement: &13558600 !ruby/object:Gem::Requirement
38
+ requirement: &16339620 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *13558600
46
+ version_requirements: *16339620
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: minitest
49
- requirement: &13557860 !ruby/object:Gem::Requirement
49
+ requirement: &16338520 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *13557860
57
+ version_requirements: *16338520
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &13556780 !ruby/object:Gem::Requirement
60
+ requirement: &16337520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *13556780
68
+ version_requirements: *16337520
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rack-test
71
- requirement: &13555880 !ruby/object:Gem::Requirement
71
+ requirement: &16336960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *13555880
79
+ version_requirements: *16336960
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard
82
- requirement: &13555380 !ruby/object:Gem::Requirement
82
+ requirement: &16336240 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *13555380
90
+ version_requirements: *16336240
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-minitest
93
- requirement: &13554440 !ruby/object:Gem::Requirement
93
+ requirement: &16335020 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *13554440
101
+ version_requirements: *16335020
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rb-inotify
104
- requirement: &13642280 !ruby/object:Gem::Requirement
104
+ requirement: &16423360 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *13642280
112
+ version_requirements: *16423360
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: libnotify
115
- requirement: &13641840 !ruby/object:Gem::Requirement
115
+ requirement: &16422940 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *13641840
123
+ version_requirements: *16422940
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: turn
126
- requirement: &13641400 !ruby/object:Gem::Requirement
126
+ requirement: &16422460 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,7 +131,7 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *13641400
134
+ version_requirements: *16422460
135
135
  description: Rack middleware for choosing a version of an interface to render for
136
136
  given request
137
137
  email:
@@ -144,6 +144,8 @@ files:
144
144
  - .yardopts
145
145
  - Gemfile
146
146
  - Guardfile
147
+ - LICENSE
148
+ - README.md
147
149
  - Rakefile
148
150
  - lib/mobvious.rb
149
151
  - lib/mobvious/config.rb
@@ -173,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
175
  version: '0'
174
176
  segments:
175
177
  - 0
176
- hash: 2708548775781943004
178
+ hash: -1726316769951614505
177
179
  required_rubygems_version: !ruby/object:Gem::Requirement
178
180
  none: false
179
181
  requirements:
@@ -182,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
184
  version: '0'
183
185
  segments:
184
186
  - 0
185
- hash: 2708548775781943004
187
+ hash: -1726316769951614505
186
188
  requirements: []
187
189
  rubyforge_project:
188
190
  rubygems_version: 1.8.11