divining_rod 0.1.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mark Percival
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,50 @@
1
+ # Divining Rod
2
+
3
+ A tool to help format your sites mobile pages. The goal is to have the ability to do things in your
4
+ views like this
5
+
6
+ <%- if request.dv_profile.youtube_capable? %>
7
+ <%= link_to "YouTube Video", @link.youtube_url %>
8
+ <%- else %>
9
+ Sorry, you have a shitty phone.
10
+ <% end %>
11
+
12
+ ## Installation
13
+
14
+ gem install divining_rod
15
+
16
+ ## Usage
17
+
18
+ require 'divining_rod'
19
+
20
+ DiviningRod::Matchers.define do |map|
21
+ map.ua /iPhone/, :webkit, :tags => [:iphone, :youtube_capable]
22
+ map.ua /Android/, :webkit, :tags => [:youtube_capable, :google_gears]
23
+ map.default :unknown
24
+ end
25
+
26
+ profile = DiviningRod::Profile(request) #profile and incoming iphone request
27
+ profile.format #=> :webkit
28
+ profile.iphone? #=> true
29
+
30
+ ## Note on the development
31
+
32
+ This is still very much in beta, but we are using in extensively in production. We plan
33
+ to keep the API the same.
34
+
35
+ The user agent definitions will be updated here later this week.
36
+
37
+
38
+ ### Note on Patches/Pull Requests
39
+
40
+ * Fork the project.
41
+ * Make your feature addition or bug fix.
42
+ * Add tests for it. This is important so I don't break it in a
43
+ future version unintentionally.
44
+ * Commit, do not mess with rakefile, version, or history.
45
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
46
+ * Send me a pull request. Bonus points for topic branches.
47
+
48
+ ### Copyright
49
+
50
+ Copyright (c) 2010 Mark Percival. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "divining_rod"
8
+ gem.summary = %Q{A mobile phone web request profiler}
9
+ gem.description = %Q{A mobile phone web request profiler using definitions that look like rails routes}
10
+ gem.email = "mark@mpercival.com"
11
+ gem.homepage = "http://github.com/markpercival/divining_rod"
12
+ gem.authors = ["Mark Percival"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "divining_rod #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{divining_rod}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mark Percival"]
12
+ s.date = %q{2010-02-17}
13
+ s.description = %q{A mobile phone web request profiler using definitions that look like rails routes}
14
+ s.email = %q{mark@mpercival.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "divining_rod.gemspec",
27
+ "lib/divining_rod.rb",
28
+ "spec/basic_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/markpercival/divining_rod}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A mobile phone web request profiler}
36
+ s.test_files = [
37
+ "spec/basic_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ end
54
+
@@ -0,0 +1,89 @@
1
+ require 'yaml'
2
+
3
+ module DiviningRod
4
+
5
+ class UndefinedDefault < StandardError
6
+
7
+ end
8
+
9
+ class Profile
10
+
11
+ attr_reader :match
12
+
13
+ def initialize(request)
14
+ matchers.each do |matcher|
15
+ @match = matcher if matcher.matches?(request)
16
+ break if @match
17
+ end
18
+ raise UndefinedDefault, "Please define a default group for DiviningRod" unless @match
19
+ end
20
+
21
+ def group
22
+ @match.group
23
+ end
24
+ alias_method :format, :group
25
+
26
+ def method_missing(meth)
27
+ if meth.to_s.match(/(.+)\?$/)
28
+ tag = $1
29
+ @match.tags.include?(tag.to_s) || @match.tags.include?(tag.to_sym) || @match.tags == tag
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def matchers
36
+ DiviningRod::Matchers.definitions || []
37
+ end
38
+
39
+ end
40
+
41
+ class Matchers
42
+
43
+ class << self
44
+
45
+ attr_accessor :definitions
46
+
47
+ def define
48
+ yield(self)
49
+ end
50
+
51
+ def clear_definitions
52
+ @definitions = []
53
+ end
54
+
55
+ def ua(pattern, group, opts={})
56
+ @definitions ||= []
57
+ @definitions << Definition.new(group, opts) { |request|
58
+ if pattern.match(request.user_agent)
59
+ true
60
+ end
61
+ }
62
+ end
63
+
64
+ def default(group, opts = {})
65
+ @definitions ||= []
66
+ @definitions << Definition.new(group, opts) { |request| true }
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ class Definition
74
+
75
+ attr_accessor :prc, :tags, :group
76
+
77
+ def initialize(group, opts={}, &blk)
78
+ @group = group
79
+ @tags = opts[:tags] || []
80
+ @prc = blk
81
+ end
82
+
83
+ def matches?(request)
84
+ !!@prc.call(request)
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe DiviningRod do
4
+
5
+ before :each do
6
+ @request = mock("rails_request", :user_agent => 'My iPhone')
7
+ DiviningRod::Matchers.clear_definitions
8
+ DiviningRod::Matchers.define do |map|
9
+ map.ua /iPhone/, :webkit, :tags => [:iphone, :youtube, :geolocate]
10
+ map.default(:unknown)
11
+ end
12
+ end
13
+
14
+ it "should recognize an iPhone" do
15
+ profile = DiviningRod::Profile.new(@request)
16
+ profile.format.should eql(:webkit)
17
+ end
18
+
19
+ it "should know if it belongs to a category tag" do
20
+ profile = DiviningRod::Profile.new(@request)
21
+ profile.geolocate?.should be_true
22
+ end
23
+
24
+ it "should know if it does not belongs to a category" do
25
+ profile = DiviningRod::Profile.new(@request)
26
+ profile.wap?.should be_false
27
+ end
28
+
29
+ it "should use the default group for unknown phones" do
30
+ @request = mock("rails_request", :user_agent => 'My Foo Phone')
31
+ profile = DiviningRod::Profile.new(@request)
32
+ profile.wap?.should be_false
33
+ end
34
+
35
+ end
36
+
37
+
38
+ describe DiviningRod::Matchers do
39
+
40
+ before :each do
41
+ @request = mock("rails_request", :user_agent => 'iPhone Foo')
42
+ DiviningRod::Matchers.clear_definitions
43
+ DiviningRod::Matchers.define do |map|
44
+ map.ua /iPhone/, :iphone, :tags => [:iphone, :youtube]
45
+ end
46
+ end
47
+
48
+ it "should recognize an iPhone" do
49
+ DiviningRod::Matchers.definitions.first.matches?(@request).should be_true
50
+ DiviningRod::Matchers.definitions.first.group.should eql(:iphone)
51
+ end
52
+
53
+ describe "defining a default definition" do
54
+
55
+ before :each do
56
+ @request = mock("rails_request", :user_agent => 'Foo Fone')
57
+ DiviningRod::Matchers.clear_definitions
58
+ DiviningRod::Matchers.define do |map|
59
+ map.default :unknown, :tags => [:html]
60
+ end
61
+ end
62
+
63
+ it "should use the default route if no other match is found" do
64
+ DiviningRod::Matchers.definitions.first.matches?(@request).should be_true
65
+ DiviningRod::Matchers.definitions.first.group.should eql(:unknown)
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path('../../lib/divining_rod', __FILE__)
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: divining_rod
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Percival
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-17 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A mobile phone web request profiler using definitions that look like rails routes
26
+ email: mark@mpercival.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - divining_rod.gemspec
42
+ - lib/divining_rod.rb
43
+ - spec/basic_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/markpercival/divining_rod
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A mobile phone web request profiler
73
+ test_files:
74
+ - spec/basic_spec.rb
75
+ - spec/spec_helper.rb