Class: Brauser::Browser
- Inherits:
-
Object
- Object
- Brauser::Browser
- Includes:
- Brauser::BrowserMethods::Attributes, Brauser::BrowserMethods::General, Brauser::BrowserMethods::Parsing, Brauser::BrowserMethods::PartialQuerying, Brauser::BrowserMethods::Querying, Brauser::BrowserMethods::Register
- Defined in:
- lib/brauser/browser.rb
Overview
This class represents a detection of the current user browser.
Instance Attribute Summary collapse
-
#accept_language ⇒ String
The raw Accept-Language HTTP header.
-
#agent ⇒ String
(also: #ua)
The raw User-Agent HTTP header.
-
#languages ⇒ Array
The accepted languages.
-
#name ⇒ String
The current browser name.
-
#platform ⇒ String
The current browser platform.
-
#version ⇒ String
The current browser version.
Instance Method Summary collapse
-
#initialize(agent = "", accept_language = "") ⇒ Browser
constructor
Creates a new browser.
-
#method_missing(query, *arguments, &block) ⇒ Boolean|Query|nil
This method enables the use of dynamic queries in just one method.
-
#to_s ⇒ String
Returns the current browser as a string.
Methods included from Brauser::BrowserMethods::Querying
Methods included from Brauser::BrowserMethods::PartialQuerying
Methods included from Brauser::BrowserMethods::Parsing
#parse_accept_language, #parse_agent
Methods included from Brauser::BrowserMethods::Attributes
#classes, #platform_name, #readable_name
Constructor Details
#initialize(agent = "", accept_language = "") ⇒ Browser
Creates a new browser.
749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/brauser/browser.rb', line 749 def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(query, *arguments, &block) ⇒ Boolean|Query|nil
This method enables the use of dynamic queries in just one method.
For example:
browser.is_msie_gt_4_1__on_windows?
#=> true
If you don't provide a trailing ?
, you will get a Brauser::Query.
If the syntax is invalid, a NoMethodError
exception will be raised.
778 779 780 781 782 783 784 785 786 |
# File 'lib/brauser/browser.rb', line 778 def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end |
Instance Attribute Details
#accept_language ⇒ String
Returns The raw Accept-Language HTTP header.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
#agent ⇒ String Also known as: ua
Returns The raw User-Agent HTTP header.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
#languages ⇒ Array
Returns The accepted languages.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
#name ⇒ String
Returns The current browser name.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
#platform ⇒ String
Returns The current browser platform.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
#version ⇒ String
Returns The current browser version.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/brauser/browser.rb', line 726 class Browser attr_accessor :agent attr_accessor :accept_language attr_accessor :languages attr_accessor :name attr_accessor :version attr_accessor :platform # Aliases alias :ua :agent alias :ua= :agent= include ::Brauser::BrowserMethods::General include ::Brauser::BrowserMethods::Attributes include ::Brauser::BrowserMethods::Register include ::Brauser::BrowserMethods::Parsing include ::Brauser::BrowserMethods::PartialQuerying include ::Brauser::BrowserMethods::Querying # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") ::Brauser::Browser.register_default_browsers ::Brauser::Browser.register_default_platforms ::Brauser::Browser.register_default_languages @agent = agent @accept_language = accept_language @languages = self.parse_accept_language(@accept_language) if @accept_language self.parse_agent(@agent) if @agent end # This method enables the use of dynamic queries in just one method. # # For example: # # ```ruby # browser.is_msie_gt_4_1__on_windows? # #=> true # ``` # # If you don't provide a trailing `?`, you will get a Brauser::Query. # # If the syntax is invalid, a `NoMethodError` exception will be raised. # # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @param arguments [Array] The arguments to pass the method. Unused from the query. # @param block [Proc] A block to pass to the method. Unused from the query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def method_missing(query, *arguments, &block) begin parsed_query = parse_query(query.ensure_string) rv = execute_query(parsed_query) || Brauser::Query.new(self, false) query.ensure_string =~ /\?$/ ? rv.result : rv rescue NoMethodError super(query, *arguments, &block) end end # Returns the current browser as a string. # # @see #classes # # @return [String] A string representation of the current browser. def to_s self.classes end private # Parse query, getting all arguments. # @param query [String] The query to issue. Use `__` to separate query and `_` in place of `.` in the version. # @return [Array] And array of `[method, arguments]` entries. def parse_query(query) query.gsub(/\?$/, "").split("__").collect do |part| parse_query_part(part) end end # Handles a part of a query. # # @param part [String] A part of a query. # @return [Boolean|Query|nil] A query or a boolean value (if `method` ends with `?`). If the query is not valid, `NoMethodError` will be raised. def parse_query_part(part) method, arguments = part.split("_", 2) if method == "v" then arguments = parse_query_version(arguments) elsif !["is", "on"].include?(method) raise NoMethodError end [method, arguments] end # Parses the version for a query. # # @param version [String] The version to parse. # @return [String] The parsed version. def parse_query_version(version) [ [/_?eq_?/, " == "], # Parse == [/_?lte_?/, " <= "], # Parse <= [/_?gte_?/, " >= "], # Parse >= [/_?lt_?/, " < "], # Parse < [/_?gt_?/, " > "], # Parse > [/_?and_?/, " && "], # Parse && ["_", "."], # Dot notation [/\s+/, " "] ].inject(version) { |current, parse| current.gsub(parse[0], parse[1])}.strip end # Executes a parsed query # # @param query [Array] And array of `[method, arguments]` entries. # @return [Brauser::Query] The result of the query. def execute_query(query) query.inject(Brauser::Query.new(self, true)) { |rv, call| break if !rv.result rv.send(call[0], *call[1]) } end end |
Instance Method Details
#to_s ⇒ String
Returns the current browser as a string.
793 794 795 |
# File 'lib/brauser/browser.rb', line 793 def to_s self.classes end |