UNPKG

strava-api-handler

Version:
119 lines (103 loc) 2.29 kB
'use strict'; var fitnessModels = require('fitness-models'); var luxon = require('luxon'); var mathjs = require('mathjs'); class Activity extends fitnessModels.Workout { constructor(options) { super(options); this.typeId = options.typeId; this.id = options.id; this.source = options.source; this.description = options.description; this.gearId = options.gearId; } static getFromApi(activity) { const { commute, gear_id, distance, start_date, timezone, id, elapsed_time, type, calories, name } = activity; return new Activity({ start: luxon.DateTime.fromISO(start_date, { zone: timezone.split(' ')[1] }), id, duration: luxon.Duration.fromObject({ seconds: elapsed_time }), typeId: type, calories, distance: distance != null ? mathjs.unit(distance, 'm') : undefined, title: name, source: activity, isCommute: commute, gearId: gear_id }); } clone(extension) { return new Activity({ ...this.toObject(), ...extension }); } toObject() { return { ...super.toObject(), typeId: this.typeId, id: this.id, source: this.source, description: this.description, gearId: this.gearId }; } getId() { return this.id; } getTypeName() { return this.typeId; } setId(id) { return this.clone({ id }); } getSource() { return this.source; } getDescription() { return this.description; } getGearId() { return this.gearId; } setGearId(gearId) { return this.clone({ gearId }); } toApiObject() { const distance = this.getDistance(); return { type: this.getTypeId(), start_date_local: this.getStart().toISO(), elapsed_time: this.getDuration().as('seconds'), commute: this.isCommute ? 1 : 0, gear_id: this.getGearId() || 'none', ...(this.getTitle() ? { name: this.getTitle() } : {}), ...(distance != null ? { distance: distance.toNumber('m') } : {}), ...(this.getDescription() != null ? { description: this.getDescription() } : {}) }; } } module.exports = Activity;