Compare commits
No commits in common. "4c1234291af815ec28cae1ffca1c844b35eb42a7" and "ac30bfb72b350986aab7153ed5d0beb747d37540" have entirely different histories.
4c1234291a
...
ac30bfb72b
|
|
@ -1,18 +1,20 @@
|
||||||
// Package jsonfeed contains the types and a parse function for JSON feeds.
|
/*
|
||||||
// Copyright (C) 2018 Peter Stuifzand
|
Microsub server
|
||||||
//
|
Copyright (C) 2018 Peter Stuifzand
|
||||||
// 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
|
This program is free software: you can redistribute it and/or modify
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
it under the terms of the GNU General Public License as published by
|
||||||
// (at your option) any later version.
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
//
|
(at your option) any later version.
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
This program is distributed in the hope that it will be useful,
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// GNU General Public License for more details.
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
//
|
GNU General Public License for more details.
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
package jsonfeed
|
package jsonfeed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -20,7 +22,6 @@ 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"`
|
||||||
|
|
@ -29,7 +30,6 @@ 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,26 +45,23 @@ 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"`
|
||||||
HomePageURL string `json:"home_page_url"`
|
HomePageURL string `json:"home_page_url"`
|
||||||
FeedURL string `json:"feed_url"`
|
FeedURL string `json:"feed_url"`
|
||||||
NextURL string `json:"next_url"`
|
NextUrl string `json:"next_url"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Favicon string `json:"favicon"`
|
Favicon string `json:"favicon"`
|
||||||
Author Author `json:"author,omitempty"`
|
Author Author `json:"author,omitempty"`
|
||||||
|
|
@ -75,6 +72,7 @@ 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
|
||||||
err := json.NewDecoder(body).Decode(&feed)
|
dec := json.NewDecoder(body)
|
||||||
|
err := dec.Decode(&feed)
|
||||||
return feed, err
|
return feed, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
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