-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cc6433
commit d6e7bfb
Showing
6 changed files
with
178 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const EventEmitter = require("events"); | ||
|
||
module.exports = class Firehose extends EventEmitter { | ||
constructor(target, ignore = [], pollTime = 300000, libbarter){ | ||
super(); | ||
|
||
this._libbarter = libbarter; | ||
this._target = target; | ||
this._cache = ignore; | ||
|
||
this._interval = setInterval(() => this._poll(), pollTime); | ||
setTimeout(() => this._poll(), 500); | ||
} | ||
|
||
async _poll(){ | ||
let items = await this._libbarter._doRequest("https://barter.vg/firehose/" + this._target); | ||
|
||
let newCache = Object.keys(items); | ||
|
||
let result = []; | ||
|
||
newCache.forEach(i => { | ||
if( this._cache.indexOf(i) < 0 ) result.push(new this._libbarter._FirehoseItem(items[i], i, this._libbarter)); | ||
}); | ||
|
||
result.forEach(i => { | ||
this.emit("item", i); | ||
}); | ||
|
||
this.emit("items", newCache); | ||
|
||
this._cache = newCache; | ||
} | ||
|
||
async destroy(){ | ||
clearInterval(this._interval); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = class FirehoseItem { | ||
constructor(firehoseItem, lineItemID, libbarter){ | ||
this.user = new libbarter._LimitedUser(firehoseItem.user_id, firehoseItem.steam_persona, libbarter); | ||
this.title = firehoseItem.title; | ||
this.id = firehoseItem.item_id; | ||
this.lineItem = lineItemID; | ||
|
||
this._libbarter = libbarter; | ||
} | ||
|
||
async getFullItem(){ | ||
await this._libbarter.getItem(this.id, this._libbarter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,102 @@ | ||
const libbarter = require("../index.js"); | ||
|
||
const Barter = new libbarter(); | ||
|
||
test("getCollection() returns an array of valid CollectionItems", () => { | ||
return Barter.getCollection("3ede", "b").then(collection => { | ||
expect(collection.filter(i => i instanceof Barter._CollectionItem).length).toBe(collection.length); | ||
expect(collection[0].id).toBe(9555); | ||
}); | ||
}); | ||
|
||
test("getGlobalOfferCollector() returns a GlobalOfferCollector that emits FeedOffers (this may take a moment...)", () => { | ||
jest.setTimeout(120000); | ||
|
||
let collector = Barter.getGlobalOfferCollector(1000); | ||
|
||
expect(collector instanceof Barter._GlobalOfferCollector).toBe(true); | ||
|
||
return new Promise((resolve, reject) => { | ||
collector.once("offer", offer => { | ||
expect(offer instanceof Barter._FeedOffer).toBe(true); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
|
||
test("getItem() returns a valid Item", () => { | ||
return Barter.getItem(9555).then(item => { | ||
expect(item instanceof Barter._Item).toBe(true); | ||
expect(item.title).toBe("Trading Card"); | ||
}); | ||
}); | ||
|
||
test("getOffer() returns a valid Offer", () => { | ||
return Barter.getOffer(2247076).then(offer => { | ||
expect(offer instanceof Barter._Offer).toBe(true); | ||
expect(offer.opened).toBe(false); | ||
expect(offer.isMultiUser).toBe(false); | ||
expect(offer.to.id).toBe("3ede"); | ||
}); | ||
}); | ||
|
||
test("getOffers() returns an object of valid LimitedOffers", () => { | ||
return Barter.getOffers("3ede").then(offers => { | ||
expect(Object.keys(offers).filter(i => offers[i] instanceof Barter._LimitedOffer).length).toBe(Object.keys(offers).length); | ||
expect(offers[2247076].isFailed).toBe(false); | ||
}); | ||
}); | ||
|
||
test("getUser() returns a valid User", () => { | ||
return Barter.getUser("3ede").then(user => { | ||
expect(user instanceof Barter._User).toBe(true); | ||
expect(user.steamid).toBe("76561198852754414"); | ||
}); | ||
}); | ||
|
||
test("getUserBySteamID() returns a valid User", () => { | ||
return Barter.getUserBySteamID("76561198852754414").then(user => { | ||
expect(user instanceof Barter._User).toBe(true); | ||
expect(user.id).toBe("3ede"); | ||
}); | ||
const libbarter = require("../index.js"); | ||
|
||
const Barter = new libbarter(); | ||
|
||
test("getCollection() returns an array of valid CollectionItems", () => { | ||
return Barter.getCollection("3ede", "b").then(collection => { | ||
expect(collection.filter(i => i instanceof Barter._CollectionItem).length).toBe(collection.length); | ||
expect(collection[0].id).toBe(9555); | ||
}); | ||
}); | ||
|
||
test("getGlobalOfferCollector() returns a GlobalOfferCollector that emits FeedOffers (this may take a moment...)", () => { | ||
jest.setTimeout(120000); | ||
|
||
let collector = Barter.getGlobalOfferCollector(1000); | ||
|
||
expect(collector instanceof Barter._GlobalOfferCollector).toBe(true); | ||
|
||
return new Promise((resolve, reject) => { | ||
collector.once("offer", offer => { | ||
expect(offer instanceof Barter._FeedOffer).toBe(true); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
|
||
test("getItem() returns a valid Item", () => { | ||
return Barter.getItem(9555).then(item => { | ||
expect(item instanceof Barter._Item).toBe(true); | ||
expect(item.title).toBe("Trading Card"); | ||
}); | ||
}); | ||
|
||
test("getOffer() returns a valid Offer", () => { | ||
return Barter.getOffer(2247076).then(offer => { | ||
expect(offer instanceof Barter._Offer).toBe(true); | ||
expect(offer.opened).toBe(false); | ||
expect(offer.isMultiUser).toBe(false); | ||
expect(offer.to.id).toBe("3ede"); | ||
}); | ||
}); | ||
|
||
test("getOffers() returns an object of valid LimitedOffers", () => { | ||
return Barter.getOffers("3ede").then(offers => { | ||
expect(Object.keys(offers).filter(i => offers[i] instanceof Barter._LimitedOffer).length).toBe(Object.keys(offers).length); | ||
expect(offers[2247076].isFailed).toBe(false); | ||
}); | ||
}); | ||
|
||
test("getUser() returns a valid User", () => { | ||
return Barter.getUser("3ede").then(user => { | ||
expect(user instanceof Barter._User).toBe(true); | ||
expect(user.steamid).toBe("76561198852754414"); | ||
}); | ||
}); | ||
|
||
test("getUserBySteamID() returns a valid User", () => { | ||
return Barter.getUserBySteamID("76561198852754414").then(user => { | ||
expect(user instanceof Barter._User).toBe(true); | ||
expect(user.id).toBe("3ede"); | ||
}); | ||
}); | ||
|
||
test("getFirehose() returns a valid Firehose that emits FirehoseItems", () => { | ||
return new Promise((resolve, reject) => { | ||
let fh = Barter.getFirehose("wishlists"); | ||
|
||
expect(fh instanceof Barter._Firehose).toBe(true); | ||
fh.once("item", resolve); | ||
}).then(item => { | ||
expect(item instanceof Barter._FirehoseItem).toBe(true); | ||
expect(item.user instanceof Barter._LimitedUser).toBe(true); | ||
}); | ||
}); | ||
|
||
test("Firehose respects cache", () => { | ||
let items; | ||
|
||
return new Promise((resolve, reject) => { | ||
let fh = Barter.getFirehose("wishlists"); | ||
|
||
fh.once("items", i => { | ||
items = i; | ||
|
||
fh.destroy(); | ||
|
||
fh = Barter.getFirehose("wishlists", items); | ||
|
||
let ok = true; | ||
|
||
fh.on("item", item => { | ||
if( items.indexOf(item.lineItem) > -1 ) ok = false; | ||
}); | ||
|
||
fh.on("items", () => { | ||
resolve(ok); | ||
}); | ||
}); | ||
}).then(res => { | ||
expect(res).toBe(true); | ||
}); | ||
}); |