Class: BitterDomain::BitShifter
- Inherits:
-
Object
- Object
- BitterDomain::BitShifter
- Defined in:
- lib/bitter_domain/bit_shifter.rb
Constant Summary collapse
- PATTERN =
accept alphanumeric chars and hyphens but no hyphens at the beginning or end of the domain
/^[A-Za-z0-9]-?[A-Za-z0-9]+$/
Instance Method Summary collapse
- #gen_shifts(byte) ⇒ Object
- #get_shifted_domains ⇒ Object
-
#initialize(domain) ⇒ BitShifter
constructor
A new instance of BitShifter.
- #valid_domain?(domain) ⇒ Boolean
Constructor Details
#initialize(domain) ⇒ BitShifter
Returns a new instance of BitShifter.
7 8 9 10 |
# File 'lib/bitter_domain/bit_shifter.rb', line 7 def initialize(domain) @domain = domain @shifts = [] end |
Instance Method Details
#gen_shifts(byte) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bitter_domain/bit_shifter.rb', line 16 def gen_shifts(byte) # left-shift 1 through each bit of the byte # and XOR to flip a single one at each position 8.times do |i| shifted = byte ^ (1 << i) @shifts.push(shifted) end @shifts end |
#get_shifted_domains ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bitter_domain/bit_shifter.rb', line 27 def get_shifted_domains bytes = @domain.bytes domains = [] # iterate over a byte array for the original domain # get a set of flips for each byte, swap each new byte # and then stringify bytes.each.with_index do |byte, idx| shifts = gen_shifts(byte) swapped_domains = shifts.map do |byte| copied_bytes = bytes.dup copied_bytes[idx] = byte copied_bytes .map(&:chr) .join("") .downcase end domains.concat(swapped_domains) end # kick out anything that doesn't match the pattern above domains.keep_if { |domain| valid_domain?(domain) }.uniq end |
#valid_domain?(domain) ⇒ Boolean
12 13 14 |
# File 'lib/bitter_domain/bit_shifter.rb', line 12 def valid_domain?(domain) domain.match?(PATTERN) && domain != @domain end |