Module: StubRequests::Utils::Fuzzy

Defined in:
lib/stub_requests/utils/fuzzy.rb

Overview

Provides convenience methods for hashes

Author:

Since:

  • 0.1.0

Constant Summary collapse

FILTER_REGEX =

Returns a pattern excluding all except alphanumeric

Returns:

  • (Regexp)

    a pattern excluding all except alphanumeric

Since:

  • 0.1.0

/(^\w\d)/.freeze

Class Method Summary collapse

Class Method Details

.compute_distances(original, others) ⇒ Object

:nodoc:

Since:

  • 0.1.0



49
50
51
52
53
# File 'lib/stub_requests/utils/fuzzy.rb', line 49

def self.compute_distances(original, others)
  others.each_with_object([]) do |other, memo|
    memo << [jaro_distance(original, other), other]
  end
end

.filter_matches(matches) ⇒ Object

:nodoc:

Since:

  • 0.1.0



40
41
42
43
44
45
46
# File 'lib/stub_requests/utils/fuzzy.rb', line 40

def self.filter_matches(matches)
  suggestions = matches.values
  return suggestions if suggestions.size <= 3

  matches.select { |distance, _| distance >= 0.7 }
         .values
end

.jaro_distance(original, other) ⇒ Object

:nodoc:

Since:

  • 0.1.0



56
57
58
59
60
61
62
# File 'lib/stub_requests/utils/fuzzy.rb', line 56

def self.jaro_distance(original, other)
  JaroWinkler.jaro_distance(
    normalize_string(original),
    normalize_string(other),
    StubRequests.config.jaro_options,
  )
end

.match(original, others) ⇒ Array

Find strings that are similar

Parameters:

  • original (String)

    a string to match

  • others (Array<String>)

    an array of string to search

Returns:

Since:

  • 0.1.0



34
35
36
37
# File 'lib/stub_requests/utils/fuzzy.rb', line 34

def self.match(original, others)
  matches = compute_distances(original, others).sort.reverse
  filter_matches(matches.to_h)
end

.normalize_string(value) ⇒ Object

:nodoc:

Since:

  • 0.1.0



65
66
67
# File 'lib/stub_requests/utils/fuzzy.rb', line 65

def self.normalize_string(value)
  value.to_s.gsub(FILTER_REGEX, "")
end