Class: Brauser::Browser

Overview

This class represents a detection of the current user browser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Brauser::BrowserMethods::Querying

#accepts?, #is?, #on?, #v?

Methods included from Brauser::BrowserMethods::PartialQuerying

#accepts, #is, #on, #v

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.

Parameters:

  • agent (String) (defaults to: "")

    The User-Agent HTTP header.

  • accept_language (String) (defaults to: "")

    The Accept-Language HTTP header.



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.

Parameters:

  • query (String)

    The query to issue. Use __ to separate query and _ in place of . in the version.

  • arguments (Array)

    The arguments to pass the method. Unused from the query.

  • block (Proc)

    A block to pass to the method. Unused from the query.

Returns:

  • (Boolean|Query|nil)

    A query or a boolean value (if method ends with ?). If the query is not valid, NoMethodError 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_languageString

Returns The raw Accept-Language HTTP header.

Returns:

  • (String)

    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

#agentString Also known as: ua

Returns The raw User-Agent HTTP header.

Returns:

  • (String)

    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

#languagesArray

Returns The accepted languages.

Returns:

  • (Array)

    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

#nameString

Returns The current browser name.

Returns:

  • (String)

    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

#platformString

Returns The current browser platform.

Returns:

  • (String)

    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

#versionString

Returns The current browser version.

Returns:

  • (String)

    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_sString

Returns the current browser as a string.

Returns:

  • (String)

    A string representation of the current browser.

See Also:



793
794
795
# File 'lib/brauser/browser.rb', line 793

def to_s
  self.classes
end