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

Fix #154: Parse Open Graph price string to a number #155

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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