Skip to content

Commit

Permalink
Moved package into cap subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-adams committed Aug 17, 2015
1 parent b08d997 commit a4b9220
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof

coverage.out
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go:
before_install:
- go get github.com/mattn/goveralls
script:
- go test -v -covermode=count -coverprofile=coverage.out
- go test ./cap -v -covermode=count -coverprofile=coverage.out
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
env:
global:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion atom_test.go → cap/atom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func getNwsAtomFeedExample() (*NWSAtomFeed, error) {
xmlData, err := ioutil.ReadFile("examples/nws_atom.xml")
xmlData, err := ioutil.ReadFile("../examples/nws_atom.xml")

if err != nil {
return nil, err
Expand Down
39 changes: 38 additions & 1 deletion cap.go → cap/cap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cap

import "encoding/xml"
import (
"encoding/xml"
"time"
)

// Alert provides basic information about the current message: its purpose, its source and its status
type Alert struct {
Expand Down Expand Up @@ -85,6 +88,32 @@ type NamedValue struct {
Value string `xml:"value"`
}

// ParseAlert parses XML bytes into a CAP 1.2 Alert
func ParseAlert(xmlData []byte) (*Alert, error) {
var alert Alert

err := xml.Unmarshal(xmlData, &alert)

if err != nil {
return nil, err
}

return &alert, nil
}

// ParseAlert parses XML bytes into a CAP 1.1 Alert
func ParseAlert11(xmlData []byte) (*Alert11, error) {
var alert Alert11

err := xml.Unmarshal(xmlData, &alert)

if err != nil {
return nil, err
}

return &alert, nil
}

// search checks a slice of NamedValues for the first value with a specific name
func search(nva *[]NamedValue, name string) string {
for _, element := range *nva {
Expand Down Expand Up @@ -135,3 +164,11 @@ func (a *Area) AddGeocode(name string, value string) {
geocode := NamedValue{ValueName: name, Value: value}
a.Geocodes = append(a.Geocodes, geocode)
}

// CAPDate is the form specified by the DateTime Data Type in 3.3.2 of the CAP specificaftion
const CAPDate string = "2006-01-02T15:04:05-07:00"

// ParseCAPDate parses a string into a CAPDate formatted value
func ParseCAPDate(dtValue string) (time.Time, error) {
return time.Parse(CAPDate, dtValue)
}
66 changes: 55 additions & 11 deletions cap_test.go → cap/cap_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package cap

import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"testing"
)

func getCAPAlertExample() (*Alert11, error) {
xmlData, err := ioutil.ReadFile("examples/nws_alert.xml")
xmlData, err := ioutil.ReadFile("../examples/nws_alert.xml")

if err != nil {
return nil, err
}

var alert Alert11

err = xml.Unmarshal(xmlData, &alert)

if err != nil {
return nil, err
}

return &alert, nil
return ParseAlert11(xmlData)
}

func TestUnmarshalAlertHasProperValues(t *testing.T) {
Expand Down Expand Up @@ -294,3 +287,54 @@ func TestAreaGecodeReturnsEmptyStringIfNotFound(t *testing.T) {
geocodeValue := area.Geocode("not-a-real-key")
assertEqual(t, geocodeValue, "", "Geocode did not return an empty string")
}

func TestParseCAPDateReturnsCorrectValue(t *testing.T) {
alert, err := getCAPAlertExample()

if err != nil {
t.Fatal(err)
}

info := alert.Infos[0]
dt, _ := ParseCAPDate(info.EffectiveDate)
_, zoneOffset := dt.Zone()
zoneOffsetHours := zoneOffset / 3600

// 2015-08-15T20:45:00-05:00
assertEqual(t, 2015, int(dt.Year()), "Year does not equal expected value")
assertEqual(t, 8, int(dt.Month()), "Month does not equal expected value")
assertEqual(t, 15, int(dt.Day()), "Day does not equal expected value")
assertEqual(t, 20, int(dt.Hour()), "Hour does not equal expected value")
assertEqual(t, 45, int(dt.Minute()), "Minute does not equal expected value")
assertEqual(t, 0, int(dt.Second()), "Second does not equal expected value")
assertEqual(t, -5, zoneOffsetHours, "TZ offset does not equal expected value")
}

func TestABC(t *testing.T) {
var xmlData = []byte(`<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<alert xmlns='urn:oasis:names:tc:emergency:cap:1.2'>
<identifier>NOAA-NWS-ALERTS-AR1253BA3B00A4.FloodWarning.1253BA3D4A94AR.LZKFLSLZK.342064b5a5aafb8265dfc3707d6a3b09</identifier>
<sender>[email protected]</sender>
<sent>2015-08-15T20:45:00-05:00</sent>
<status>Actual</status>
<msgType>Alert</msgType>
<scope>Public</scope>
<info>
<category>Met</category>
<event>Flood Warning</event>
<urgency>Expected</urgency>
<severity>Moderate</severity>
<certainty>Likely</certainty>
</info>
</alert>`)

alert, err := ParseAlert(xmlData)

if err != nil {
fmt.Errorf(err.Error())
os.Exit(1)
}

fmt.Println(alert.MessageID)
fmt.Println(alert.Infos[0].EventType)
}
File renamed without changes.

0 comments on commit a4b9220

Please sign in to comment.