Permalink
Browse files

Moved the browser detector into its own Gem.

  • Loading branch information...
1 parent 2f6ccf9 commit 704641e3a285cf2665d7b40c4cbaecc4d3379177 @midas committed Oct 20, 2009
View
@@ -1,6 +1,6 @@
= guilded
-http://github.com/midas/guilded/tree/master
+http://github.com/midas/guilded
== DESCRIPTION
@@ -77,7 +77,6 @@ have decided to include support for it in Guilded as of release 0.3.0.
an unpacked version of the file.
=== Additional common utilities
-* Determine the browser making a request with the BrowserDetector class and related view helpers.
* Extends ActiveRecord to have a attr_human_hint method. This method works similar to the attr_human_name method except a hint
to use on forms, etc is being set.
* Extends InactiveRecord (http://github.com/midas/inactive_record/tree/master) to include the attr_human_hint method.
@@ -112,15 +111,15 @@ have decided to include support for it in Guilded as of release 0.3.0.
== INSTALL
- gem sources -a http://gems.github.com
- sudo gem install midas-guilded
+ gem sources -a http://gemcutter.org
+ sudo gem install guilded
== INSTALL FOR RAILS
Add to environment file:
- config.gem "guilded", :version => '1.0.0'
+ config.gem "guilded", :version => '1.0.0', :source => 'http://gemcutter.org'
Run:
@@ -165,46 +164,6 @@ filter in your application controller:
Then install one or more Guilded components and you are on your way!
-=== BROWSER DETECTOR
-
-The browser detector uses the HTTP user agent string to determine which browser is making the current request. The detector
-can determine the name, version, major version, minor version, build version and revision version of the current browser.
-
-If using Rails, there are several view helpers available that use the BrowserDetector: g_browser_name(), g_browser_version(),
-g_browser_full_name() and g_browser_is?().
-
-The following examples will assume Firefox 3.5.3 on OS/X is being used. The user agent string for Firefox 3.5.3 on OS/X is:
-
- Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
-
-g_browser_name
-
- <% if g_browser_name == 'firefox' %>
- ...
-
-g_browser_version
-
- <% if g_browser_version == '3.5.3' %>
- ...
-
-g_browser_full_name
-
- <%= g_browser_full_name # Outputs: Firefox 3.5.3 %>
-
-g_browser_is?
-
- <% if g_browser_is?( :name => 'firefox', :version => '3.5.3' ) %>
- ...
-
- <% if g_browser_is?( :name => 'firefox', :major_version => '3' ) %>
- ...
-
- <% if g_browser_is?( :name => :firefox, :major_version => 3, :minor_version => 5, :build_version => 3 ) %>
- ...
-
- <% if g_browser_is?( :name => :firefox, :major_version => 3, :minor_version => 5, :build_version => 3, :revision_version => 0 ) %>
- ...
-
== LICENSE
View
@@ -10,6 +10,7 @@ begin
gem.email = "jason@lookforwardenterprises.com"
gem.homepage = "http://github.com/midas/guilded"
gem.authors = ["C. Jason Harrelson (midas)"]
+ gem.add_development_dependency "shoulda", ">= 2.10.2"
gem.add_dependency "activesupport", ">= 2.0.2"
end
Jeweler::GemcutterTasks.new
View
@@ -1,7 +1,6 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-require 'guilded/browser_detector'
require 'guilded/component_def'
require 'guilded/guilder'
require 'guilded/exceptions'
@@ -73,7 +72,7 @@
# <%= g_load_alerter :skin => 'blueish', :id => 'load_alerter' %>
#
module Guilded
- VERSION = '0.3.0'
+ VERSION = '1.0.1'
end
ActionView::Base.send( :include, Guilded::Rails::ViewHelpers ) if defined?( ActionView )
@@ -1,237 +0,0 @@
-module Guilded
-
- # The BrowserDetector provides the ability to determine browser information from the user
- # agent string.
- #
- class BrowserDetector
-
- attr_reader :ua
-
- def initialize( user_agent )
- @ua = user_agent.downcase
- @version_regex = /(\d*)\.(\d*)\.*(\d*)\.*(\d*)/
- end
-
- # Returns true if the browser matches the options ent in, otherwise returns false.
- #
- # === Request
- # * +request+ - The request object.
- #
- # === Options
- # * +:name+ - The name of the browser. For example 'ie' or :ie.
- # * +:version+ - The version of the browser. For example '3.0.5'.
- # * +:major_version+ - The major version of the browser. For example '3' or 3.
- # * +:minor_version+ - The minor version of the browser. For example '0' or 0.
- #
- def browser_is?( options={} )
- name = options[:name]
- version = options[:version]
- major_version = options[:major_version]
- minor_version = options[:minor_version]
- build_version = options[:build_version]
- revision_version = options[:revision_version]
-
- name ||= self.browser_name
- version ||= self.browser_version
- major_version ||= self.browser_version_major
- minor_version ||= self.browser_version_minor
- build_version ||= self.browser_version_build
- revision_version ||= self.browser_version_revision
-
- name = name.to_s.strip
- version = version.to_s.strip
- major_version = major_version.to_s.strip
- minor_version = minor_version.to_s.strip
- build_version = build_version.to_s.strip
- revision_version = revision_version.to_s.strip
-
- self.browser_name == name && self.browser_version == version && self.browser_version_major == major_version &&
- self.browser_version_minor == minor_version #&& self.browser_version_build == build_version &&
- #self.browser_version_revision == revision_version
- end
-
- # Returns the name of the browser that is making this request. For example 'ie'.
- #
- # === Request
- # * +request+ - The request object.
- #
- def browser_name
- begin
- @browser_name ||= begin
- ua = @ua
- if ua.nil?
- 'unknown'
- else
- if ua.index( 'msie' ) && !ua.index( 'opera' ) && !ua.index( 'webtv' )
- if ua.index( 'windows ce' )
- 'ie' + '_ce' #+ ua[ua.index( 'msie' ) + 5].chr
- else
- 'ie' # + ua[ua.index( 'msie' ) + 5].chr
- end
- elsif ua.include?( 'opera' )
- 'opera'
- elsif ua.include?( 'konqueror' )
- 'konqueror'
- elsif ua.include?( 'applewebkit/' )
- 'safari'
- elsif ua.include?( 'chrome' )
- 'chrome'
- #elsif ua.include?( 'mozilla/' )
- # 'firefox'
- elsif ua.include?( 'firefox' )
- 'firefox'
- #elsif ua.include?( 'gecko/' )
- # 'firefox'
- elsif ua.include?( 'netscape' )
- 'netscape'
- else
- 'unknown'
- end
- end
- end
- rescue
- 'unknown'
- end
- end
-
- # Returns the version of the browser that is making this request. For example '7'.
- #
- # === Request
- # * +request+ - The request object.
- #
- def browser_version
- @browser_version ||= begin
- self.send( "resolve_version_for_#{self.browser_name}".to_sym )
- end
- end
-
- def browser_version_major
- match = @version_regex.match( browser_version )
- return match[1].to_i.to_s unless match.nil? || match.size < 2
- '0'
- end
-
- def browser_version_minor
- match = @version_regex.match( browser_version )
- return match[2].to_i.to_s unless match.nil? || match.size < 3
- '0'
- end
-
- def browser_version_build
- match = @version_regex.match( browser_version )
- return match[3].to_i.to_s unless match.nil? || match.size < 4 || match[3].empty? || match[3].nil?
- '0'
- end
-
- def browser_version_revision
- match = @version_regex.match( browser_version )
- return match[4].to_i.to_s unless match.nil? || match.size < 5 || match[4].empty? || match[4].nil?
- '0'
- end
-
- # Returns the browser name concatenated with the browser version. for example, 'ie7'.
- #
- # === Request
- # * +request+ - The request object.
- #
- def browser_full_name
- self.send( "browser_full_name_for_#{self.browser_name}".to_sym )
- end
-
- # Returns the browser name concatenated with the browser version. for example, 'ie7'.
- #
- # === Request
- # * +request+ - The request object.
- #
- def browser_id
- browser_name + browser_version.gsub( /\./, '' )
- end
-
- # Returns true if the current browser type can handle PNGs.
- #
- def can_use_png?
- return browser_version.to_i >= 7 if browser_name== 'ie'
- true
- end
-
- # A list of all the browser types that Guilded recognizes.
- #
- def self.all_browsers
- %w( chrome firefox ie55 ie60 ie70 ie80 konqueror netscape opera safari )
- end
-
- # A list of all the mobile browser types that Guilded recognizes.
- #
- def self.all_mobile_browsers
- %w( ie_ce4 iphone )
- end
-
- def self.user_agents
- {
- :ie55 => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)',
- :ie60 => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
- :ie70 => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
- :ie80 => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
- :firefox2 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.17) Gecko/20080829 Firefox/2.0.0.17',
- :firefox3 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11',
- :firefox35 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3',
- :firefox35win => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)',
- :opera10 => 'Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.00',
- :safari403 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Chrome/4.0.221.8 Safari/532.2',
- :iphone3 => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7C144 Safari/528.16',
- :iphone2 => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A535b Safari/419.3',
- :chrome3 => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.27 Safari/532.0',
- :chrome4 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Chrome/4.0.221.8 Safari/532.2'
- }
- end
-
- protected
-
- def resolve_version_for_ie
- match = /.*msie (.*); windows nt 5.1/.match( @ua )
- return match.nil? ? '0' : match[1]
- end
-
- def resolve_version_for_firefox
- match = /.*\((.*); u;.*firefox\/(.*) \(.net.*/.match( @ua )
- if match.nil?
- match = /.*\((.*); u;.*firefox\/(.*)/.match( @ua )
- return match.nil? ? '0' : match[2]
- end
- return match.nil? ? '0' : match[2]
- end
-
- def resolve_version_for_opera
- match = /.*\((.*); intel.*version\/(.*)/.match( @ua )
- return match.nil? ? '0' : match[2]
- end
-
- def resolve_version_for_safari
- match = /.* safari\/(.*)/.match( @ua )
- rev = match.nil? ? 0 : match[1]
- return case rev
- when '528.16' then '4.0.0'
- when '530.18' then '4.0.1'
- when '530.19' then '4.0.2'
- when '531.9', '532.2' then '4.0.3'
- else '0.0.0.0'
- end
- end
-
- def browser_full_name_for_ie
- "Internet Explorer #{browser_version}"
- end
-
- def browser_full_name_for_firefox
- "Firefox #{browser_version}"
- end
-
- def browser_full_name_for_opera
- "Opera #{browser_version}"
- end
-
- def browser_full_name_for_safari
- "Safari #{browser_version}"
- end
- end
-end
Oops, something went wrong.

0 comments on commit 704641e

Please sign in to comment.