Class: Brauser::Browser

Overview

This class represents a detection of the current user browser.

Constant Summary

Constants included from Brauser::Browseable::DefaultDefinitions

Brauser::Browseable::DefaultDefinitions::LANGUAGES, Brauser::Browseable::DefaultDefinitions::MAJOR_DESKTOP_BROWSERS, Brauser::Browseable::DefaultDefinitions::MINOR_DESKTOP_BROWSERS, Brauser::Browseable::DefaultDefinitions::MOBILE_BROWSERS, Brauser::Browseable::DefaultDefinitions::MSIE_BROWSERS, Brauser::Browseable::DefaultDefinitions::PLATFORMS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Brauser::Browseable::Querying

#accepts?, #is?, #on?, #supported?, #version?

Methods included from Brauser::Browseable::PartialQuerying

#accepts, #is, #on, #version_equals_to

Methods included from Brauser::Browseable::Parsing

#parse_accept_language, #parse_agent

Methods included from Brauser::Browseable::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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brauser/browser.rb', line 46

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.



85
86
87
88
89
90
91
# File 'lib/brauser/browser.rb', line 85

def method_missing(query, *arguments, &block)
  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

Instance Attribute Details

#accept_languageString

Returns The raw Accept-Language HTTP header.

Returns:

  • (String)

    The raw Accept-Language HTTP header.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end

#agentString

Returns The raw User-Agent HTTP header.

Returns:

  • (String)

    The raw User-Agent HTTP header.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end

#languagesArray

Returns The accepted languages.

Returns:

  • (Array)

    The accepted languages.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end

#nameString

Returns The current browser name.

Returns:

  • (String)

    The current browser name.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end

#platformString

Returns The current browser platform.

Returns:

  • (String)

    The current browser platform.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end

#version(versions = nil) ⇒ String|Query

Get the current browser version (if called without arguments) or checks if the browser is a specific version.

Parameters:

  • versions (String|Hash) (defaults to: nil)

    A string in the form operator version && ... (example: >= 7 && < 4) or an hash with specific version to match against, in form {:operator => version}, where operator is one of :lt, :lte, :eq, :gt, :gte.

Returns:

  • (String|Query)

    The browser version or a query which can evaluated for concatenation or result.

See Also:

  • #is_version


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brauser/browser.rb', line 23

class Browser
  attr_accessor :agent
  attr_accessor :accept_language
  attr_accessor :languages
  attr_accessor :name
  attr_accessor :version
  attr_accessor :platform

  # Aliases
  alias_attribute :ua, :agent

  include ::Brauser::Browseable::General
  include ::Brauser::Browseable::Attributes
  include ::Brauser::Browseable::DefaultDefinitions
  include ::Brauser::Browseable::Register
  include ::Brauser::Browseable::Parsing
  include ::Brauser::Browseable::PartialQuerying
  include ::Brauser::Browseable::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

  # Get the current browser version (if called without arguments) or checks if the browser is a specific version.
  #
  # @see #is_version
  # @param versions [String|Hash] A string in the form `operator version && ...` (example: `>= 7 && < 4`) or an hash with specific version to match against,
  #   in form `{:operator => version}`, where operator is one of `:lt, :lte, :eq, :gt, :gte`.
  # @return [String|Query] The browser version or a query which can evaluated for concatenation or result.
  def version(versions = nil)
    !versions ? @version : version_equals_to(versions)
  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)
    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

  private

  # 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.reduce(Brauser::Query.new(self, true)) { |rv, call|
      break unless rv.result
      rv.send(call[0], *call[1])
    }
  end
end