Class: Polyn::Naming
- Inherits:
-
Object
- Object
- Polyn::Naming
- Defined in:
- lib/polyn/naming.rb
Overview
Methods for formatting and validating names of fields
Class Method Summary collapse
- .build_subject_pattern_part(token) ⇒ Object
-
.consumer_name(type, source = nil) ⇒ Object
Create a consumer name from a source and type.
- .domain ⇒ Object
-
.dot_to_colon(str) ⇒ Object
Convert a dot separated name into a colon separated name.
-
.subject_matches?(subject, pattern) ⇒ Boolean
Determine if a given subject matches a subscription pattern.
-
.trim_domain_prefix(str) ⇒ Object
Remove the ‘domain` name from the beginning of a string.
-
.validate_domain_name!(name) ⇒ Object
Validate that the configured ‘domain` is in the correct format.
-
.validate_message_name!(name) ⇒ Object
Validate the event type.
-
.validate_source_name(name) ⇒ Object
Validate the ‘source` name.
-
.validate_source_name!(name) ⇒ Object
Validate the ‘source` name and raise if invalid.
-
.validate_source_root!(name) ⇒ Object
Validate that the configured ‘source_root` is in the correct format.
Class Method Details
.build_subject_pattern_part(token) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/polyn/naming.rb', line 104 def self.build_subject_pattern_part(token) single_wildcard = "*" multiple_wildcard = ">" return "(\\w+)" if token == single_wildcard return "((\\w+\\.)*\\w)" if token == multiple_wildcard token end |
.consumer_name(type, source = nil) ⇒ Object
Create a consumer name from a source and type
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/polyn/naming.rb', line 75 def self.consumer_name(type, source = nil) (type) type = trim_domain_prefix(type) type = type.gsub(".", "_") root = Polyn.configuration.source_root root = root.gsub(".", "_") root = root.gsub(":", "_") if source validate_source_name!(source) source = source.gsub(".", "_") source = source.gsub(":", "_") [root, source, type].join("_") else [root, type].join("_") end end |
.domain ⇒ Object
115 116 117 |
# File 'lib/polyn/naming.rb', line 115 def self.domain Polyn.configuration.domain end |
.dot_to_colon(str) ⇒ Object
Convert a dot separated name into a colon separated name
9 10 11 |
# File 'lib/polyn/naming.rb', line 9 def self.dot_to_colon(str) str.gsub(".", ":") end |
.subject_matches?(subject, pattern) ⇒ Boolean
Determine if a given subject matches a subscription pattern
96 97 98 99 100 101 102 |
# File 'lib/polyn/naming.rb', line 96 def self.subject_matches?(subject, pattern) separator = "." pattern_tokens = pattern.split(separator).map { |token| build_subject_pattern_part(token) } pattern_tokens = pattern_tokens.join("\\#{separator}") subject.match?(Regexp.new(pattern_tokens)) end |
.trim_domain_prefix(str) ⇒ Object
Remove the ‘domain` name from the beginning of a string
68 69 70 71 |
# File 'lib/polyn/naming.rb', line 68 def self.trim_domain_prefix(str) str = str.sub("#{domain}.", "") str.sub("#{dot_to_colon(domain)}:", "") end |
.validate_domain_name!(name) ⇒ Object
Validate that the configured ‘domain` is in the correct format
15 16 17 18 19 20 21 22 |
# File 'lib/polyn/naming.rb', line 15 def self.validate_domain_name!(name) if name.is_a?(String) && name.match?(/\A[a-z0-9]+(?:(?:\.|:)[a-z0-9]+)*\z/) name else raise Polyn::Errors::ConfigurationError, "You must configure the `domain` for Polyn. It must be lowercase, alphanumeric and dot/colon separated, got #{name}" end end |
.validate_message_name!(name) ⇒ Object
Validate the event type
57 58 59 60 61 62 63 64 |
# File 'lib/polyn/naming.rb', line 57 def self.(name) if name.is_a?(String) && name.match?(/\A[a-z0-9]+(?:\.[a-z0-9]+)*\z/) name else raise Polyn::Errors::ValidationError, "Event types must be lowercase, alphanumeric and dot separated" end end |
.validate_source_name(name) ⇒ Object
Validate the ‘source` name
26 27 28 29 30 31 32 |
# File 'lib/polyn/naming.rb', line 26 def self.validate_source_name(name) if name.is_a?(String) && name.match?(/\A[a-z0-9]+(?:(?:\.|:)[a-z0-9]+)*\z/) true else "Event source must be lowercase, alphanumeric and dot/colon separated, got #{name}" end end |
.validate_source_name!(name) ⇒ Object
Validate the ‘source` name and raise if invalid
36 37 38 39 40 41 |
# File 'lib/polyn/naming.rb', line 36 def self.validate_source_name!(name) = validate_source_name(name) raise Polyn::Errors::ValidationError, unless == true name end |
.validate_source_root!(name) ⇒ Object
Validate that the configured ‘source_root` is in the correct format
45 46 47 48 49 50 51 52 53 |
# File 'lib/polyn/naming.rb', line 45 def self.validate_source_root!(name) = validate_source_name(name) if == true name else raise Polyn::Errors::ConfigurationError, "You must configure the `source_root` for Polyn. #{}" end end |