class Mobi::Mtld::Da::Device::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

KEY_DEFAULT_REGEX_SET
KEY_REFINE_REGEX_ID
KEY_REGEXES
KEY_REGEX_MATCH_POS
KEY_RULE_GROUPS
KEY_RULE_REGEX_ID
KEY_SEARCH_REGEX_ID
KEY_SKIP_IDS
KEY_UA_RULES

Constants to make keys more readable

Public Class Methods

new(tree_provider) click to toggle source
# File ../../device_api/trunk/ruby/src/lib/ua_props.rb, line 39
def initialize tree_provider
        super(tree_provider, KEY_UA_RULES)
        # process the regexes - we need to override the default ones with any API
        # specific regexes
        init_process_regexes
end

Public Instance Methods

put_properties(user_agent, props_to_vals, sought = nil) click to toggle source
# File ../../device_api/trunk/ruby/src/lib/ua_props.rb, line 46
def put_properties user_agent, props_to_vals, sought = nil
  # first check list of items that skip rules - these are typically non-mobile
  # boolean properties such as isBrowser, isBot etc

  if skip_ua_rules props_to_vals
    return
  end

  regexes = @branch[KEY_REGEXES]

  # 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
  rule_groups = @branch[KEY_RULE_GROUPS]
  rules_to_run = ua_property_rules(user_agent, props_to_vals, rule_groups,
    regexes)

  # STEP 2: try and extract properties using the rules
  if !rules_to_run.nil?
    extract_properties rules_to_run, user_agent, regexes, sought
  end

end

Protected Instance Methods

init_get_matcher_propery_ids(group, prop_ids) click to toggle source

Find all the properties that are used for matching.

group

The rule group that can contain a property matcher

prop_ids

The list of found property IDs

# File ../../device_api/trunk/ruby/src/lib/ua_props.rb, line 78
  def init_get_matcher_propery_ids group, prop_ids
          # the properties matcher may not exist....
          if group[KEY_PROPERTY_MATCHER]
                  group[KEY_PROPERTY_MATCHER].each do |prop_id, prop_value|
                          prop_ids.push(prop_id) unless prop_ids.include?(prop_id)
end
          end
  end
init_rule_sets(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 ../../device_api/trunk/ruby/src/lib/ua_props.rb, line 92
def init_rule_sets group
        sets = group[KEY_RULE_SET]
        group[KEY_RULE_SET_COUNT] = sets.length
        return sets
end