Skip to content

Instantly share code, notes, and snippets.

@bytesizedpcs
Created July 12, 2017 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytesizedpcs/8c19c827ebaf3850b8a926c9ac54e452 to your computer and use it in GitHub Desktop.
Save bytesizedpcs/8c19c827ebaf3850b8a926c9ac54e452 to your computer and use it in GitHub Desktop.
var request = require('request')
var bunyan = require('bunyan')
var fs = require('fs')
var log = bunyan.createLogger({
name: "strange-supreme"
})
var options = {
url: `http://www.supremenewyork.com/shop/{id}.json`,
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
}
}
function findCategory(category, jsonObj, itemToSearchFor) {
var key
if (jsonObj.products_and_categories.hasOwnProperty(category)) {
for (item in jsonObj.products_and_categories.new) {
var itemInScope = jsonObj.products_and_categories.new[item].name
if (itemInScope.includes(itemToSearchFor)) {
var idForItem = jsonObj.products_and_categories.new[item].id
return idForItem
} else {
return null
}
}
}
}
// ./store.json is just a test file for
// supreme's json file for their store
fs.readFile('./store.json', 'utf8', function (err, data) {
if (err) {
throw err
}
var jsonObj = JSON.parse(data)
var category = 'new'
var itemToSearchFor = "Half Zip"
var id = findCategory(category, jsonObj, itemToSearchFor)
// request(options, searchForSize)
})
// ./styles.json is just a test file for
// supreme's json file for styles for items
fs.readFile('./styles.json', 'utf8', function (err, data) {
if (err) {
throw err
}
var jsonObj = JSON.parse(data)
var color = 'Blue Plaid'
var size = '30'
// will need a id variable for request to server
var sizeId = searchForSizeWithFile(color, size, jsonObj)
if (sizeId == null) {
console.log('')
}
console.log('sizeId is: ' + sizeId)
})
function searchForSizeWithFile(color, size, jsonObj) {
// needs to return
// size, style, qty
// in format
// size='size'&style='style'&qty='qty'
var key
for (i in jsonObj['styles']) {
if (jsonObj['styles'][i]['name'].includes(color)) {
for (j in jsonObj['styles'][i]['sizes']) {
var itemInScope = jsonObj['styles'][i]['sizes'][j]['id']
var itemInScopeSize = jsonObj['styles'][i]['sizes'][j]['name']
var itemInScopeStock = jsonObj['styles'][i]['sizes'][j]['stock_level']
if (itemInScopeSize.includes(size) && itemInScopeStock > 0) {
return itemInScope
} else if (itemInScopeSize.includes(size) && itemInScopeStock <= 0){
return 'Item not in stock'
} else if (!itemInScopeSize.includes(size)) {
return 'Size not found'
} else {
return null
}
}
} else {
return 'Color not found'
}
}
}
function searchForSize(error, response, body, id) {
if (error) {
throw error
} else if (!error && response.statusCode == 200) {
var jsonInfo = JSON.parse(body)
console.log(jsonInfo)
} else if (response.statusCode != 200) {
console.log("Something went wrong: CODE ", response.statusCode)
}
}
// function to call on actual supremenewyork
// commented out because season is over
// function callback(error, response, body) {
// if(!error && response.statusCode == 200) {
// var jsonInfo = JSON.parse(body)
// log.info(response.statusCode)
// console.log(jsonInfo.products_and_categories)
// }
// }
// request(options, callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment