Skip to content

Commit e9df18f

Browse files
committed
Refactor
1 parent 6e97fa4 commit e9df18f

File tree

7 files changed

+38
-35
lines changed

7 files changed

+38
-35
lines changed

__snapshots__/index.js.snap-shot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ exports['the-verge 1'] = {
647647
"url": "http://www.theverge.com/2016/5/24/11763836/apple-siri-speaker-amazon-echo-alexa-google-home-ai"
648648
}
649649

650-
exports['twitter 1'] = {
650+
exports['twitter (tweet with gif) 1'] = {
651651
"author": "Kikobeats",
652652
"date": "2017-06-28T21:01:00.000Z",
653653
"description": "“Experimenting with Clearbit API + Apple TV 3D Parallax https://t.co/Qsm163k4mJ https://t.co/5bcuqoEyAa”",
@@ -658,7 +658,7 @@ exports['twitter 1'] = {
658658
"url": "https://twitter.com/Kikobeats/status/880139124791029763"
659659
}
660660

661-
exports['twitter 2'] = {
661+
exports['twitter (tweet with image) 1'] = {
662662
"author": "K4rliky",
663663
"date": "2017-11-25T19:04:00.000Z",
664664
"description": "“Lo mejor de @codemotion_es #codemotionMadrid es estar con la gente que quieres 😍@ladyCircus”",

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
'use strict'
22

3-
const {reduce} = require('lodash')
3+
const {isEmpty, reduce} = require('lodash')
44
const {ensureAsync} = require('async')
55
const {promisify} = require('util')
66

77
const getData = require('./src/get-data')
88
const loadHtml = require('./src/html')
9-
const {props} = getData
9+
const {props, getConnector} = getData
1010

1111
const getMetaData = ensureAsync(({url, html}, cb) => {
1212
const htmlDom = loadHtml(html)
1313

1414
const output = reduce(props, (acc, conditions, propName) => {
15-
const value = getData({htmlDom, url, conditions})
16-
// TODO: Avoid response nil values
17-
acc[propName] = value
15+
const value = Object.assign(
16+
getData({htmlDom, url, conditions}) || {},
17+
getConnector({htmlDom, url})
18+
)
19+
20+
acc[propName] = !isEmpty(value) ? value : null
1821
return acc
1922
}, {})
2023

src/get-data/connectors/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const reqAll = require('req-all')
4+
const {find} = require('lodash')
5+
6+
const domains = reqAll('./domains')
7+
8+
module.exports = ({htmlDom, url}) => {
9+
const connector = find(domains, domain => domain.test(url))
10+
return connector && connector(({htmlDom, url}))
11+
}

src/get-data/index.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
'use strict'
22

33
const rules = require('req-all')('./rules')
4+
const {isEmpty} = require('lodash')
45

5-
const isValid = result => result !== null && result !== undefined && result !== ''
6-
7-
const getValue = ({htmlDom, url, conditions}) => {
6+
module.exports = ({htmlDom, url, conditions}) => {
87
const size = conditions.length
98
let index = -1
10-
let value = null
9+
let value
1110

12-
while (!isValid(value) && index++ < size - 1) {
11+
while (isEmpty(value) && index++ < size - 1) {
1312
value = conditions[index](htmlDom, url)
1413
}
1514

1615
return value
1716
}
1817

19-
const getData = ({htmlDom, url, conditions}) => {
20-
const data = getValue({htmlDom, url, conditions})
21-
return isValid(data) ? data : null
22-
}
23-
24-
getData.props = rules
25-
26-
module.exports = getData
18+
module.exports.props = rules
19+
module.exports.getConnector = require('./connectors')

src/html/index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
'use strict'
22

33
const sanitizeHtml = require('sanitize-html')
4-
const {flow} = require('lodash')
4+
const {flow, isNil} = require('lodash')
55
const cheerio = require('cheerio')
66

7+
const normalizeAttributes = propName => (tagName, attribs) => {
8+
if (!isNil(attribs[propName])) attribs[propName] = attribs[propName].toLowerCase()
9+
return {tagName, attribs}
10+
}
11+
712
const sanitize = html => sanitizeHtml(html, {
813
allowedTags: false,
914
allowedAttributes: false,
1015
transformTags: {
11-
meta: (tagName, attribs) => {
12-
if (attribs.name) attribs.name = attribs.name.toLowerCase()
13-
return {tagName, attribs}
14-
},
15-
a: (tagName, attribs) => {
16-
if (attribs.href) attribs.href = attribs.href.toLowerCase()
17-
return {tagName, attribs}
18-
},
19-
link: (tagName, attribs) => {
20-
if (attribs.rel) attribs.rel = attribs.rel.toLowerCase()
21-
return {tagName, attribs}
22-
}
16+
meta: normalizeAttributes('name'),
17+
a: normalizeAttributes('href'),
18+
link: normalizeAttributes('rel')
2319
}
2420
})
2521

test/web/twitter-gif/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const readFile = promisify(fs.readFile)
1111

1212
const url = 'https://twitter.com/Kikobeats/status/880139124791029763'
1313

14-
it('twitter', async () => {
14+
it('twitter (tweet with gif)', async () => {
1515
const html = await readFile(resolve(__dirname, 'input.html'))
1616
const metadata = await getMetaData({html, url})
1717
snapshot(metadata)

test/web/twitter-image/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const readFile = promisify(fs.readFile)
1111

1212
const url = 'https://twitter.com/k4rliky/status/934482867480121345'
1313

14-
it('twitter', async () => {
14+
it('twitter (tweet with image)', async () => {
1515
const html = await readFile(resolve(__dirname, 'input.html'))
1616
const metadata = await getMetaData({html, url})
1717
snapshot(metadata)

0 commit comments

Comments
 (0)