示例#1
0
func (w *Wunderground) Hourly(zipCode string) (*features.HourlyResponse, error) {
	err := validation.Zip(zipCode)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("%s/api/%s/hourly/q/%s.json", w.ApiHost(), w.ApiKey(), zipCode)

	resp, err := w.makeRequest(url)
	defer resp.Body.Close()
	if err != nil {
		log.Panic("Error making request", err)
	}

	hourlyResp := &features.HourlyResponse{}
	err = json.NewDecoder(resp.Body).Decode(hourlyResp)
	if err != nil {
		log.Panic("Error unmarshalling condition body ", err)
	}

	err = hourlyResp.Response.HasError()
	if err != nil {
		return nil, err
	}

	return hourlyResp, nil
}
示例#2
0
package validation_test

import (
	"github.com/wfernandes/weather/wunderground/validation"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Zip", func() {

	It("returns error when empty", func() {

		err := validation.Zip("")
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(Equal(validation.ErrInvalidZip))
	})

	It("returns error for invalid zip code", func() {
		err := validation.Zip("123")
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(Equal(validation.ErrInvalidZip))

		err = validation.Zip("1234#")
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(Equal(validation.ErrInvalidZip))

		err = validation.Zip("12345")
		Expect(err).ToNot(HaveOccurred())
	})
})