Skip to content

Commit

Permalink
fix: support empty prefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpeterswa committed Sep 18, 2021
1 parent 4c8c86d commit 21ade68
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
30 changes: 26 additions & 4 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,33 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)

func CreateMessage(prefix string, text string, callsignNames []string, transmitterGroupNames []string, emergency bool) []Message {
var messages []Message
var length int
// Calculate length of message, including "xxxxx: " for prefix at start of message
length := MaxMessageLength - len(prefix) - 2
if prefix == "" {
length = MaxMessageLength
} else {
length = MaxMessageLength - len(prefix) - 2
}

texts := sliceStringByN(text, length)

for _, msg := range texts {
var text string
if length == MaxMessageLength {
text = msg
} else {
text = fmt.Sprintf("%s: %s", prefix, msg)
}
currentMessage := Message{
Text: fmt.Sprintf("%s: %s", prefix, msg),
Text: text,
CallsignNames: callsignNames,
TransmitterGroupNames: transmitterGroupNames,
Emergency: emergency,
Expand Down Expand Up @@ -60,9 +73,18 @@ func SendMessage(payloads []string, username string, password string) {
if err != nil {
log.Printf("Send Request Failed: %s\n", err.Error())
}
log.Printf("Response (%s): %s\n", resp.Status, resp.Body)

defer resp.Body.Close()

if resp.StatusCode == http.StatusCreated {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Printf("Response (%s): %s\n", resp.Status, string(bodyBytes))
} else {
log.Printf("Response (%s)\n", resp.Status)
}

}

}
16 changes: 16 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ func TestCreateMessage(t *testing.T) {
},
},
},
{
name: "testing CreateMessage empty prefix",
prefix: "",
text: "this is a test message",
callNames: []string{"testcall"},
txGrpNames: []string{"us-wa"},
emergency: false,
output: []Message{
{
Text: "this is a test message",
CallsignNames: []string{"testcall"},
TransmitterGroupNames: []string{"us-wa"},
Emergency: false,
},
},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 21ade68

Please sign in to comment.