Class: Brauser::Definitions::Browser

Inherits:
Base
  • Object
show all
Defined in:
lib/brauser/definitions/browser.rb

Overview

A definition of a platform.

Instance Attribute Summary collapse

Attributes inherited from Base

#id

Instance Method Summary collapse

Constructor Details

#initialize(id, name, engine_matcher, version_matcher, **_) ⇒ Browser

Creates a new definition.

Parameters:

  • id (Symbol)

    The platform id.

  • name (String)

    The platform name.

  • engine_matcher (Regexp|Proc)

    The pattern or the block to recognize the engine.

  • version_matcher (Regexp|Proc)

    The pattern or the block to recognize the version.



25
26
27
28
29
30
# File 'lib/brauser/definitions/browser.rb', line 25

def initialize(id, name, engine_matcher, version_matcher, **_)
  @id = id
  @name = name
  @engine_matcher = engine_matcher
  @version_matcher = version_matcher
end

Instance Attribute Details

#engine_matcherRegexp|Proc (readonly)

Returns The pattern or the block to recognize the engine.

Returns:

  • (Regexp|Proc)

    The pattern or the block to recognize the engine.



16
17
18
19
20
21
22
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
# File 'lib/brauser/definitions/browser.rb', line 16

class Browser < Base
  attr_reader :name, :engine_matcher, :version_matcher

  # Creates a new definition.
  #
  # @param id [Symbol] The platform id.
  # @param name [String] The platform name.
  # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine.
  # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version.
  def initialize(id, name, engine_matcher, version_matcher, **_)
    @id = id
    @name = name
    @engine_matcher = engine_matcher
    @version_matcher = version_matcher
  end

  # Matches against an header.
  #
  # @param header [String] The header to match
  # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise.
  def match(header)
    # First of all, match the engine
    engine = perform_match(@engine_matcher, header) ? @id : nil

    if engine
      version = extract_version(perform_match(@version_matcher, header))
      platform = extract_platform(header, engine)
      [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)]
    end
  end

  private

  # :nodoc:
  def perform_match(pattern, subject)
    method = pattern.is_a?(Regexp) ? :match : :call
    pattern.send(method, subject)
  end

  # :nodoc:
  # @private
  def extract_version(version)
    # Adjust version
    version = "0.0" if version.blank?
    version = version.to_a.last if version.is_a?(::MatchData)
    Versionomy.parse(version)
  rescue
    version
  end

  # :nodoc:
  def extract_platform(header, engine)
    catch(:result) do
      Brauser::Definitions.platforms.each do |platform, definition|
        throw(:result, platform) if definition.match(header, engine)
      end

      nil
    end
  end
end

#nameString (readonly)

Returns The platform name.

Returns:

  • (String)

    The platform name.



16
17
18
19
20
21
22
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
# File 'lib/brauser/definitions/browser.rb', line 16

class Browser < Base
  attr_reader :name, :engine_matcher, :version_matcher

  # Creates a new definition.
  #
  # @param id [Symbol] The platform id.
  # @param name [String] The platform name.
  # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine.
  # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version.
  def initialize(id, name, engine_matcher, version_matcher, **_)
    @id = id
    @name = name
    @engine_matcher = engine_matcher
    @version_matcher = version_matcher
  end

  # Matches against an header.
  #
  # @param header [String] The header to match
  # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise.
  def match(header)
    # First of all, match the engine
    engine = perform_match(@engine_matcher, header) ? @id : nil

    if engine
      version = extract_version(perform_match(@version_matcher, header))
      platform = extract_platform(header, engine)
      [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)]
    end
  end

  private

  # :nodoc:
  def perform_match(pattern, subject)
    method = pattern.is_a?(Regexp) ? :match : :call
    pattern.send(method, subject)
  end

  # :nodoc:
  # @private
  def extract_version(version)
    # Adjust version
    version = "0.0" if version.blank?
    version = version.to_a.last if version.is_a?(::MatchData)
    Versionomy.parse(version)
  rescue
    version
  end

  # :nodoc:
  def extract_platform(header, engine)
    catch(:result) do
      Brauser::Definitions.platforms.each do |platform, definition|
        throw(:result, platform) if definition.match(header, engine)
      end

      nil
    end
  end
end

#version_matcherRegexp|Proc (readonly)

Returns The pattern or the block to recognize the version.

Returns:

  • (Regexp|Proc)

    The pattern or the block to recognize the version.



16
17
18
19
20
21
22
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
# File 'lib/brauser/definitions/browser.rb', line 16

class Browser < Base
  attr_reader :name, :engine_matcher, :version_matcher

  # Creates a new definition.
  #
  # @param id [Symbol] The platform id.
  # @param name [String] The platform name.
  # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine.
  # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version.
  def initialize(id, name, engine_matcher, version_matcher, **_)
    @id = id
    @name = name
    @engine_matcher = engine_matcher
    @version_matcher = version_matcher
  end

  # Matches against an header.
  #
  # @param header [String] The header to match
  # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise.
  def match(header)
    # First of all, match the engine
    engine = perform_match(@engine_matcher, header) ? @id : nil

    if engine
      version = extract_version(perform_match(@version_matcher, header))
      platform = extract_platform(header, engine)
      [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)]
    end
  end

  private

  # :nodoc:
  def perform_match(pattern, subject)
    method = pattern.is_a?(Regexp) ? :match : :call
    pattern.send(method, subject)
  end

  # :nodoc:
  # @private
  def extract_version(version)
    # Adjust version
    version = "0.0" if version.blank?
    version = version.to_a.last if version.is_a?(::MatchData)
    Versionomy.parse(version)
  rescue
    version
  end

  # :nodoc:
  def extract_platform(header, engine)
    catch(:result) do
      Brauser::Definitions.platforms.each do |platform, definition|
        throw(:result, platform) if definition.match(header, engine)
      end

      nil
    end
  end
end

Instance Method Details

#match(header) ⇒ Array|NilClass

Matches against an header.

Parameters:

  • header (String)

    The header to match

Returns:

  • (Array|NilClass)

    An array with the engine and the version if match succeeded, false or nil otherwise.



36
37
38
39
40
41
42
43
44
45
# File 'lib/brauser/definitions/browser.rb', line 36

def match(header)
  # First of all, match the engine
  engine = perform_match(@engine_matcher, header) ? @id : nil

  if engine
    version = extract_version(perform_match(@version_matcher, header))
    platform = extract_platform(header, engine)
    [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)]
  end
end