Skip to content

Commit

Permalink
Add new market functionality, add verbose option, fix snake_case AGAIN
Browse files Browse the repository at this point in the history
  • Loading branch information
kaimoe committed Apr 1, 2019
1 parent 8861e68 commit 21e6ecc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 90 deletions.
16 changes: 13 additions & 3 deletions XIVAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class XIVAPI {
/*{
private_key string undefined optional
language string 'en' optional
staging bool false optional
snake_case bool false optional
staging bool false optional
verbose bool false optional
}
*/
constructor(options = {}, legacyOptions = {}) {
Expand All @@ -30,9 +31,18 @@ See how in https://github.com/xivapi/xivapi-js/releases/tag/v0.1.3.\n\

this.endpoint = `https://${options.staging ? 'staging.' : ''}xivapi.com`
if(options.language && !resources.languages.includes(options.language))
throw Error(`Invalid language given, must be: ${this.resources.languages}`)
throw Error(`Invalid language given, must be one of: ${this.resources.languages}`)

this.globalParams = {}

for (let x of ['private_key', 'language']) {
if(typeof options[x] !== 'undefined')
this.globalParams[x] = options[x]
}
if(options.snake_case)
this.globalParams.snake_case = 1

this.globalParams = options
this.verbose = options.verbose

this.resources = resources

Expand Down
2 changes: 1 addition & 1 deletion lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Content extends Lib {
reject(Error('The id must be defined for get() in Content.'))

this.req(`/${name}/${id}`).then((res) => {
resolve(cleanContent(res))
resolve(cleanContent(res, this.parent.globalParams.snake_case))
}).catch((err) => {
reject(err)
})
Expand Down
106 changes: 30 additions & 76 deletions lib/market.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://xivapi.com/docs/Welcome#section-4
const Lib = require('./Lib')
let { firstCapital, cleanContent, correctCase, getCurrCase } = require('../utils')
let { cleanContent, getCurrCase, makeCSV } = require('../utils')

class Content extends Lib {
constructor(parent) {
Expand All @@ -9,102 +9,48 @@ class Content extends Lib {
cleanContent = cleanContent.bind(parent)
}

prices(id, server, params = {}) {
get(ids, params = {}) {
return new Promise((resolve, reject) => {
if(typeof(id) === 'undefined')
reject(Error('The id must be defined for market prices.'))
if(typeof(server) === 'undefined')
reject(Error('The server must be defined for market prices.'))
if(!this.parent.resources.servers.includes(firstCapital(server)))
reject(Error('The server is not valid for market prices.'))
if(typeof(ids) === 'undefined')
reject(Error('The ids must be defined for market get.'))
if(!params.servers && !params.dc)
reject(Error('The servers or dc params must be defined for market get.'))

let currCase = getCurrCase(this.parent.globalParams, params)

this.req(
`/market/${server}/items/${id}`,
params
).then((res) => {
let item = correctCase('item', currCase),
prices = correctCase('prices', currCase),
town = correctCase('town', currCase)

res[item] = cleanContent(res[item])
for (let i = 0; i < res[prices].length; i++) {
res[prices][i][town] = cleanContent(res[prices][i][town])
}

resolve(res)
}).catch((err) => {
reject(err)
})
})
}
let path = '/market/'
//currCase = getCurrCase(this.parent.globalParams, params)

history(id, server, params = {}) {
return new Promise((resolve, reject) => {
if(typeof(id) === 'undefined')
reject(Error('The id must be defined for market history.'))
if(typeof(server) === 'undefined')
reject(Error('The server must be defined for market history.'))
if(!this.parent.resources.servers.includes(firstCapital(server)))
reject(Error('The server is not valid for market history.'))
if(!getSingle(ids)) {//multiple IDs
path += 'items'
params.ids = makeCSV(ids)
params.servers = makeCSV(params.servers)

let currCase = getCurrCase(this.parent.globalParams, params)
} else if(params.dc || !getSingle(params.servers)) {//single ID, multiple servers
path += `item/${getSingle(ids)}`
params.servers = makeCSV(params.servers)

this.req(
`/market/${server}/items/${id}/history`,
params
).then((res) => {
let item = correctCase('item', currCase),
history = correctCase('history', currCase),
purchase_date = correctCase('purchase_date', currCase)
} else {//single ID & server
let server = getSingle(params.servers)
path += `${server}/item/${getSingle(ids)}`

res[item] = cleanContent(res[item])
for (let i = 0; i < res[history].length; i++) {
res[history][i][purchase_date] = new Date(res[history][i][purchase_date] * 1000)
}
}

this.req(path, params).then((res) => {
resolve(res)
}).catch((err) => {
reject(err)
})
})
}

stock(category, server, params = {}) {
categories(params = {}) {
return new Promise((resolve, reject) => {
if(typeof(category) === 'undefined')
reject(Error('The category must be defined for market stock.'))
if(typeof(server) === 'undefined')
reject(Error('The server must be defined for market stock.'))
if(!this.parent.resources.servers.includes(firstCapital(server)))
reject(Error('The server is not valid for market stock.'))

let currCase = getCurrCase(this.parent.globalParams, params)

this.req(
`/market/${server}/category/${category}`,
params
).then((res) => {
let item = correctCase('item', currCase)
for (let i = 0; i < res.length; i++) {
res[i][item] = cleanContent(res[i][item])
}

resolve(res)
}).catch((err) => {
reject(err)
})
})
}

categories(params = {}) {
return new Promise((resolve, reject) => {
this.req(
'/market/categories',
params
).then((res) => {
res = cleanContent(res)
res = cleanContent(res, currCase)

resolve(res)
}).catch((err) => {
Expand All @@ -117,3 +63,11 @@ class Content extends Lib {
}

module.exports = Content

const getSingle = (x) => {
if(typeof x === 'number' || (typeof x === 'string' && !x.includes(',')))
return x
if(x.length === 1)
return x[0]
return false
}
4 changes: 2 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(input, params = {}) {
Object.assign(params, {string: input})
).then((res) => {
res[correctCase('results', currCase)].forEach((entry) => { //eslint-disable-line no-unused-vars
entry = cleanContent(entry)
entry = cleanContent(entry, currCase)
})
resolve(res)
}).catch((err) => {
Expand All @@ -49,7 +49,7 @@ module.exports = function(input, params = {}) {
input
).then((res) => {
res[correctCase('results', currCase)].forEach((entry) => { //eslint-disable-line no-unused-vars
entry = cleanContent(entry)
entry = cleanContent(entry, currCase)
})
resolve(res)
}).catch((err) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "xivapi-js",
"version": "0.1.3",
"description": "A pure JS wrapper for xivapi.com",
"version": "0.2.0",
"description": "A Node.JS wrapper for xivapi.com",
"main": "XIVAPI.js",
"directories": {
"lib": "lib"
Expand Down
5 changes: 4 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const XIVAPI = require('./XIVAPI'),
readline = require('readline')

const xiv = new XIVAPI({snake_case: true})
const xiv = new XIVAPI({
//snake_case: true,
verbose: true
})

let rl = readline.createInterface({
input: process.stdin,
Expand Down
18 changes: 13 additions & 5 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ const request = require('request-promise-native'),
module.exports = {
//standard request function
req(path, params) {
if(typeof params.snake_case !== 'undefined')
if(params && typeof params.snake_case !== 'undefined')
params.snake_case = params.snake_case ? 1 : 0

params = Object.assign({}, this.globalParams, params)

if(this.verbose)
console.log(`Requesting ${path} with params: `, params)

return request({
uri: this.endpoint + path,
qs: Object.assign(this.globalParams, params),
qs: params,
json: true
})
},

//JSON request function
reqJSON(path, body) {
if(this.verbose)
console.log(`Requesting ${path} with body: `, body)

return request({
method: 'POST',
uri: this.endpoint + path,
Expand Down Expand Up @@ -64,9 +72,9 @@ module.exports = {
},

//transform URLs properly
cleanContent(input, deep) {
let icon = module.exports.correctCase('icon', this.globalParams.snake_case),
url = module.exports.correctCase('url', this.globalParams.snake_case)
cleanContent(input, snake_case, deep) {
let icon = module.exports.correctCase('icon', snake_case),
url = module.exports.correctCase('url', snake_case)

const properties = [icon, url]

Expand Down

0 comments on commit 21e6ecc

Please sign in to comment.