Skip to content

Commit

Permalink
Merge pull request #81 from nashvillefcc/meetup_api_for_events#24
Browse files Browse the repository at this point in the history
Meetup api for events#24
  • Loading branch information
MoribundMedium authored Sep 3, 2021
2 parents a9faef9 + 3b9b9a0 commit 63abd5d
Show file tree
Hide file tree
Showing 7 changed files with 16,681 additions and 220 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ yarn-error.log
.pnp.js
# Yarn Integrity file
.yarn-integrity

# Local Netlify folder
.netlify
63 changes: 63 additions & 0 deletions netlify/functions/pingEvents/pingEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fetch = require('node-fetch');
var faunadb = require('faunadb'),
q = faunadb.query;
var client = new faunadb.Client({
secret: 'fnAEMbG7V8ACBCYzAGMmFrbunudXm4WMofc2WMMy',
});

var collection_reference = '301610187038392838'; // a reference to the specific collection that contains the cached events data

const handler = async function () {
try {
const cache_is_expired = await client
.query(q.Get(q.Collection('Events'))) // query the events collection to grab the timestamp of the last time it was changed
.then(function (response) {
const eight_hours = 28800000; // 8 hours is the expiration time
const today = Date.now();

let lastCachedDate = new Date(response.ts / 1000); // take the timestamp and convert it to milliseconds (from microseconds which is faunaDB default)

let now = new Date(today);
let difference = now - lastCachedDate; // take the difference between today and the last timestamp

return difference >= eight_hours; // return whether or not the timestamp is expired
});

const response = cache_is_expired // if the cache_is_expired is true, then request fresh data from the api, store it and then return it
? await fetch(
'https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fwww.meetup.com%2FfreeCodeCamp-Nashville%2Fevents%2Frss',
{
headers: { Accept: 'application/json' },
}
)
.then(response => response.json())
.then(data => {
return client.query(
q.Replace(q.Ref(q.Collection('Events'), collection_reference), {
// replace the expired data
data: { events: data },
}),
q.Get(q.Ref(q.Collection('Events'), collection_reference)) // return the fresh data
);
})
: await client.query(
q.Get(q.Ref(q.Collection('Events'), collection_reference)) // if it is not expired, just query the database
);

let stringifiedData = JSON.stringify(response.data.events);
return {
statusCode: 200,
body: stringifiedData,
};
} catch (error) {
// output to netlify function log
console.log(error);
return {
statusCode: 500,
// Could be a custom message or object i.e. JSON.stringify(err)
body: JSON.stringify({ msg: error.message }),
};
}
};

module.exports = { handler };
Loading

0 comments on commit 63abd5d

Please sign in to comment.