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.



677
678
679
680
681
682
683
684
685
686
687
# File 'lib/brauser/browser.rb', line 677

def initialize(agent = "", accept_language = "")
  ::Brauser::Browser.add_default_browsers
  ::Brauser::Browser.add_default_platforms
  ::Brauser::Browser.add_default_languages

  @agent = agent
  @accept_language = accept_language

  @languages = parse_accept_language(@accept_language) if @accept_language
  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_v_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 _ 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.



706
707
708
709
710
711
712
713
714
# File 'lib/brauser/browser.rb', line 706

def method_missing(query, *arguments, &block)
  begin
    query_s = query.ensure_string
    rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
    query_s =~ /\?$/ ? 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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/brauser/browser.rb', line 654

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.add_default_browsers
    ::Brauser::Browser.add_default_platforms
    ::Brauser::Browser.add_default_languages

    @agent = agent
    @accept_language = accept_language

    @languages = parse_accept_language(@accept_language) if @accept_language
    parse_agent(@agent) if @agent
  end

  # This method enables the use of dynamic queries in just one method.
  #
  # For example:
  #
  # ```ruby
  # browser.is_msie_v_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 `_` 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
      query_s = query.ensure_string
      rv = execute_query(parse_query(query_s)) || Brauser::Query.new(self, false)
      query_s =~ /\?$/ ? 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(/\?$/, "").gsub(/(_(v|on|is))/, " \\2").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:



721
722
723
# File 'lib/brauser/browser.rb', line 721

def to_s
  self.classes
end