Skip to content

Commit

Permalink
Merge pull request #20 from ndaidong/v3.0.2
Browse files Browse the repository at this point in the history
v3.0.2
  • Loading branch information
ndaidong authored Nov 24, 2021
2 parents b682389 + 9994d25 commit 3b28d2a
Showing 9 changed files with 71 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ Parse RSS/ATOM data from given feed url.
[![NPM](https://badge.fury.io/js/feed-reader.svg)](https://badge.fury.io/js/feed-reader)
![CI test](https://github.com/ndaidong/feed-reader/workflows/ci-test/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/feed-reader/badge.svg)](https://coveralls.io/github/ndaidong/feed-reader)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ndaidong_feed-reader&metric=alert_status)](https://sonarcloud.io/dashboard?id=ndaidong_feed-reader)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=ndaidong_feed-reader&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=ndaidong_feed-reader)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.1",
"version": "3.0.2",
"name": "feed-reader",
"description": "Parse ATOM/RSS data from given feed url",
"homepage": "https://www.npmjs.com/package/feed-reader",
42 changes: 0 additions & 42 deletions src/config.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/main.test.js
Original file line number Diff line number Diff line change
@@ -93,3 +93,21 @@ entryAttrs.forEach((k) => {
expect(hasProperty(state.rssFeedEntry, k)).toBe(true)
})
})

test('test parse from a more complicate atom source', async () => {
const url = 'https://headline.com/atom'
const xml = readFileSync('test-data/another-atom.xml', 'utf8')
const { baseUrl, path } = parseUrl(url)
nock(baseUrl).head(path).reply(200)
nock(baseUrl).get(path).reply(200, xml, {
'Content-Type': 'application/xml'
})
const result = await parse(url)
expect(result).toBeInstanceOf(Object)
feedAttrs.forEach((k) => {
expect(hasProperty(result, k)).toBe(true)
})
entryAttrs.forEach((k) => {
expect(hasProperty(result.entries[0], k)).toBe(true)
})
})
3 changes: 1 addition & 2 deletions src/utils/parser.js
Original file line number Diff line number Diff line change
@@ -94,7 +94,6 @@ const parseRSS = (xmldata) => {

const parseAtom = (xmldata) => {
const { feed = {} } = xmldata

const {
title = '',
link = '',
@@ -105,7 +104,7 @@ const parseAtom = (xmldata) => {
entry = []
} = feed

const entries = entry.map(nomalizeAtomItem)
const entries = isArray(entry) ? entry.map(nomalizeAtomItem) : [nomalizeAtomItem(entry)]

return {
title: toText(title),
15 changes: 14 additions & 1 deletion src/utils/purifyUrl.test.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,20 @@

const purifyUrl = require('./purifyUrl')

test('test purifyUrl in detailt', () => {
test('test purifyUrl() with invalid url', () => {
const urls = [
null,
'',
123,
{}
]
urls.forEach((url) => {
const result = purifyUrl(url)
expect(result).toEqual(null)
})
})

test('test purifyUrl() removing regular marketing params', () => {
const entries = [
{
url: 'https://some.where/article/abc-xyz',
17 changes: 10 additions & 7 deletions src/utils/retrieve.js
Original file line number Diff line number Diff line change
@@ -7,17 +7,20 @@ const {
info
} = require('./logger')

const { getFetchOptions } = require('../config')
const { name, version } = require('../../package.json')

const fetchOptions = {
headers: {
'user-agent': `${name}/${version}`
},
timeout: 30 * 1e3,
redirect: 'follow'
}

module.exports = async (url) => {
try {
info(`Start loading feed content from "${url}"`)
const res = await got(url, getFetchOptions())
if (res.statusCode !== 200) {
error(`Got ${res.statusCode} error code from "${url}"`)
return null
}

const res = await got(url, fetchOptions)
const contentType = res.headers['content-type'] || ''
if (!contentType || !contentType.includes('xml')) {
error(`Got invalid content-type (${contentType}) from "${url}"`)
3 changes: 3 additions & 0 deletions src/utils/retrieve.test.js
Original file line number Diff line number Diff line change
@@ -39,6 +39,9 @@ test('test retrieve with invalid status', async () => {
const url = 'https://some.where/bad/page'
const { baseUrl, path } = parseUrl(url)
nock(baseUrl).head(path).reply(500)
nock(baseUrl).get(path).reply(500, '<xml><message>Error 500</message></xml>', {
'Content-Type': 'application/xml'
})
const result = await retrieve(url)
expect(result).toBe(null)
})
23 changes: 23 additions & 0 deletions test-data/another-atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<feed version="0.3"
xmlns="http://purl.org/atom/ns#"
xmlns:g="http://base.google.com/ns/1.0">
<title>The name of your data feed</title>
<link href="http://www.example.com" rel="alternate" type="text/html" />
<modified>2005-10-11T18:30:02Z</modified>
<author>
<name>Google</name>
</author>
<id>tag:google.com,2005-10-15:/support/products</id>
<entry>
<title>Red wool sweater</title>
<link href="http://www.example.com/item1-info-page.html" />
<summary>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</summary>
<id>tag:google.com,2005-10-15:/support/products</id>
<issued>2005-10-13T18:30:02Z</issued>
<modified>2005-10-13T18:30:02Z</modified>
<g:image_link>http://www.example.com/image1.jpg</g:image_link>
<g:price>25</g:price>
<g:condition>new</g:condition>
</entry>
</feed>

0 comments on commit 3b28d2a

Please sign in to comment.