Module: FerrisBueller::WebHelpers

Included in:
Web
Defined in:
lib/ferris-bueller/web_helpers.rb

Instance Method Summary collapse

Instance Method Details

#apiObject



8
# File 'lib/ferris-bueller/web_helpers.rb', line 8

def api ; settings.api end

#compare(name1, name2) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ferris-bueller/web_helpers.rb', line 59

def compare name1, name2
  return nil if name1.nil? || name1.empty?
  return nil if name2.nil? || name2.empty?
  @@jaro ||= FuzzyStringMatch::JaroWinkler.create :native
  n1 = name1.gsub(/\W/, '').downcase
  n2 = name2.gsub(/\W/, '').downcase
  @@jaro.getDistance n1, n2
end

#logObject



6
# File 'lib/ferris-bueller/web_helpers.rb', line 6

def log ; settings.logger end

#storeObject



4
# File 'lib/ferris-bueller/web_helpers.rb', line 4

def store ; settings.store end

#user_lookup(params, threshold = 0.75) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ferris-bueller/web_helpers.rb', line 11

def user_lookup params, threshold=0.75
  data = api.send 'users.info', user: params['user_id']

  slack_user = {
    key: data['user']['id'],
    name: (data['user']['real_name'] || data['user']['name']),
    nick: data['user']['name'],
    email: data['user']['email']
  }

  jira_matches = store['jira_users'].values.map do |jira_user|
    distances = [ :name, :nick ].map do |k|
      compare slack_user[k], jira_user[k]
    end.compact
    mean_distance = 1.0 * distances.inject(:+) / distances.size
    if mean_distance > threshold
      { user: jira_user, distance: mean_distance}
    end
  end.compact

  jira_match = jira_matches.sort_by { |m| m[:distance] }.last

  unless jira_match
    log.warn \
      event: 'unmatched user',
      slack_user: slack_user
    return nil
  end

  log.info \
    event: 'matched user',
    slack_user: slack_user,
    jira_match: jira_match

  return { slack: slack_user, jira: jira_match[:user] }

rescue StandardError => e
  log.error \
    error: 'could not lookup users',
    event: 'exception',
    exception: e.inspect,
    class: e.class,
    message: e.message,
    backtrace: e.backtrace
  return nil
end