Skip to content

Commit

Permalink
Refactor context handler usage
Browse files Browse the repository at this point in the history
To make this more scalable, as we add more handlers for retrieving
context, we can use a `getHandler` method that can provide the relevant
handler.

This then allows us to pass around that handler, not worrying what it
is, just that it's doing its job.
  • Loading branch information
jamietanna committed Nov 16, 2021
1 parent 4eac7de commit ca4aae9
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/events/fetch-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ const granary = require('./granary')
const meetup = require('./meetup')
const openGraph = require('./open-graph')

async function getContext (url) {
// for specific sites, use custom parsing
async function getHandler (url) {
if (meetup.isMeetupUrl(url)) {
const properties = await meetup.fetchContext(url)
if (properties) {
return properties
}
return meetup
} else if (eventbrite.isEventbriteUrl(url)) {
const properties = await eventbrite.fetchContext(url)
if (properties) {
return properties
}
return eventbrite
} else {
return granary
}
// otherwise fallback to Granary, and then OpenGraph
const properties = await granary.fetchContext(url)
}

async function getContext (handler, url) {
// if our fetching fails, fallback to OpenGraph
const properties = await handler.fetchContext(url)
if (properties) {
return properties
}
Expand All @@ -30,7 +28,8 @@ async function getContext (url) {
exports.handler = async function subscribe (event) {
const data = await arc.tables()
const { url } = JSON.parse(event.Records[0].Sns.Message)
const properties = await getContext(url)
const handler = await getHandler(url)
const properties = await getContext(handler, url)
await data.contexts.put({
url,
properties
Expand Down

0 comments on commit ca4aae9

Please sign in to comment.