UNPKG

3.13 kBtext/coffeescriptView Raw
1Configuration = require './configuration'
2_ = require('underscore')._
3
4Hipchat = Configuration.Hipchat
5Async = require 'async'
6Styled = require 'styled'
7
8status_icon = (status) ->
9 if (status?)
10 if status then "✓" else "✘"
11 else
12 "●"
13
14status_color = (status) ->
15 if (status?)
16 if status then "green" else "red"
17 else
18 "yellow"
19
20format_term = (title, url, infos, comments, status, avatar, tails) ->
21 icon = status_icon(status)
22 color = status_color(status)
23
24 text = ""
25 text += "#{Styled(color, icon)} " if icon? and color?
26 text += "#{title}"
27 text += " - #{Styled('bold', infos)}" if infos?
28 text += " - #{Styled('italic', comments)}" if comments?
29 if (tails?)
30 for tail in tails
31 do (tail) ->
32 text += "\n\t ↳ #{tail}"
33
34 text
35
36# TODO: Use templating
37format_html = (title, url, infos, comments, status, avatar, tails) ->
38 html = ""
39 html += "#{status_icon(status)} "
40 if (avatar?) and Configuration.Github.Gravatar
41 html += "<img src='http://www.gravatar.com/avatar/#{avatar}?s=20' /> - "
42 html += "<a href='#{url}'>" if url?
43 html += "#{title}"
44 html += "</a>" if url?
45 html += " - <strong>#{infos}</strong>" if infos?
46 html += " - <i>#{comments}</i>" if comments?
47 if (tails?)
48 for tail in tails
49 do (tail) ->
50 html += "<br />&nbsp; ↳ #{tail}"
51
52 html
53
54print = (o, callback) ->
55 console.log format_term(
56 o['title'],
57 o['url'],
58 o['infos'],
59 o['comments'],
60 o['status']
61 o['avatar']
62 o['tails']
63 )
64 if (callback? and _.isFunction(callback)) then callback(null)
65
66render = (o, callback) ->
67 Hipchat.Rooms.message Hipchat.Channel,
68 Configuration.Nickname,
69 format_html(
70 o['title'],
71 o['url'],
72 o['infos'],
73 o['comments'],
74 o['status'],
75 o['avatar']
76 o['tails']
77 ), {
78 message_format: "html",
79 color: status_color(o['status'])
80 }, (error) ->
81 if (callback? and _.isFunction(callback)) then callback(error)
82
83fallback_print = (fallback) ->
84 if fallback? and _.isFunction(fallback) then fallback else print
85
86fallback_printList = (fallback, list, filter) ->
87 if (_.isEmpty(list))
88 fallback_print(fallback) { title: "No result for your request" }
89
90 if (_.every(list, (o) -> _.has(o, 'order')))
91 list = _.sortBy(list, 'order').reverse()
92
93 if (filter?)
94 list = filter list
95
96 Async.eachSeries list, (item, callback) ->
97 fallback_print(fallback) {
98 title: item.title,
99 url: item.url,
100 infos: item.infos,
101 comments: item.comments,
102 status: item.status,
103 avatar: item.avatar,
104 tails: item.tails
105 }, callback
106 , (error) ->
107 if (error?)
108 print { title: "An error occured while sending a message: #{JSON.stringify(error)}", status: false }
109
110fallback_printError = (fallback, error) ->
111 fallback_print(fallback) { title: "An error occured: #{JSON.stringify(error)}", status: false }
112
113module.exports = {
114 format_term: format_term,
115 format_html: format_html,
116 print: print,
117 render: render,
118 fallback_print: fallback_print,
119 fallback_printList: fallback_printList,
120 fallback_printError: fallback_printError
121}