Refactor to remove helpers package

This commit removes the "helpers" package by moving functions from the
package into the other packages that use them.

CheckPayloadSignature() and ExtractParamater() are simply moved to the
"hook" package.  I'm not sure of the usefulness of having these
functions exported, but I left them allow for now.

ValuesToMap() is moved to the "main" webhook package and renamed to
valuesToMap().

Tests were moved into the "hook" package since we only test
ExtractParameter() right now.

This commit closes adnanh/webhook#12.
This commit is contained in:
Cameron Moore 2015-03-20 08:55:42 -05:00
parent d8a21582a3
commit 7dd55f5232
4 changed files with 77 additions and 84 deletions

View file

@ -12,7 +12,6 @@ import (
"os/exec"
"strings"
"github.com/adnanh/webhook/helpers"
"github.com/adnanh/webhook/hook"
"github.com/codegangsta/negroni"
@ -143,10 +142,10 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
}
// parse headers
headers := helpers.ValuesToMap(r.Header)
headers := valuesToMap(r.Header)
// parse query variables
query := helpers.ValuesToMap(r.URL.Query())
query := valuesToMap(r.URL.Query())
// parse body
var payload map[string]interface{}
@ -167,7 +166,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("error parsing form payload %+v\n", err)
} else {
payload = helpers.ValuesToMap(fd)
payload = valuesToMap(fd)
}
}
@ -236,3 +235,16 @@ func watchForFileChange() {
}
}
}
// valuesToMap converts map[string][]string to a map[string]string object
func valuesToMap(values map[string][]string) map[string]interface{} {
ret := make(map[string]interface{})
for key, value := range values {
if len(value) > 0 {
ret[key] = value[0]
}
}
return ret
}