From b9ce23aba92ac0fded30669ad501a68307d55017 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 14 Jul 2020 21:25:52 -0400 Subject: [PATCH] commit code --- go.mod | 5 ++++ go.sum | 10 +++++++ jsonfeed.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++ jsonfeed_test.go | 42 ++++++++++++++++++++++++++ testdata/feed.json | 21 +++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 jsonfeed.go create mode 100644 jsonfeed_test.go create mode 100644 testdata/feed.json diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..434a8f5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module tulpa.dev/Xe/jsonfeed + +go 1.14 + +require github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..56d62e7 --- /dev/null +++ b/go.sum @@ -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= diff --git a/jsonfeed.go b/jsonfeed.go new file mode 100644 index 0000000..913880d --- /dev/null +++ b/jsonfeed.go @@ -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 +} diff --git a/jsonfeed_test.go b/jsonfeed_test.go new file mode 100644 index 0000000..d345f38 --- /dev/null +++ b/jsonfeed_test.go @@ -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, "

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) +} diff --git a/testdata/feed.json b/testdata/feed.json new file mode 100644 index 0000000..ad4bbe1 --- /dev/null +++ b/testdata/feed.json @@ -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": "

We ...", + "date_published": "2017-05-17T08:02:12-07:00" + } + ] +}