Skip to content

Commit

Permalink
rss decoder added
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed May 13, 2017
1 parent 6febf5b commit 298ef85
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func (s *ServiceConfig) initBackendDefaults(e, b int) {
switch strings.ToLower(backend.Encoding) {
case encoding.XML:
backend.Decoder = encoding.NewXMLDecoder(backend.IsCollection)
case encoding.RSS:
backend.Decoder = encoding.NewRSSDecoder()
default:
backend.Decoder = encoding.NewJSONDecoder(backend.IsCollection)
}
Expand Down
7 changes: 6 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func TestConfig_init(t *testing.T) {
Host: []string{"https://jsonplaceholder.typicode.com"},
Mapping: map[string]string{"email": "personal_email"},
}
rssBackend := Backend{
URLPattern: "/users/{user}",
Host: []string{"https://jsonplaceholder.typicode.com"},
Encoding: "rss",
}
postBackend := Backend{
URLPattern: "/posts/{user}",
Host: []string{"https://jsonplaceholder.typicode.com"},
Expand All @@ -138,7 +143,7 @@ func TestConfig_init(t *testing.T) {
}
userEndpoint := EndpointConfig{
Endpoint: "/users/{user}",
Backend: []*Backend{&userBackend, &postBackend},
Backend: []*Backend{&userBackend, &rssBackend, &postBackend},
}

subject := ServiceConfig{
Expand Down
38 changes: 38 additions & 0 deletions encoding/rss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package encoding

import (
"io"

"github.com/mmcdole/gofeed"
)

// RSS is the key for the rss encoding
const RSS = "rss"

// NewRSSDecoder returns the RSS decoder
func NewRSSDecoder() Decoder {
fp := gofeed.NewParser()
return func(r io.Reader, v *map[string]interface{}) error {
feed, err := fp.Parse(r)
if err != nil {
return err
}
*(v) = map[string]interface{}{
"items": feed.Items,
"author": feed.Author,
"categories": feed.Categories,
"custom": feed.Custom,
"copyright": feed.Copyright,
"description": feed.Description,
"type": feed.FeedType,
"language": feed.Language,
"title": feed.Title,
"published": feed.Published,
"updated": feed.Updated,
}
if feed.Image != nil {
(*v)["img_url"] = feed.Image.URL
}
return nil
}
}
76 changes: 76 additions & 0 deletions encoding/rss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package encoding

import (
"bytes"
"testing"

"github.com/mmcdole/gofeed"
)

func TestRSSEncoder(t *testing.T) {
subject := bytes.NewBuffer([]byte(`<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<image>
<url>https://www.w3schools.com/pretty_image.png</url>
<description>Free web building tutorials</description>
</image>
<language>en_us</language>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
<item>
<title>XML Tutorial</title>
<link>https://www.w3schools.com/xml</link>
<description>New XML tutorial on W3Schools</description>
</item>
</channel>
</rss>`))

decoder := NewRSSDecoder()

var result map[string]interface{}

if err := decoder(subject, &result); err != nil {
t.Error(err)
}

if result["type"] != "rss" {
t.Error("Error unexpected result type:", result["type"])
}

if result["description"] != "Free web building tutorials" {
t.Error("Error unexpected description:", result["description"])
}

if result["language"] != "en_us" {
t.Error("Error unexpected language:", result["language"])
}

if result["img_url"] != "https://www.w3schools.com/pretty_image.png" {
t.Error("Error unexpected image url:", result["img_url"])
}

if len(result["items"].([]*gofeed.Item)) != 2 {
t.Error("Error unexpected number of result items", result["items"])
}

}

func TestRSSEncoder_ko(t *testing.T) {
decoder := NewRSSDecoder()

var result map[string]interface{}

if err := decoder(bytes.NewBuffer([]byte(``)), &result); err == nil {
t.Error("The decoder didn't return an error")
}

}

0 comments on commit 298ef85

Please sign in to comment.