Skip to content

Instantly share code, notes, and snippets.

@eatoncw
eatoncw / batch.ts
Created August 18, 2020 20:17
batch async
// takes a large array of items, chunks them, performs an async task on that item
import { chunk } from "lodash";
function wait(time: number) { // in ms
return new Promise(resolve => setTimeout(resolve, time));
}
export async function batch(
arr: any,
@eatoncw
eatoncw / product_filters_data_helper.rb
Created March 7, 2018 23:07
eg rails helper using request params and data set to create radio buttons for product filtering
def ProductFiltersDataHelper.furniture_types
[
{name: "all", types: "all"},{name: "sofas and loveseats", types: "!table sofa couch sectional loveseat settee"},
{name: "wardrobes", types: "wardrobe armoire chifforobe"},{name: "buffets", types: "buffet sideboard credenza"},
{name: "chairs and stools", types: "chair armchair recliner stool rocker"},{name: "tables", types: "table sidetable"},
{name: "beds", types: "!table bed"},{name: "desks", types: "desk"},
{name: "bookcases and shelves", types: "!desk bookcase shelves"},{name: "cabinets", types: "cabinet"},
{name: "mirrors", types: "mirror"}
]
end
@eatoncw
eatoncw / gist:279579ca5ec76b07b9e89d4221b5139c
Created March 7, 2018 20:26
dynamically generate params for GET request to update products page based on user-selected filters
//uses jquery to send request to rails 5 built api-only backend
$('.sales-filters input').on('click', function() {
var params = {};
var filters = $('.sales-filters input:checked'); //get array of all checked radio buttons
for (i = 0; i < filters.length; i++) {
params[filters[i].name] = filters[i].value //dynamically create params for GET request based on name and value attributes
}
var url = window.location.origin + '.....'
fetchAndPaginate(url,params)
@eatoncw
eatoncw / best_buy_sales_finder_worker.rb
Created March 7, 2018 20:15
Sample api request - find certain Best Buy products that are on sale and save to database
#utilize a background job - using sidekiq with redis queue
class ApisSalesFinders::BestbuySalesFinderWorker < ApisSalesFinders::SalesFinders
include Sidekiq::Worker
require './app/models/scrape_constants/collections' #ebay product categories
def perform
current_items = Product.where(retailer: "best buy")
discount = 10
minimum_price = 75 #used to filter out some accessory products
@eatoncw
eatoncw / gist:1e6616d8a85c96130ab7cee40fffb8cf
Last active February 16, 2018 03:09
Javascript find max value in array of objects with key as argument, using reduce
//find max value in array of objects with key as argument
function findMax(key) {
return function maxx(prev,curr) {
return Math.max(prev, curr[key]);
}
}
//then pass function to reduce with key as argument
//eg get max price from an array of objects
var maxPrice = array.reduce(findMax("price"),0);
@eatoncw
eatoncw / gist:2deb6a62e3056e5e53b0af8cd39a1c36
Last active June 7, 2019 01:44
PDF to JPG conversion using Rails 5, Paperclip. Improved resolution of output
#Accepts all image files and pdfs. If the upload is a pdf, it is converted to a jpg
#Paperclip incorporates ImageMagick. If a multipage page PDF, only the first page is converted
#Model
#here the original style needs to be given source_file_options. It's critical that the original file is not passed geometry
#or the resolution won't really improve
has_attached_file :avatar,
styles: lambda { |attachment|
styles = {}
@eatoncw
eatoncw / about.html
Last active February 23, 2016 22:40
My homepage
<!DOCTYPE html>
<html lang="en">
<head>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">