Skip to content

Commit

Permalink
fix: prettier format 🍅
Browse files Browse the repository at this point in the history
  • Loading branch information
kosssi committed Mar 16, 2018
1 parent 61a309c commit 93d6334
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"extends": [
"cozy-konnector"
]
"extends": ["cozy-konnector"]
}
25 changes: 16 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const { BaseKonnector, requestFactory, saveFiles, addData } = require('cozy-konnector-libs')
const {
BaseKonnector,
requestFactory,
saveFiles,
addData
} = require('cozy-konnector-libs')
const request = requestFactory({ cheerio: true })

const baseUrl = 'http://books.toscrape.com'
Expand All @@ -8,21 +13,23 @@ module.exports = new BaseKonnector(start)
// The start function is run by the BaseKonnector instance only when it got all the account
// information (fields). When you run this connector yourself in "standalone" mode or "dev" mode,
// the account information come from ./konnector-dev-config.json file
function start (fields) {
function start(fields) {
// The BaseKonnector instance expects a Promise as return of the function
return request(`${baseUrl}/index.html`)
.then($ => {
return request(`${baseUrl}/index.html`).then($ => {
// cheerio (https://cheerio.js.org/) uses the same api as jQuery (http://jquery.com/)
// here I do an Array.from to convert the cheerio fake array to a real js array.
const entries = Array.from($('article')).map(article => parseArticle($, article))
return addData(entries, 'com.toscrape.books')
.then(() => saveFiles(entries, fields))
const entries = Array.from($('article')).map(article =>
parseArticle($, article)
)
return addData(entries, 'com.toscrape.books').then(() =>
saveFiles(entries, fields)
)
})
}

// The goal of this function is to parse a html page wrapped by a cheerio instance // and return an array of js objects which will be saved to the cozy by addData (https://github.com/cozy/cozy-konnector-libs/blob/master/docs/api.md#module_addData)
// and saveFiles (https://github.com/cozy/cozy-konnector-libs/blob/master/docs/api.md#savefiles)
function parseArticle ($, article) {
function parseArticle($, article) {
const $article = $(article)
const title = $article.find('h3 a').attr('title')
return {
Expand All @@ -37,6 +44,6 @@ function parseArticle ($, article) {
}

// convert a price string to a float
function normalizePrice (price) {
function normalizePrice(price) {
return parseFloat(price.trim().replace('£', ''))
}

0 comments on commit 93d6334

Please sign in to comment.