Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Commit

Permalink
Fix #154: Parse Open Graph price string to a number
Browse files Browse the repository at this point in the history
This PR depends on PR#143, so merge that one first.

This is a bit of a moot point for the MVP, since #70 restricts extraction to our five supported sites, none of which make use of Open Graph meta tags, however, that may change, and the fix was quick.

Note: To test this, comment out the other extraction methods in './src/extraction/index.js' and the allowlist check in that file's 'main' function, then visit a Crate and Barrel product page.
  • Loading branch information
biancadanforth committed Oct 9, 2018
1 parent f5309a3 commit b058f0e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/extraction/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@
* Product extraction via Open Graph tags.
*/

import {parsePrice} from 'commerce/extraction/utils';

const OPEN_GRAPH_PROPERTY_VALUES = {
title: 'og:title',
image: 'og:image',
price: 'og:price:amount',
};

/** How product information is extracted depends on the feature */
const FEATURE_DEFAULTS = {
getValueFromElement(element) {
return element.getAttribute('content');
},
};
const PRODUCT_FEATURES = {
image: {
...FEATURE_DEFAULTS,
},
title: {
...FEATURE_DEFAULTS,
},
price: {
...FEATURE_DEFAULTS,
getValueFromElement(element) {
return parsePrice([element.getAttribute('content')]);
},
},
};

/**
* Returns any product information available on the page from Open Graph <meta>
* tags.
Expand All @@ -26,7 +49,7 @@ export default function extractProduct() {
return null;
}

extractedProduct[feature] = metaEle.getAttribute('content');
extractedProduct[feature] = PRODUCT_FEATURES[feature].getValueFromElement(metaEle);
}

return extractedProduct;
Expand Down

0 comments on commit b058f0e

Please sign in to comment.