Module: Helpers

Included in:
Bender::Main, BenderBot
Defined in:
lib/bender/helpers.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  hipchat_token: nil,
  hipchat_v2_token: nil,
  primary_room_id: nil,
  jid: nil,
  password: nil,
  nick: nil,
  mention: nil,
  rooms: nil,
  database: nil,
  jira_user: nil,
  jira_pass: nil,
  jira_site: nil,
  jira_project: nil,
  jira_group: nil,
  jira_type: nil,
  user_refresh: 300,
  issue_refresh: 5,
  group_refresh: 60,
  room_base_name: nil,
  order: false
}
QUOTES =
[
  'Bite my shiny metal ass!',
  'This is the worst kind of discrimination there is: the kind against me!',
  'I guess if you want children beaten, you have to do it yourself.',
  "Hahahahaha! Oh wait you're serious. Let me laugh even harder.",
  "You know what cheers me up? Other people's misfortune.",
  'Anything less than immortality is a complete waste of time.',
  "Blackmail is such an ugly word. I prefer extortion. The 'x' makes it sound cool.",
  'Have you tried turning off the TV, sitting down with your children, and hitting them?',
  "You're a pimple on society’s ass and you'll never amount to anything!",
  'Shut up baby, I know it!',
  "I'm so embarrassed. I wish everyone else was dead!",
  "Afterlife? If I thought I had to live another life, I'd kill myself right now!",
  "I'm back baby!",
  "LET'S GO ALREADYYYYYY!",
  "I don't have emotions and sometimes that makes me very sad",
  "Life is hilariously cruel"
]

Instance Method Summary collapse

Instance Method Details

#friendly_date(val) ⇒ Object



94
95
96
# File 'lib/bender/helpers.rb', line 94

def friendly_date val
  Time.parse(val).strftime('%Y-%m-%d %H:%M %Z')
end


125
126
127
128
129
130
# File 'lib/bender/helpers.rb', line 125

def incident_link incident
  '<a href="%s">%s</a>' % [
    incident_url(incident),
    incident['key']
  ]
end

#incident_url(incident) ⇒ Object



121
122
123
# File 'lib/bender/helpers.rb', line 121

def incident_url incident
  config[:jira_site] + '/browse/' + incident['key']
end

#normalize_date(val) ⇒ Object



89
90
91
# File 'lib/bender/helpers.rb', line 89

def normalize_date val
  Time.parse(val).utc.iso8601(0).sub(/Z$/, 'UTC')
end

#normalize_value(val) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bender/helpers.rb', line 75

def normalize_value val
  case val
  when Hash
    val['name'] || val['value'] || val
  when Array
    val.map { |v| v['value'] }.join(', ')
  when /^\d{4}\-\d{2}\-\d{2}/
    '%s (%s)' % [ val, normalize_date(val) ]
  else
    val
  end
end

#one_dayObject



105
106
107
# File 'lib/bender/helpers.rb', line 105

def one_day
  24 * 60 * 60 # seconds/day
end

#recent_incident?(i) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/bender/helpers.rb', line 99

def recent_incident? i
  it = Time.parse(i['fields']['created'])
  Time.now - it < one_day
end

#refresh_incidents(bot = self) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bender/helpers.rb', line 45

def refresh_incidents bot=self
  req_path = '/rest/api/2/search'
  req_params = QueryParams.encode \
    jql: "project = #{config[:jira_project]} ORDER BY created ASC, priority DESC",
    fields: SHOW_FIELDS.keys.join(','),
    startAt: 0,
    maxResults: 1_000_000

  uri = URI(config[:jira_site] + req_path + '?' + req_params)
  http = Net::HTTP.new uri.hostname, uri.port

  req = Net::HTTP::Get.new uri
  req.basic_auth config[:jira_user], config[:jira_pass]
  req['Content-Type'] = 'application/json'
  req['Accept'] = 'application/json'

  resp = http.request req
  begin
    issues = JSON.parse(resp.body)['issues']
  rescue JSON::ParserError
    log.warn 'Could not refresh incidents!'
    return
  end

  bot.store['incidents'] = issues.map! do |i|
    i['num'] = i['key'].split('-', 2).last ; i
  end
end

#select_incident(num, refresh = true) ⇒ Object



110
111
112
113
# File 'lib/bender/helpers.rb', line 110

def select_incident num, refresh=true
  refresh_incidents if refresh
  store['incidents'].select { |i| i['num'] == num }.first
end

#short_severity(s) ⇒ Object



116
117
118
# File 'lib/bender/helpers.rb', line 116

def short_severity s
  s.split(' - ', 2).first
end