aboutsummaryrefslogtreecommitdiffhomepage
path: root/model
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-10-14 21:43:48 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-10-14 21:43:48 -0700
commit778346b0b04bc52c89529668b37c1086bebe1674 (patch)
tree957b4d049210af1753bfa6690a2810f6634193ad /model
parent5870f0426002c8e26a9ff472b23e15d7bf1235f7 (diff)
Simplify feed fetcher
- Add browser package to handle HTTP errors - Reduce code duplication
Diffstat (limited to 'model')
-rw-r--r--model/feed.go45
-rw-r--r--model/feed_test.go101
2 files changed, 145 insertions, 1 deletions
diff --git a/model/feed.go b/model/feed.go
index 0ace098..63957c6 100644
--- a/model/feed.go
+++ b/model/feed.go
@@ -7,9 +7,11 @@ package model // import "miniflux.app/model"
import (
"fmt"
"time"
+
+ "miniflux.app/http/client"
)
-// Feed represents a feed in the database.
+// Feed represents a feed in the application.
type Feed struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
@@ -43,5 +45,46 @@ func (f *Feed) String() string {
)
}
+// WithClientResponse updates feed attributes from an HTTP request.
+func (f *Feed) WithClientResponse(response *client.Response) {
+ f.EtagHeader = response.ETag
+ f.LastModifiedHeader = response.LastModified
+ f.FeedURL = response.EffectiveURL
+}
+
+// WithCategoryID initializes the category attribute of the feed.
+func (f *Feed) WithCategoryID(categoryID int64) {
+ f.Category = &Category{ID: categoryID}
+}
+
+// WithBrowsingParameters defines browsing parameters.
+func (f *Feed) WithBrowsingParameters(crawler bool, userAgent, username, password string) {
+ f.Crawler = crawler
+ f.UserAgent = userAgent
+ f.Username = username
+ f.Password = password
+}
+
+// WithError adds a new error message and increment the error counter.
+func (f *Feed) WithError(message string) {
+ f.ParsingErrorCount++
+ f.ParsingErrorMsg = message
+}
+
+// ResetErrorCounter removes all previous errors.
+func (f *Feed) ResetErrorCounter() {
+ f.ParsingErrorCount = 0
+ f.ParsingErrorMsg = ""
+}
+
+// CheckedNow set attribute values when the feed is refreshed.
+func (f *Feed) CheckedNow() {
+ f.CheckedAt = time.Now()
+
+ if f.SiteURL == "" {
+ f.SiteURL = f.FeedURL
+ }
+}
+
// Feeds is a list of feed
type Feeds []*Feed
diff --git a/model/feed_test.go b/model/feed_test.go
new file mode 100644
index 0000000..3ca81b4
--- /dev/null
+++ b/model/feed_test.go
@@ -0,0 +1,101 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package model // import "miniflux.app/reader/model"
+
+import (
+ "testing"
+
+ "miniflux.app/http/client"
+)
+
+func TestFeedWithResponse(t *testing.T) {
+ response := &client.Response{ETag: "Some etag", LastModified: "Some date", EffectiveURL: "Some URL"}
+
+ feed := &Feed{}
+ feed.WithClientResponse(response)
+
+ if feed.EtagHeader != "Some etag" {
+ t.Fatal(`The ETag header should be set`)
+ }
+
+ if feed.LastModifiedHeader != "Some date" {
+ t.Fatal(`The LastModified header should be set`)
+ }
+
+ if feed.FeedURL != "Some URL" {
+ t.Fatal(`The Feed URL should be set`)
+ }
+}
+
+func TestFeedCategorySetter(t *testing.T) {
+ feed := &Feed{}
+ feed.WithCategoryID(int64(123))
+
+ if feed.Category == nil {
+ t.Fatal(`The category field should not be null`)
+ }
+
+ if feed.Category.ID != int64(123) {
+ t.Error(`The category ID must be set`)
+ }
+}
+
+func TestFeedBrowsingParams(t *testing.T) {
+ feed := &Feed{}
+ feed.WithBrowsingParameters(true, "Custom User Agent", "Username", "Secret")
+
+ if !feed.Crawler {
+ t.Error(`The crawler must be activated`)
+ }
+
+ if feed.UserAgent != "Custom User Agent" {
+ t.Error(`The user agent must be set`)
+ }
+
+ if feed.Username != "Username" {
+ t.Error(`The username must be set`)
+ }
+
+ if feed.Password != "Secret" {
+ t.Error(`The password must be set`)
+ }
+}
+
+func TestFeedErrorCounter(t *testing.T) {
+ feed := &Feed{}
+ feed.WithError("Some Error")
+
+ if feed.ParsingErrorMsg != "Some Error" {
+ t.Error(`The error message must be set`)
+ }
+
+ if feed.ParsingErrorCount != 1 {
+ t.Error(`The error counter must be set to 1`)
+ }
+
+ feed.ResetErrorCounter()
+
+ if feed.ParsingErrorMsg != "" {
+ t.Error(`The error message must be removed`)
+ }
+
+ if feed.ParsingErrorCount != 0 {
+ t.Error(`The error counter must be set to 0`)
+ }
+}
+
+func TestFeedCheckedNow(t *testing.T) {
+ feed := &Feed{}
+ feed.FeedURL = "https://example.org/feed"
+ feed.CheckedNow()
+
+ if feed.SiteURL != feed.FeedURL {
+ t.Error(`The site URL must not be empty`)
+ }
+
+ if feed.CheckedAt.IsZero() {
+ t.Error(`The checked date must be set`)
+ }
+}