This commit is contained in:
parent
600c44a252
commit
4c1234291a
|
@ -1,20 +1,18 @@
|
||||||
/*
|
// Package jsonfeed contains the types and a parse function for JSON feeds.
|
||||||
Microsub server
|
// Copyright (C) 2018 Peter Stuifzand
|
||||||
Copyright (C) 2018 Peter Stuifzand
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
This program is free software: you can redistribute it and/or modify
|
// it under the terms of the GNU General Public License as published by
|
||||||
it under the terms of the GNU General Public License as published by
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
// (at your option) any later version.
|
||||||
(at your option) any later version.
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
This program is distributed in the hope that it will be useful,
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// GNU General Public License for more details.
|
||||||
GNU General Public License for more details.
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
You should have received a copy of the GNU General Public License
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package jsonfeed
|
package jsonfeed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -22,6 +20,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Attachment contains attachments for podcasts
|
||||||
type Attachment struct {
|
type Attachment struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
MimeType string `json:"mime_type"`
|
MimeType string `json:"mime_type"`
|
||||||
|
@ -30,6 +29,7 @@ type Attachment struct {
|
||||||
DurationInSeconds int `json:"duration_in_seconds,omitempty"`
|
DurationInSeconds int `json:"duration_in_seconds,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Item is the main item in the feed
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
ContentText string `json:"content_text,omitempty"`
|
ContentText string `json:"content_text,omitempty"`
|
||||||
|
@ -45,17 +45,20 @@ type Item struct {
|
||||||
Attachments []Attachment `json:"attachments,omitempty"`
|
Attachments []Attachment `json:"attachments,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Author is the author of the Item
|
||||||
type Author struct {
|
type Author struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
URL string `json:"url,omitempty"`
|
URL string `json:"url,omitempty"`
|
||||||
Avatar string `json:"avatar,omitempty"`
|
Avatar string `json:"avatar,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hub contains a reference to a feed hub
|
||||||
type Hub struct {
|
type Hub struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Feed is the main object
|
||||||
type Feed struct {
|
type Feed struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
@ -72,7 +75,6 @@ type Feed struct {
|
||||||
// Parse parses a jsonfeed
|
// Parse parses a jsonfeed
|
||||||
func Parse(body io.Reader) (Feed, error) {
|
func Parse(body io.Reader) (Feed, error) {
|
||||||
var feed Feed
|
var feed Feed
|
||||||
dec := json.NewDecoder(body)
|
err := json.NewDecoder(body).Decode(&feed)
|
||||||
err := dec.Decode(&feed)
|
|
||||||
return feed, err
|
return feed, err
|
||||||
}
|
}
|
||||||
|
|
25
pkg/jsonfeed/jsonfeed_test.go
Normal file
25
pkg/jsonfeed/jsonfeed_test.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package jsonfeed
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParse(t *testing.T) {
|
||||||
|
f, err := os.Open("testdata/feed.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
feed, err := Parse(f)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "https://jsonfeed.org/version/1", feed.Version)
|
||||||
|
assert.Equal(t, "JSON Feed", feed.Title)
|
||||||
|
assert.Equal(t, "https://www.jsonfeed.org/", feed.HomePageURL)
|
||||||
|
assert.Equal(t, "https://www.jsonfeed.org/feed.json", feed.FeedURL)
|
||||||
|
|
||||||
|
assert.Len(t, feed.Items, 2)
|
||||||
|
assert.Equal(t, "http://jsonfeed.micro.blog/2020/08/07/json-feed-version.html", feed.Items[0].ID)
|
||||||
|
assert.Equal(t, "http://jsonfeed.micro.blog/2017/05/17/announcing-json-feed.html", feed.Items[1].ID)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user