Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1651 from duckduckgo/zaahir/octopart-update
Browse files Browse the repository at this point in the history
Zaahir/octopart update
  • Loading branch information
moollaza committed Mar 20, 2015
2 parents 503f194 + 728e24b commit cfcef06
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lib/DDG/Spice/Octopart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ attribution github => ['https://github.com/bnewbold', 'bnewbold'];

triggers any => "datasheet", "specs", "octopart";

spice to => 'http://octopart.com/api/v2/parts/search?apikey={{ENV{DDG_SPICE_OCTOPART_APIKEY}}}&limit=12&optimize.hide_offers=1&optimize.hide_specs=1&q=$1&callback={{callback}}';
spice to => 'http://octopart.com/api/v3/parts/search?apikey={{ENV{DDG_SPICE_OCTOPART_APIKEY}}}&limit=12&q=$1&include[]=datasheets&include[]=avg_price_v2&include[]=market_status_v2&include[]=imagesets&include[]=short_description&callback={{callback}}';

handle remainder => sub {
return $_ if $_;
Expand Down
21 changes: 2 additions & 19 deletions share/spice/octopart/buy.handlebars
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
{{#if description}}
<div>Details: {{description}}</div>
{{/if}}
{{#if item.market_status}}
<div>Market Status: {{item.market_status}}</div>
{{/if}}
{{#if price}}
<div>Average Price: ${{price}}</div>
{{/if}}
<br>
{{#if datasheet}}
<a href="{{{datasheet}}}" class="btn btn__datasheet">Datasheet</a>
{{/if}}
{{#if url}}
<a href="{{{url}}}" class="btn btn--primary">
Buy at Octopart
</a>
{{/if}}


<a href="{{{datasheet}}}" class="btn btn__datasheet">Datasheet</a>
<a href="{{{item.octopart_url}}}" class="btn btn--primary">Authorized Distributors</a>
2 changes: 2 additions & 0 deletions share/spice/octopart/content.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{{#if market_status}}<p>Market Status: {{market_status}}</p>{{/if}}
4 changes: 0 additions & 4 deletions share/spice/octopart/octopart.handlebars

This file was deleted.

151 changes: 74 additions & 77 deletions share/spice/octopart/octopart.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,87 @@
function ddg_spice_octopart (api_result) {
(function (env) {
"use strict";

if(!api_result || !api_result.results || api_result.results.length === 0) {
return Spice.failed('octopart');
}
// iterate over each distributors
// to get matching large and medium size images
function get_images(imagesets) {
var images = {};

// var results = [],
// isRelevant,
// skip_words = [
// "datasheet",
// "specs",
// "octopart"
// ];
$.each(imagesets, function(){
if (!is_mobile && this.large_image && this.medium_image){
images.large = DDG.toHTTP(this.large_image.url);
images.medium = DDG.toHTTP(this.medium_image.url);
return false;

// for(var i = 0; i < api_result.results.length; i += 1) {
// var iter = api_result.results[i];
// fallback in case we never find a large image
// or on mobile device
} else if (this.medium_image){
images.medium = DDG.toHTTP(this.medium_image.url);
images.large = images.medium;

// // Checks both the MPN and the manufacturer if the query is somehow relevant.
// isRelevant = DDG.isRelevant(iter.item.mpn, skip_words, 4, true) ||
// DDG.isRelevant(iter.item.manufacturer.displayname, skip_words, 4, true);
// worst case
} else if (this.small_image){
images.medium = DDG.toHTTP(this.small_image.url);
images.large = images.medium;
}
});
return images;
}

// // Check if we have images.
// if(iter.item.images.length > 0 && isRelevant) {
// results.push(iter);
// // If an image doesn't exist, add a different image.
// } else if(isRelevant) {
// iter.item.images.push({
// url_90px: "http://n1.octostatic.com/o3web/detail/images/camera-icon.png"
// });
// results.push(iter);
// }
// }
env.ddg_spice_octopart = function (api_result) {

// if(results.length === 0) {
// return;
// }
if(!api_result || !api_result.results || api_result.results.length === 0) {
return Spice.failed('octopart');
}

var isMobile = $('.is-mobile').length;
Spice.add({
id: 'octopart',
name: 'Parts',
data: api_result.results,
signal: 'high',
meta: {
itemType: 'Parts',
sourceName: 'Octopart',
sourceUrl : 'http://octopart.com/search?q=' + api_result.request.q
},
normalize: function(item) {

Spice.add({
id: 'octopart',
name: 'Parts',
data: api_result.results,
signal: 'high',

meta: {
itemType: 'Parts',
sourceName: 'Octopart',
sourceUrl : 'http://octopart.com/partsearch#search/requestData&q=' + api_result.request.q
},
normalize: function(item) {
var img = DDG.getProperty(item, "item.images.0.url_90px");
var desc_length = 60;
var description = (isMobile ?
Handlebars.helpers.ellipsis(DDG.strip_html(item.item.short_description), desc_length) :
DDG.strip_html(item.item.short_description));
item = item.item; // our item object is wrapped in an "item" property

return {
brand: item.item.manufacturer.displayname,
price: item.item.avg_price[1] + ' ' + item.item.avg_price[0].toFixed(2),
img: img,
img_m: img,
url: item.item.detail_url,
title: DDG.strip_html(item.item.mpn).toUpperCase(),
heading: DDG.strip_html(item.item.mpn).toUpperCase(),
description: description,
datasheet: item.item.datasheets[0].url
};
},
var images = get_images(item.imagesets);

templates: {
group: 'products',
options: {
rating: false,
buy: Spice.octopart.buy
return {
brand: item.brand.name,
price: item.avg_price_v2[1] + ' $' + item.avg_price_v2[0].toFixed(2),
img: images.medium,
img_m: images.large,
url: item.octopart_url,
title: item.mpn,
heading: item.mpn,
abstract: item.short_description,
datasheet: item.datasheets[0].url,
market_status: item.market_status_v2.replace(/^\w+: /, '') // strip out "GOOD: " from market_status
};
},
variants: {
detail: 'light'
templates: {
group: 'products',
options: {
rating: false,
subtitle_content: Spice.octopart.content,
buy: Spice.octopart.buy
},
variants: {
detail: 'light'
}
},
relevancy: {
primary: [
{ required: 'item.short_description' },
{ required: 'item.octopart_url' },
{ required: 'item.datasheets' },
{ required: 'item.imagesets' },
{ required: 'item.avg_price_v2' },
],
}
}

});
};

// Handlebars.registerHelper("toFixed", function(number) {
// "use strict";

// return number.toFixed(2);
// });
});
};
})(this);

0 comments on commit cfcef06

Please sign in to comment.