divining_rod
所属分类:WEB开发
开发工具:Ruby
文件大小:12KB
下载次数:0
上传日期:2011-05-12 17:22:36
上 传 者:
sh-1993
说明: 移动电话web请求探查器
(A mobile phone web request profiler)
文件列表:
.document (60, 2011-05-13)
Gemfile (66, 2011-05-13)
Gemfile.lock (158, 2011-05-13)
LICENSE (1096, 2011-05-13)
Rakefile (220, 2011-05-13)
divining_rod.gemspec (762, 2011-05-13)
example_config.rb (1475, 2011-05-13)
lib (0, 2011-05-13)
lib\divining_rod.rb (215, 2011-05-13)
lib\divining_rod (0, 2011-05-13)
lib\divining_rod\definition.rb (690, 2011-05-13)
lib\divining_rod\mappings.rb (1487, 2011-05-13)
lib\divining_rod\mash.rb (315, 2011-05-13)
lib\divining_rod\matchers.rb (659, 2011-05-13)
lib\divining_rod\murge.rb (404, 2011-05-13)
lib\divining_rod\profile.rb (783, 2011-05-13)
lib\divining_rod\rack.rb (41, 2011-05-13)
lib\divining_rod\rack (0, 2011-05-13)
lib\divining_rod\rack\divining_rack.rb (276, 2011-05-13)
lib\divining_rod\utilities.rb (397, 2011-05-13)
lib\divining_rod\version.rb (43, 2011-05-13)
test (0, 2011-05-13)
test\test_definition.rb (1385, 2011-05-13)
test\test_example_config.rb (2486, 2011-05-13)
test\test_helper.rb (441, 2011-05-13)
test\test_mash.rb (703, 2011-05-13)
# Divining Rod
A tool to profile web requests. Especially useful for mobile site development
## Installation
gem install divining_rod
## Example
Using the example configuration (found in [example_config.rb](http://github.com/markpercival/divining_rod/blob/master/example_config.rb))
# For a request with the user agent
# "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"
profile = DiviningRod::Profile.new(request)
profile.iphone? #=> true
profile.name #=> 'iPhone'
profile.youtube_capable? #=> true
profile.format #=> :webkit
## Mappings
Matches happen in the order they are defined, and then proceed down to the subsequent block. So for example:
DiviningRod::Mappings.define do |map|
map.ua /Apple/, :format => :webkit, :tags => [:apple, :iphone_os] do
iphone.ua /iPad/, :tags => :ipad, :name => 'iPad', :format => nil
iphone.ua /iPod/, :tags => :ipod, :name => 'iPod Touch'
iphone.ua /iPhone/, :tags => :iphone, :name => 'iPhone'
end
end
Will match "Apple iPad" first with the /Apple/ matcher, then with the /iPad/ matcher, and the tags will be
[:apple, :iphone_os, :ipad] # Notice tags get appended, *not* overridden.
And _:format_ will be set to _nil_
Why _nil_? Because when :format is set to _nil_ and you ask for it, DiviningRod will return the original _request_ objects format.
## Usage
_initializers/divining\_rod.rb_
DiviningRod::Mappings.define do |map|
# Android based phones
map.ua /Android/, :format => :webkit, :name => 'Android', :tags => [:android, :youtube_capable, :google_gears]
# Apple iPhone OS
map.ua /Apple.*Mobile.*Safari/, :format => :webkit, :tags => [:apple, :iphone_os, :youtube_capable] do |iphone|
iphone.ua /iPad/, :tags => :ipad, :name => 'iPad', :format => nil
iphone.ua /iPod/, :tags => :ipod, :name => 'iPod Touch'
iphone.ua /iPhone/, :tags => :iphone, :name => 'iPhone'
end
#Blackberry, needs more detail here
map.ua /BlackBerry/, :tags => :blackberry, :name => 'BlackBerry'
map.subdomain /wap/, :format => :wap, :tags => [:crappy_old_phone]
# Enable this to forces a default format if unmatched
# otherwise it will return the request.format
# map.default :format => :html
end
_initializers/mime\_types.rb_
Mime::Type.register_alias "text/html", :webkit
_app/controllers/mobile\_controller.rb_
class MobileController < ApplicationController
before_filter :detect_mobile_type
....
private
def detect_mobile_type
# If the profile isn't matched it defaults to request.format
@profile = DiviningRod::Profile.new(request)
request.format = @profile.format
end
end
_app/views/mobile/show.webkit.html_
<%- if @profile.iphone? %>
<%= link_to "Install our iPhone App in the AppStore", @iPhone_appstore_url %>
<%- elsif @profile.android? %>
<%= link_to "Direct download", @android_app_url %>
<% end %>
## Writing your own custom profiler
You can also include the DiviningRod::Profiler mixin in your own custom class
_lib/browser_profile.rb_
class BrowserProfile
include DivingingRod::Profiler
def has_an_app_store?
android? || iphone? || windows_phone?
end
end
Usage
prof = BrowserProfile.new(request)
prof.has_an_app_store?
## Todo
### Copyright
Copyright (c) 2010 Mark Percival. See LICENSE for details.
近期下载者:
相关文件:
收藏者: