class UAProps

Copyright
Author

This class tries to extract properties from the User-Agent string itself. This is a completely separate step to the main JSON tree walk but uses the results of the tree walk to optimise the property extraction. The property extraction is done in two steps.

Step 1: Try and identify the type of User-Agent and thus the set of property extraction rules to run. This is optimised by the properties from the tree walk.

Step 2: Run the rules found in step 1 to try and extract the properties.

Constants

UA_RULES

Set a public static constant.

Public Class Methods

new(tree) click to toggle source
Calls superclass method PostWalkRules.new
# File ../Src/uaprops.rb, line 38
def initialize(tree)   

        # Set the private constants
        @SKIP_IDS = 'sk'
        @RULE_GROUPS = "rg"
        @REGEXES = "reg"
        @DEFAULT_REGEX_SET = "d"
        @REFINE_REGEX_ID = "f"
        @SEARCH_REGEX_ID = "s"
        @RULE_REGEX_ID = "r"
        @REGEX_MATCH_POS = "m"
        
        super(tree, UA_RULES)

        # process the regexes - we need to override the default ones with any API
        # specific regexes
        initProcessRegexes
end

Public Instance Methods

getProperties(userAgent, idProperties, sought, typedValues) click to toggle source

Get the User-Agent string properties using the User-Agent rules.

userAgent

The User-Agent to find properties for

idProperties

The results of the tree walk, map of property id to value id

sought

A set of properties to return values for.

typedValues

Whether to return typed values or string values

# File ../Src/uaprops.rb, line 118
def getProperties(userAgent, idProperties, sought, typedValues)
        propsToReturn = nil

        # first check list of items that skip rules - these are typically non-mobile
        # boolean properties such as isBrowser, isBot etc
        if skipUaRules(idProperties)
                return propsToReturn
        end

        regexes = @branch[@REGEXES] # we'll need this later...

        # now find the rules to run on the UA. This is a two step process.
        # Step 1 identifies the UA type and finds as list of rules to run.
        # Step 2 uses the list of rules to find properties in a UA

        # STEP 1: try and find the rules to run on the UA
        ruleGroups = @branch[@RULE_GROUPS]    
        rulesToRun = getUaPropertyRules(userAgent, idProperties, ruleGroups, regexes)
        
        # STEP 2: try and extract properties using the rules
        if rulesToRun.any?
                propsToReturn = extractProperties(rulesToRun, userAgent, regexes, sought, typedValues)
        end

        propsToReturn
end

Protected Instance Methods

initGetMatcherPropertyIds(group, propIds) click to toggle source

Find all the properties that are used for matching. This is needed in case the Api.getProperty() function is called as we need these properties for the User-Agent extraction rules to work correctly.

group

The rule group that can contain a property matcher

propIds

The list of found property IDs

# File ../Src/uaprops.rb, line 64
def initGetMatcherPropertyIds(group, propIds)
        # the properties matcher may not exist.... 
        if group[@PROPERTY_MATCHER]
                group[@PROPERTY_MATCHER].each { |propId, propValue|
                        propIds.push(propId) unless propIds.include?(propId)
                }
        end
end
initRuleSets(group) click to toggle source

Prepare the rule set by extracting it from the current group and counting the items in the group. This is done to avoid counting the items on every request.

group

The current parent group.

# File ../Src/uaprops.rb, line 79
def initRuleSets(group)
        sets = group[@RULE_SET]
        group[@RULE_SET_COUNT] = sets.length
        sets
end