Module: TopSecret::Mapping

Included in:
Text::BatchResult, Text::Result, Text::ScanResult
Defined in:
lib/top_secret/mapping.rb

Overview

Provides dynamic category methods for querying sensitive information by type.

This module automatically generates methods for accessing sensitive information organized by category (emails, credit cards, people, etc.). Methods are available for all default filter types and any custom labels used in the mapping.

Examples:

Querying emails

result = TopSecret::Text.filter("Contact ralph@example.com")
result.emails?         # => true
result.emails          # => ["ralph@example.com"]
result.email_mapping   # => {:EMAIL_1=>"ralph@example.com"}

With no matches

result = TopSecret::Text.filter("No sensitive data")
result.emails?         # => false
result.emails          # => []
result.email_mapping   # => {}

Custom labels

result = TopSecret::Text.filter(
  "user[at]example.com",
  email_filter: TopSecret::Filters::Regex.new(
    label: "EMAIL_ADDRESS",
    regex: /\w+\[at\]\w+\.\w+/
  )
)
result.email_addresses          # => ["user[at]example.com"]
result.email_address_mapping    # => {:EMAIL_ADDRESS_1=>"user[at]example.com"}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/top_secret/mapping.rb', line 43

def method_missing(method_name, *args, &block)
  category = category_for(method_name)

  if category
    result = category.resolve(method_name, mapping)
    define_singleton_method(method_name) { result }
    result
  else
    super
  end
end

Instance Method Details

#categoriesArray<Symbol>

Returns categories that have matches in the mapping.

Returns:

  • (Array<Symbol>)

    List of categories with matches



62
63
64
# File 'lib/top_secret/mapping.rb', line 62

def categories
  @categories ||= category_objects.select { |c| c.matches?(mapping) }.map { |c| c.type.to_sym }
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/top_secret/mapping.rb', line 55

def respond_to_missing?(method_name, include_private = false)
  category_for(method_name) || super
end

#safe?Boolean

Returns Whether sensitive information was not found.

Returns:

  • (Boolean)

    Whether sensitive information was not found



39
40
41
# File 'lib/top_secret/mapping.rb', line 39

def safe?
  !sensitive?
end

#sensitive?Boolean

Returns Whether sensitive information was found.

Returns:

  • (Boolean)

    Whether sensitive information was found



34
35
36
# File 'lib/top_secret/mapping.rb', line 34

def sensitive?
  mapping.any?
end