Skip to content

Commit

Permalink
Initial commit of Strava components
Browse files Browse the repository at this point in the history
  • Loading branch information
dylburger committed Oct 4, 2020
1 parent e34511a commit e1e3ead
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
35 changes: 35 additions & 0 deletions components/strava/activity-created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const strava = require("https://github.com/PipedreamHQ/pipedream/components/strava/strava.app.js");

module.exports = {
name: "Activity Created",
description: "Emits an event when a new activity is created",
version: "0.0.1",
props: {
strava,
stravaApphook: {
type: "$.interface.apphook",
appProp: "strava",
eventNames: ["activity.create"],
},
},
async run(event) {
console.log(event);
let details;
try {
details = await this.strava.getActivity(event.object_id);
} catch (err) {
console.log(`Error fetching activity details: ${err}`);
}
let summary = "Activity created";
if (details && details.name) {
summary += `: ${details.name}`;
}
this.$emit(
{ event, details },
{
summary,
ts: event.event_time * 1000,
}
);
},
};
25 changes: 25 additions & 0 deletions components/strava/activity-deleted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const strava = require("https://github.com/PipedreamHQ/pipedream/components/strava/strava.app.js");

module.exports = {
name: "Activity Deleted",
description: "Emits an event when an activity is deleted",
version: "0.0.1",
props: {
strava,
stravaApphook: {
type: "$.interface.apphook",
appProp: "strava",
eventNames: ["activity.delete"],
},
},
async run(event) {
console.log(event);
this.$emit(
{ event },
{
summary: `Activity deleted: ${event.object_id}`,
ts: event.event_time * 1000,
}
);
},
};
35 changes: 35 additions & 0 deletions components/strava/activity-updated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const strava = require("https://github.com/PipedreamHQ/pipedream/components/strava/strava.app.js");

module.exports = {
name: "Activity Updated",
description: "Emits an event when an activity is updated",
version: "0.0.1",
props: {
strava,
stravaApphook: {
type: "$.interface.apphook",
appProp: "strava",
eventNames: ["activity.update"],
},
},
async run(event) {
console.log(event);
let details;
try {
details = await this.strava.getActivity(event.object_id);
} catch (err) {
console.log(`Error fetching activity details: ${err}`);
}
let summary = "Activity updated";
if (details && details.name) {
summary += `: ${details.name}`;
}
this.$emit(
{ event, details },
{
summary,
ts: event.event_time * 1000,
}
);
},
};
47 changes: 47 additions & 0 deletions components/strava/custom-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const strava = require("https://github.com/PipedreamHQ/pipedream/components/strava/strava.app.js");

module.exports = {
name: "Strava Custom Events",
description:
"Emits an event when an activity is created, updated, or deleted",
version: "0.0.1",
props: {
strava,
eventNameOptions: {
label: "Strava Events",
type: "string[]",
async options() {
return ["activity.create", "activity.update", "activity.delete"];
},
},
stravaApphook: {
type: "$.interface.apphook",
appProp: "strava",
async eventNames() {
return this.eventNameOptions;
},
},
},
async run(event) {
console.log(event);
const { object_type, object_id, event_time } = event;
const ts = event_time * 1000;

if (object_type === "activity") {
let details;
// Optimistically fetch activity details. When an event is deleted, this will fail
try {
details = await this.strava.getActivity(object_id);
} catch (err) {
console.log(`Error fetching activity details: ${err}`);
}
this.$emit(
{ event, details },
{
summary: `Activity ${event.aspect_type}d`,
ts,
}
);
}
},
};
35 changes: 35 additions & 0 deletions components/strava/strava.app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Strava API app file
const axios = require("axios");

module.exports = {
type: "app",
app: "strava",
methods: {
async _makeAPIRequest(opts) {
if (!opts.headers) opts.headers = {};
opts.headers["Authorization"] = `Bearer ${this.$auth.oauth_access_token}`;
opts.headers["Content-Type"] = "application/json";
opts.headers["user-agent"] = "@PipedreamHQ/pipedream v0.1";
const { path } = opts;
delete opts.path;
opts.url = `https://www.strava.com/api/v3${
path[0] === "/" ? "" : "/"
}${path}`;
return await axios(opts);
},
async getActivity(id) {
return (
await this._makeAPIRequest({
path: `/activities/${id}`,
})
).data;
},
async getAuthenticatedAthlete() {
return (
await this._makeAPIRequest({
path: "/athlete",
})
).data;
},
},
};

0 comments on commit e1e3ead

Please sign in to comment.