commit code

This commit is contained in:
Cadey Ratio 2020-07-14 21:25:52 -04:00
parent 2973ba261d
commit b9ce23aba9
5 changed files with 151 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module tulpa.dev/Xe/jsonfeed
go 1.14
require github.com/stretchr/testify v1.6.1

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

73
jsonfeed.go Normal file
View File

@ -0,0 +1,73 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
package jsonfeed
import (
"encoding/json"
"io"
"time"
)
const CurrentVersion = "https://jsonfeed.org/version/1"
type Item struct {
ID string `json:"id"`
URL string `json:"url"`
ExternalURL string `json:"external_url"`
Title string `json:"title"`
ContentHTML string `json:"content_html"`
ContentText string `json:"content_text"`
Summary string `json:"summary"`
Image string `json:"image"`
BannerImage string `json:"banner_image"`
DatePublished time.Time `json:"date_published"`
DateModified time.Time `json:"date_modified"`
Author Author `json:"author"`
Tags []string `json:"tags"`
}
type Author struct {
Name string `json:"name"`
URL string `json:"url"`
Avatar string `json:"avatar"`
}
type Hub struct {
Type string `json:"type"`
URL string `json:"url"`
}
type Attachment struct {
URL string `json:"url"`
MIMEType string `json:"mime_type"`
Title string `json:"title"`
SizeInBytes int64 `json:"size_in_bytes"`
DurationInSeconds int64 `json:"duration_in_seconds"`
}
type Feed struct {
Version string `json:"version"`
Title string `json:"title"`
HomePageURL string `json:"home_page_url"`
FeedURL string `json:"feed_url"`
Description string `json:"description"`
UserComment string `json:"user_comment"`
NextURL string `json:"next_url"`
Icon string `json:"icon"`
Favicon string `json:"favicon"`
Author Author `json:"author"`
Expired bool `json:"expired"`
Hubs []Hub `json:"hubs"`
Items []Item `json:"items"`
}
func Parse(r io.Reader) (Feed, error) {
var feed Feed
decoder := json.NewDecoder(r)
if err := decoder.Decode(&feed); err != nil {
return Feed{}, err
}
return feed, nil
}

42
jsonfeed_test.go Normal file
View File

@ -0,0 +1,42 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
package jsonfeed
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseSimple(t *testing.T) {
r, err := os.Open("testdata/feed.json")
assert.NoError(t, err, "Could not open testdata/feed.json")
feed, err := Parse(r)
assert.NoError(t, err, "Could not parse testdata/feed.json")
assert.Equal(t, "https://jsonfeed.org/version/1", feed.Version)
assert.Equal(t, "JSON Feed", feed.Title)
assert.Equal(t, "JSON Feed is a ...", feed.Description)
assert.Equal(t, "https://jsonfeed.org/", feed.HomePageURL)
assert.Equal(t, "https://jsonfeed.org/feed.json", feed.FeedURL)
assert.Equal(t, "This feed allows ...", feed.UserComment)
assert.Equal(t, "https://jsonfeed.org/graphics/icon.png", feed.Favicon)
assert.Equal(t, "Brent Simmons and Manton Reece", feed.Author.Name)
assert.Equal(t, 1, len(feed.Items))
assert.Equal(t, "https://jsonfeed.org/2017/05/17/announcing_json_feed", feed.Items[0].ID)
assert.Equal(t, "https://jsonfeed.org/2017/05/17/announcing_json_feed", feed.Items[0].URL)
assert.Equal(t, "Announcing JSON Feed", feed.Items[0].Title)
assert.Equal(t, "<p>We ...", feed.Items[0].ContentHTML)
datePublished, err := time.Parse("2006-01-02T15:04:05-07:00", "2017-05-17T08:02:12-07:00")
assert.NoError(t, err, "Could not parse timestamp")
assert.Equal(t, datePublished, feed.Items[0].DatePublished)
}

21
testdata/feed.json vendored Normal file
View File

@ -0,0 +1,21 @@
{
"version": "https://jsonfeed.org/version/1",
"title": "JSON Feed",
"description": "JSON Feed is a ...",
"home_page_url": "https://jsonfeed.org/",
"feed_url": "https://jsonfeed.org/feed.json",
"user_comment": "This feed allows ...",
"favicon": "https://jsonfeed.org/graphics/icon.png",
"author": {
"name": "Brent Simmons and Manton Reece"
},
"items": [
{
"id": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
"url": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
"title": "Announcing JSON Feed",
"content_html": "<p>We ...",
"date_published": "2017-05-17T08:02:12-07:00"
}
]
}