Skip to content

Commit

Permalink
Native promises (#26)
Browse files Browse the repository at this point in the history
feat: implement native promises

BREAKING CHANGE: update Node version to v8.0.0
  • Loading branch information
tannerbaum authored and antosan committed Oct 11, 2018
1 parent 9f15648 commit 44ba27d
Show file tree
Hide file tree
Showing 7 changed files with 3,697 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"

script:
- npm test
77 changes: 40 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Specify basic configuration:

```javascript
config = {
auth: {
user: 'username',
pass: 'password'
},
// example using baikal as CalDAV server
uri: 'http://example.com/cal.php/calendars/<user name>/<calendar name>'
}
auth: {
user: "username",
pass: "password"
},
// example using baikal as CalDAV server
uri: "http://example.com/cal.php/calendars/<user name>/<calendar name>"
};
```

The request will timeout if it gets no reponse from the CalDav server after 10 seconds.
Expand All @@ -24,24 +24,23 @@ An optional `timeout` parameter can be provided to override this default by pass
```javascript
config = {
auth: {
user: 'username',
pass: 'password'
user: "username",
pass: "password"
},
// example using baikal as CalDAV server
uri: 'http://example.com/cal.php/calendars/<user name>/<calendar name>',
uri: "http://example.com/cal.php/calendars/<user name>/<calendar name>",
timeout: 20000
}
};
```

API
---
## API

### scrapegoat.getCtag()

Fetches the ctag of a calendar. You can use the calendar's ctag to see if anything in the calendar has changed.

```javascript
const Scrapegoat = require('scrapegoat');
const Scrapegoat = require("scrapegoat");

const scrapegoat = new Scrapegoat(config);

Expand All @@ -60,7 +59,7 @@ You'll get an object, which looks like this:

### scrapegoat.getEtags()

Fetches the etags of a all events. You can use the events etags to see if an event has changed.
Fetches the etags of all events. You can use the events etags to see if an event has changed.

```javascript
scrapegoat.getEtags().then(console.log);
Expand All @@ -70,15 +69,15 @@ You'll get an array of objects, which looks like this:

```javascript
[
{
ics: '/cal.php/calendars/test/holidays/6151613161614616.ics',
etag: 'fc46dd304e83f572688c68ab63816c8f'
},
{
ics: '/cal.php/calendars/test/holidays/6816189165131651.ics',
etag: '8d59671ba294af1de0e0b154a8ea64c2'
}
]
{
ics: "/cal.php/calendars/test/holidays/6151613161614616.ics",
etag: "fc46dd304e83f572688c68ab63816c8f"
},
{
ics: "/cal.php/calendars/test/holidays/6816189165131651.ics",
etag: "8d59671ba294af1de0e0b154a8ea64c2"
}
];
```

### scrapegoat.getEvents(events)
Expand All @@ -99,15 +98,15 @@ Output should be something like this:
```javascript
[
{
ics: '/cal.php/calendars/test/holidays/1234564316516.ics',
etag: 'fc46dd304e83f572688c68ab63816c8f',
ics: "/cal.php/calendars/test/holidays/1234564316516.ics",
etag: "fc46dd304e83f572688c68ab63816c8f",
data: {
title: 'Holiday: John Doe',
uid: '56ea42c0-e4af-4ac8-8d60-d95996c9ddc5',
location: 'Kissing, Augsburg, Germany',
title: "Holiday: John Doe",
uid: "56ea42c0-e4af-4ac8-8d60-d95996c9ddc5",
location: "Kissing, Augsburg, Germany",
description: null,
start: '2017-02-16T00:00:00.000Z',
end: '2017-02-18T00:00:00.000Z',
start: "2017-02-16T00:00:00.000Z",
end: "2017-02-18T00:00:00.000Z",
duration: {
weeks: 0,
days: 2,
Expand All @@ -117,10 +116,10 @@ Output should be something like this:
isNegative: false
},
type: { recurring: false, edited: false },
createdAt: '2017-01-24T15:33:04.000Z'
createdAt: "2017-01-24T15:33:04.000Z"
}
}
]
];
```

### scrapegoat.getAllEvents()
Expand All @@ -137,18 +136,22 @@ The end-date must be larger that the start-date.
Example using [moment.js](http://momentjs.com/) for date formatting:

```javascript
const moment = require('moment');
const moment = require("moment");

const start = moment().startOf('month').format('YYYYMMDD[T]HHmmss[Z]');
const end = moment().endOf('month').format('YYYYMMDD[T]HHmmss[Z]');
const start = moment()
.startOf("month")
.format("YYYYMMDD[T]HHmmss[Z]");
const end = moment()
.endOf("month")
.format("YYYYMMDD[T]HHmmss[Z]");

scrapegoat.getEventsByTime(start, end).then(console.log);
```

The example below gets all events happening on a single day

```javascript
const moment = require('moment');
const moment = require("moment");

const start = moment("20170216T0000").format("YYYYMMDD[T]HHmmss[Z]");
const end = moment("20170216T2300").format("YYYYMMDD[T]HHmmss[Z]");
Expand Down
44 changes: 27 additions & 17 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";

const { promisify } = require("util");
const doRequest = require("request");
const nodefn = require("when/node");
const _ = require("lodash");
const doRequestAsync = promisify(doRequest);
const version = require("../package.json").version;

/**
Expand All @@ -14,36 +14,46 @@ const version = require("../package.json").version;
* @returns {Promise}
*/
function request(baseConfig, method, depth, xml) {
const config = _.assign({}, baseConfig);

config.headers = _.assign({}, baseConfig.headers);
const config = {
...baseConfig,
headers: {
...baseConfig.headers,
"Content-length": xml.length,
Depth: depth
},
body: xml,
method
};

if (!baseConfig.headers || "User-Agent" in baseConfig.headers === false) {
config.headers["User-Agent"] = "scrapegoat/" + version;
}

if (baseConfig.auth) {
config.auth = _.assign({}, baseConfig.auth);
config.auth.sendImmediately = "sendImmediately" in baseConfig.auth ? baseConfig.auth.sendImmediately : false;
config.auth = {
sendImmediately: false,
...baseConfig.auth
};
}

config.body = xml;
config.method = method;
config.headers["Content-length"] = xml.length;
config.headers.Depth = depth;
config.timeout = baseConfig.timeout ? baseConfig.timeout : 10000;

return nodefn.call(doRequest, config)
.spread((res, body) => {
let err;
return doRequestAsync(config)
.then(({ statusCode, body }) => {
if (statusCode >= 300) {
const err = new Error(
"Response with status code: " + statusCode
);

err.statusCode = statusCode;

if (res.statusCode >= 300) {
err = new Error("Response with status code: " + res.statusCode);
err.statusCode = res.statusCode;
throw err;
}

return body;
})
.catch(err => {
throw err;
});
}

Expand Down
11 changes: 6 additions & 5 deletions lib/xml/parser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"use strict";

const parseXMLString = require("xml2js").parseString;
const nodefn = require("when/node");
const { promisify } = require("util");
const { parseString } = require("xml2js");
const parseXMLString = promisify(parseString);
const ICAL = require("ical.js");
const moment = require("moment");

// parse calendar object
function parseCalendarMultistatus(xml) {
return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
const parsed = {};

if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
Expand All @@ -27,7 +28,7 @@ function parseEventsMultistatus(xml) {
let parsed;
const formatted = [];

return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
return formatted;
}
Expand Down Expand Up @@ -134,7 +135,7 @@ function parseEvents(xml) {
let parsed;
const formatted = [];

return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
return formatted;
}
Expand Down
Loading

0 comments on commit 44ba27d

Please sign in to comment.