Skip to content

Instantly share code, notes, and snippets.

View circa10a's full-sized avatar

Caleb Lemoine circa10a

View GitHub Profile
@circa10a
circa10a / request_futures.py
Last active August 20, 2018 17:34
request_futures sample usage
from requests_futures.sessions import FuturesSession
import os
urls = []
num_workers = os.cpu_count()
session = FuturesSession(max_workers=num_workers)
while urls:
futures = []
for i in range(num_workers):
if not urls:
break
@circa10a
circa10a / joi-usage.js
Last active September 27, 2019 01:02
example usage of joi object schema validation
const Joi = require('joi');
const schema = Joi.object().keys({
birthyear: Joi.number().integer().min(1900).max(2013),
test: Joi.string(),
}).without('birthyear', 'test');
let result = Joi.validate({ birthyear: 2013 }, schema);
// No error
result.error ? console.log(result.error) : console.log('schema valid\n');
@circa10a
circa10a / tinydb.py
Last active February 18, 2019 20:41
tinydb python usage
from tinydb import TinyDB, Query
db = TinyDB('./test.db')
query = Query()
usersObj = [
{
'name':'jim',
'age': 24
},
{
@circa10a
circa10a / minimizeJs.sh
Last active July 3, 2018 19:45
One line to safely minimize js files using sed(there will be single spaces left to not break variable assignment)
# Requires appropriate semi-colons
FILE_PATH=$(pwd); for i in $(ls $FILE_PATH/*.js); do sed -i 's|//.*||g; /\/\*/,/*\//d; /^\s*$/d;' $i && sed -i ':a;N;$!ba;s/\n/ /g; s/ //g' $i; done
# Before
# const os = require('os');
# function num(num){
# return 5 + num
# }
# // Sample Comment
@circa10a
circa10a / matchObjects.js
Last active June 30, 2018 21:19
Filter an array of objects with key/value pairs
const _ = require('underscore');
arr = [
{
car: "toyota",
color: "blue",
year: 2010,
trans: "auto",
warrantyEnd: "2013"
},
{
@circa10a
circa10a / reverse-array.js
Last active March 25, 2019 02:17
javascript reverse array (no helper methods)
const arr = [1, 2, 3, 4, 5];
const newArr = [];
for(i = arr.length; i >=arr[0]; i--) {
newArr.push(i);
}
console.log(newArr); // [ 5, 4, 3, 2, 1 ]
/* with helper method (recommended)
@circa10a
circa10a / easy-soap-request-test.js
Last active January 24, 2020 11:41
medium mocha and chai test example
const fs = require('fs');
const { expect } = require('chai');
const soapRequest = require('../index');
const url = 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php';
const urlFail = 'https://graphical.weather.gov:80/xml/SOAP_server/ndfdXMLserver.php';
const headers = {
'user-agent': 'easy-soap-request-test',
'Content-Type': 'text/xml;charset=UTF-8',
SOAPAction: 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode',
const soapRequest = require('easy-soap-request');
const fs = require('fs');
// example data
const url = 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php';
const sampleHeaders = {
'user-agent': 'sampleTest',
'Content-Type': 'text/xml;charset=UTF-8',
'soapAction': 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode',
};
@circa10a
circa10a / easy-soap-request-index.js
Last active May 24, 2021 23:02
easy-soap-request-index.js
const axios = require('axios-https-proxy-fix');
/**
* @author Caleb Lemoine
* @param {object} opts easy-soap-request options
* @param {string} opts.url endpoint URL
* @param {object} opts.headers HTTP headers, can be string or object
* @param {string} opts.xml SOAP envelope, can be read from file or passed as string
* @param {int} opts.timeout Milliseconds before timing out request
* @param {object} opts.proxy Object with proxy configuration
@circa10a
circa10a / curl_noaa.sh
Last active April 2, 2018 00:19
medium curl command
curl -H "soapAction: \"https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListCityNames\"" -H "Content-Type: text/xml;charset=UTF-8" --data @test.xml https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php