Sha256: 5147920440c25f6071f544593ac1fb37c828d698e9ab96651fe2c9aeaae90f5f

Contents?: true

Size: 1.22 KB

Versions: 19

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'yaml'

# 🐵 patches eager-loading and cacheing of the default user_agent patterns
# PR to add the functionality for reals: https://github.com/ua-parser/uap-ruby/pull/56
module UserAgentParser
  class Parser
    class Patterns
      def initialize
        @cache = {}
        get # warm the cache for the default patterns
      end

      def get(path = nil)
        path = UserAgentParser::DefaultPatternsPath if path.nil?
        @cache[path] ||= load_and_parse(path)
      end

      private

      def load_and_parse(path)
        yml = YAML.load_file(path)

        # Parse all the regexs
        yml.each_pair do |_type, patterns|
          patterns.each do |pattern|
            pattern[:regex] = Regexp.new(pattern['regex'], pattern['regex_flag'] == 'i')
          end
        end

        [yml['user_agent_parsers'], yml['os_parsers'], yml['device_parsers']]
      end
    end

    # rubocop:disable Style/ClassVars
    @@patterns = Patterns.new

    def self.load_patterns(path)
      @@patterns.get(path)
    end

    def initialize(options = {})
      @patterns_path = options[:patterns_path]
      @ua_patterns, @os_patterns, @device_patterns = Parser.load_patterns(@patterns_path)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sewing_kit-0.130.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.129.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.128.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.127.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.126.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.125.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.124.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.123.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.122.1 lib/hacks/user_agent_parser.rb
sewing_kit-0.122.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.114.2 lib/hacks/user_agent_parser.rb
sewing_kit-0.114.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.97.2 lib/hacks/user_agent_parser.rb
sewing_kit-0.97.1 lib/hacks/user_agent_parser.rb
sewing_kit-0.97.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.96.0 lib/hacks/user_agent_parser.rb
sewing_kit-0.95.4 lib/hacks/user_agent_parser.rb
sewing_kit-0.95.3 lib/hacks/user_agent_parser.rb
sewing_kit-0.95.2 lib/hacks/user_agent_parser.rb