Skip to content

Commit

Permalink
Move template rendering to Notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bittersweet committed Oct 11, 2015
1 parent 09fea09 commit 53d7062
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
27 changes: 24 additions & 3 deletions notifier.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"bytes"
"fmt"
"log"
"text/template"

"github.com/jmoiron/sqlx/types"
)
Expand Down Expand Up @@ -39,19 +42,37 @@ func (n *Notifier) checkRules(s *Stat) bool {
return true
}

func (n *Notifier) renderTemplate(s *Stat) []byte {
var err error
var doc bytes.Buffer

t := template.New("notificationTemplate")
t, err = t.Parse(n.Template)
if err != nil {
log.Fatal("t.Parse of n.Template", err)
}

err = t.Execute(&doc, s.toMap())
if err != nil {
log.Fatal("t.Execute ", err)
}

return doc.Bytes()
}

func (n *Notifier) notify(s *Stat) {
nt := n.NotificationType
fmt.Printf("Notifying notifier id: %d type: %s\n", n.Id, nt)
fmt.Printf("Incoming data: %v\n", s.toMap())

if !n.checkRules(s) {
// early return
}

message := n.renderTemplate(s)
switch nt {
case "email":
sendEmailNotification(s, n)
sendEmail(s.Key, message)
case "slack":
sendSlackNotification(s, n)
sendSlack(s.Key, message)
}
}
47 changes: 47 additions & 0 deletions notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,50 @@ func TestNotifierCheckRulesSettingIsBlank(t *testing.T) {

assert.Equal(t, n.checkRules(&s), true)
}

func TestNotifierNotify(t *testing.T) {
// fake sendSlackNotification somehow?
}

func TestNotifierNotifyReturnsEarlyIfRulesAreNotMet(t *testing.T) {
}

func TestNotifierRenderTemplate(t *testing.T) {
n := Notifier{
Id: 1,
NotificationType: "email",
Class: "User",
Template: "name: {{.name}}",
}

var jt = types.JsonText(`{"active": true, "name": "Go", "number": "12"}`)
s := Stat{"Mark", jt}

result := n.renderTemplate(&s)
expected := "name: Go"
assert.Equal(t, result, expected)
}

func TestNotifierRenderTemplateWithLogic(t *testing.T) {
template := `{{ if .active }}Active!{{ else }}inactive{{ end }}`
n := Notifier{
Id: 1,
NotificationType: "email",
Class: "User",
Template: template,
}

var jt = types.JsonText(`{"active": true, "name": "Go", "number": "12"}`)
s := Stat{"Mark", jt}

result := n.renderTemplate(&s)
expected := "Active!"
assert.Equal(t, result, expected)

jt = types.JsonText(`{"active": false, "name": "Go", "number": "12"}`)
s = Stat{"Mark", jt}

result = n.renderTemplate(&s)
expected = "inactive"
assert.Equal(t, result, expected)
}
20 changes: 0 additions & 20 deletions slack.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"text/template"
)

var transport http.RoundTripper
Expand All @@ -18,24 +16,6 @@ type slackResponse struct {
Ts string `json:"ts"`
}

func sendSlackNotification(s *Stat, notifier *Notifier) {
var err error
var doc bytes.Buffer

t := template.New("notificationTemplate")
t, err = t.Parse(notifier.Template)
if err != nil {
log.Fatal("t.Parse of n.Template", err)
}

err = t.Execute(&doc, s.toMap())
if err != nil {
log.Fatal("t.Execute ", err)
}

sendSlack(s.Key, doc.Bytes())
}

func getTransport() http.RoundTripper {
// If we have overridden this variable in testing
if transport != nil {
Expand Down
1 change: 1 addition & 0 deletions udp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *Stat) notify() {
if err != nil {
log.Fatal("db.Select ", err)
}
fmt.Printf("Incoming data: %v\n", s.toMap())
fmt.Printf("Found %d notifiers\n", len(notifiers))

for i := 0; i < len(notifiers); i++ {
Expand Down

0 comments on commit 53d7062

Please sign in to comment.