Skip to content

Commit

Permalink
cleaned warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutz Horn authored and SlyMarbo committed Jun 6, 2017
1 parent 495adb9 commit 023392a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Item struct {

type Image struct {
Title string
Url string
URL string
Height uint32
Width uint32
}
Expand Down
8 changes: 4 additions & 4 deletions atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func parseAtom(data []byte) (*Feed, error) {
out.Refresh = time.Now().Add(10 * time.Minute)

if feed.Items == nil {
return nil, fmt.Errorf("Error: no feeds found in %q.", string(data))
return nil, fmt.Errorf("no feeds found in %q", string(data))
}

out.Items = make([]*Item, 0, len(feed.Items))
Expand Down Expand Up @@ -60,7 +60,7 @@ func parseAtom(data []byte) (*Feed, error) {
next.Link = link.Href
} else {
next.Enclosures = append(next.Enclosures, &Enclosure{
Url: link.Href,
URL: link.Href,
Type: link.Type,
Length: link.Length,
})
Expand Down Expand Up @@ -121,7 +121,7 @@ type atomItem struct {
type atomImage struct {
XMLName xml.Name `xml:"image"`
Title string `xml:"title"`
Url string `xml:"url"`
URL string `xml:"url"`
Height int `xml:"height"`
Width int `xml:"width"`
}
Expand All @@ -136,7 +136,7 @@ type atomLink struct {
func (a *atomImage) Image() *Image {
out := new(Image)
out.Title = a.Title
out.Url = a.Url
out.URL = a.URL
out.Height = uint32(a.Height)
out.Width = uint32(a.Width)
return out
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package RSS is a small library for simplifying the parsing of RSS and Atom feeds.
Package rss is a small library for simplifying the parsing of RSS and Atom feeds.
The package could do with more testing, but it conforms to the RSS 1.0, 2.0, and Atom 1.0
specifications, to the best of my ability. I've tested it with about 15 different feeds,
Expand Down Expand Up @@ -58,7 +58,7 @@ type Item struct {
type Image struct {
Title string
Url string
URL string
Height uint32
Width uint32
}
Expand Down
30 changes: 20 additions & 10 deletions rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func Parse(data []byte) (*Feed, error) {
}
}

// A FetchFunc is a function that fetches a feed for given URL.
type FetchFunc func(url string) (resp *http.Response, err error)

// DefaultFetchFunc uses http.DefaultClient to fetch a feed.
var DefaultFetchFunc = func(url string) (resp *http.Response, err error) {
client := http.DefaultClient
return client.Get(url)
Expand All @@ -46,13 +48,15 @@ func Fetch(url string) (*Feed, error) {
return FetchByFunc(DefaultFetchFunc, url)
}

// FetchByClient uses a http.Client to fetch a URL.
func FetchByClient(url string, client *http.Client) (*Feed, error) {
fetchFunc := func(url string) (resp *http.Response, err error) {
return client.Get(url)
}
return FetchByFunc(fetchFunc, url)
}

// FetchByFunc uses a func to fetch a URL.
func FetchByFunc(fetchFunc FetchFunc, url string) (*Feed, error) {
resp, err := fetchFunc(url)
if err != nil {
Expand Down Expand Up @@ -111,7 +115,7 @@ func (r refreshError) Temporary() bool {
return true
}

var ErrUpdateNotReady refreshError = "not ready to update: too soon to refresh"
var errUpdateNotReady refreshError = "not ready to update: too soon to refresh"

// Update fetches any new items and updates f.
func (f *Feed) Update() error {
Expand All @@ -121,15 +125,16 @@ func (f *Feed) Update() error {
return f.UpdateByFunc(f.FetchFunc)
}

// UpdateByFunc uses a func to update f.
func (f *Feed) UpdateByFunc(fetchFunc FetchFunc) error {

// Check that we don't update too often.
if f.Refresh.After(time.Now()) {
return ErrUpdateNotReady
return errUpdateNotReady
}

if f.UpdateURL == "" {
return errors.New("Error: feed has no URL.")
return errors.New("feed has no URL")
}

if f.ItemMap == nil {
Expand Down Expand Up @@ -171,7 +176,7 @@ func (f *Feed) String() string {
fmt.Fprintf(w, "\xff\t\xffDescription:\t%q\n", f.Description)
fmt.Fprintf(w, "\xff\t\xffLink:\t%q\n", f.Link)
fmt.Fprintf(w, "\xff\t\xffUpdateURL:\t%q\n", f.UpdateURL)
fmt.Fprintf(w, "\xff\t\xffImage:\t%q (%s)\n", f.Image.Title, f.Image.Url)
fmt.Fprintf(w, "\xff\t\xffImage:\t%q (%s)\n", f.Image.Title, f.Image.URL)
fmt.Fprintf(w, "\xff\t\xffRefresh:\t%s\n", f.Refresh.Format(DATE))
fmt.Fprintf(w, "\xff\t\xffUnread:\t%d\n", f.Unread)
fmt.Fprintf(w, "\xff\t\xffItems:\t(%d) {\n", len(f.Items))
Expand Down Expand Up @@ -213,6 +218,7 @@ func (i *Item) String() string {
return i.Format(0)
}

// Format formats an item using tabs.
func (i *Item) Format(indent int) string {
buf := new(bytes.Buffer)
single := strings.Repeat("\t", indent)
Expand Down Expand Up @@ -242,38 +248,42 @@ func (i *Item) Format(indent int) string {
return buf.String()
}

// Enclosure maps an enclosure.
type Enclosure struct {
Url string `json:"url"`
URL string `json:"url"`
Type string `json:"type"`
Length uint `json:"length"`
}

// Get uses http.Get to fetch an enclosure.
func (e *Enclosure) Get() (io.ReadCloser, error) {
if e == nil || e.Url == "" {
if e == nil || e.URL == "" {
return nil, errors.New("No enclosure")
}

res, err := http.Get(e.Url)
res, err := http.Get(e.URL)
if err != nil {
return nil, err
}

return res.Body, nil
}

// Image maps an image.
type Image struct {
Title string `json:"title"`
Url string `json:"url"`
URL string `json:"url"`
Height uint32 `json:"height"`
Width uint32 `json:"width"`
}

// Get uses http.Get to fetch an image.
func (i *Image) Get() (io.ReadCloser, error) {
if i == nil || i.Url == "" {
if i == nil || i.URL == "" {
return nil, errors.New("No image")
}

res, err := http.Get(i.Url)
res, err := http.Get(i.URL)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions rss_1.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func parseRSS1(data []byte) (*Feed, error) {
return nil, err
}
if feed.Channel == nil {
return nil, fmt.Errorf("Error: no channel found in %q.", string(data))
return nil, fmt.Errorf("no channel found in %q", string(data))
}

channel := feed.Channel
Expand Down Expand Up @@ -57,7 +57,7 @@ func parseRSS1(data []byte) (*Feed, error) {
}

if feed.Items == nil {
return nil, fmt.Errorf("Error: no feeds found in %q.", string(data))
return nil, fmt.Errorf("no feeds found in %q", string(data))
}

out.Items = make([]*Item, 0, len(feed.Items))
Expand Down Expand Up @@ -151,14 +151,14 @@ type rss1_0Item struct {

type rss1_0Enclosure struct {
XMLName xml.Name `xml:"enclosure"`
Url string `xml:"resource,attr"`
URL string `xml:"resource,attr"`
Type string `xml:"type,attr"`
Length uint `xml:"length,attr"`
}

func (r *rss1_0Enclosure) Enclosure() *Enclosure {
out := new(Enclosure)
out.Url = r.Url
out.URL = r.URL
out.Type = r.Type
out.Length = r.Length
return out
Expand All @@ -167,15 +167,15 @@ func (r *rss1_0Enclosure) Enclosure() *Enclosure {
type rss1_0Image struct {
XMLName xml.Name `xml:"image"`
Title string `xml:"title"`
Url string `xml:"url"`
URL string `xml:"url"`
Height int `xml:"height"`
Width int `xml:"width"`
}

func (i *rss1_0Image) Image() *Image {
out := new(Image)
out.Title = i.Title
out.Url = i.Url
out.URL = i.URL
out.Height = uint32(i.Height)
out.Width = uint32(i.Width)
return out
Expand Down
12 changes: 6 additions & 6 deletions rss_2.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func parseRSS2(data []byte) (*Feed, error) {
return nil, err
}
if feed.Channel == nil {
return nil, fmt.Errorf("Error: no channel found in %q.", string(data))
return nil, fmt.Errorf("no channel found in %q", string(data))
}

channel := feed.Channel
Expand Down Expand Up @@ -62,7 +62,7 @@ func parseRSS2(data []byte) (*Feed, error) {
}

if channel.Items == nil {
return nil, fmt.Errorf("Error: no feeds found in %q.", string(data))
return nil, fmt.Errorf("no feeds found in %q", string(data))
}

out.Items = make([]*Item, 0, len(channel.Items))
Expand Down Expand Up @@ -165,14 +165,14 @@ type rss2_0Item struct {

type rss2_0Enclosure struct {
XMLName xml.Name `xml:"enclosure"`
Url string `xml:"url,attr"`
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
Length uint `xml:"length,attr"`
}

func (r *rss2_0Enclosure) Enclosure() *Enclosure {
out := new(Enclosure)
out.Url = r.Url
out.URL = r.URL
out.Type = r.Type
out.Length = r.Length
return out
Expand All @@ -181,15 +181,15 @@ func (r *rss2_0Enclosure) Enclosure() *Enclosure {
type rss2_0Image struct {
XMLName xml.Name `xml:"image"`
Title string `xml:"title"`
Url string `xml:"url"`
URL string `xml:"url"`
Height int `xml:"height"`
Width int `xml:"width"`
}

func (i *rss2_0Image) Image() *Image {
out := new(Image)
out.Title = i.Title
out.Url = i.Url
out.URL = i.URL
out.Height = uint32(i.Height)
out.Width = uint32(i.Width)
return out
Expand Down
1 change: 1 addition & 0 deletions rss_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ package rss

const debug = false

// DATE is a constant date string.
const DATE = "15:04:05 MST 02/01/2006"
8 changes: 4 additions & 4 deletions rss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestParseTitle(t *testing.T) {

func TestEnclosure(t *testing.T) {
tests := map[string]Enclosure{
"rss_1.0": Enclosure{Url: "http://foo.bar/baz.mp3", Type: "audio/mpeg", Length: 65535},
"rss_2.0": Enclosure{Url: "http://example.com/file.mp3", Type: "audio/mpeg", Length: 65535},
"rss_2.0-1": Enclosure{Url: "http://gdb.voanews.com/6C49CA6D-C18D-414D-8A51-2B7042A81010_cx0_cy29_cw0_w800_h450.jpg", Type: "image/jpeg", Length: 3123},
"atom_1.0": Enclosure{Url: "http://example.org/audio.mp3", Type: "audio/mpeg", Length: 1234},
"rss_1.0": Enclosure{URL: "http://foo.bar/baz.mp3", Type: "audio/mpeg", Length: 65535},
"rss_2.0": Enclosure{URL: "http://example.com/file.mp3", Type: "audio/mpeg", Length: 65535},
"rss_2.0-1": Enclosure{URL: "http://gdb.voanews.com/6C49CA6D-C18D-414D-8A51-2B7042A81010_cx0_cy29_cw0_w800_h450.jpg", Type: "image/jpeg", Length: 3123},
"atom_1.0": Enclosure{URL: "http://example.org/audio.mp3", Type: "audio/mpeg", Length: 1234},
}

for test, want := range tests {
Expand Down

0 comments on commit 023392a

Please sign in to comment.