-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an identify command that checks artists, artworks and partners
- Loading branch information
1 parent
41892f9
commit e90f9b4
Showing
2 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Command, flags } from "@oclif/command" | ||
import Gravity from "../lib/gravity" | ||
|
||
export default class Identify extends Command { | ||
static description = "Identify a Gravity resource by its BSON ID" | ||
|
||
static flags = { | ||
help: flags.help({ char: "h" }), | ||
} | ||
|
||
static args = [{ name: "id" }] | ||
|
||
static collectionsToCheck = [ | ||
{ name: "Artist", endpoint: "artist" }, | ||
{ name: "Artwork", endpoint: "artwork" }, | ||
{ name: "Partner", endpoint: "partner" }, | ||
] | ||
|
||
async run() { | ||
const gravity = new Gravity() | ||
const { args } = this.parse(Identify) | ||
const { id } = args | ||
|
||
const gravityPromises = Identify.collectionsToCheck.map(collection => { | ||
const resource = `${collection.endpoint}/${id}` | ||
return gravity.get(resource) | ||
}) | ||
|
||
const gravityResponses = await Promise.all(gravityPromises) | ||
const foundIndex = gravityResponses.findIndex(r => r.status === 200) | ||
|
||
if (foundIndex >= 0) { | ||
const foundCollection = Identify.collectionsToCheck[foundIndex] | ||
const foundResource = `${foundCollection.endpoint}/${id}` | ||
this.log(`${foundCollection.name} ${gravity.url(foundResource)}`) | ||
} else { | ||
const collections = Identify.collectionsToCheck.map(c => c.name) | ||
this.log(`Nothing found in: ${collections.join(", ")}`) | ||
} | ||
} | ||
} |
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,83 @@ | ||
import { expect, test } from "@oclif/test" | ||
|
||
describe("identify", () => { | ||
describe("when the artwork exists", () => { | ||
test | ||
.nock("https://stagingapi.artsy.net", api => | ||
api | ||
.get("/api/v1/artist/abc123") | ||
.reply(404) | ||
.get("/api/v1/artwork/abc123") | ||
.reply(200) | ||
.get("/api/v1/partner/abc123") | ||
.reply(404) | ||
) | ||
.stdout() | ||
.command(["identify", "abc123"]) | ||
.it("displays a found artwork message", ctx => { | ||
expect(ctx.stdout).to.equal( | ||
"Artwork https://stagingapi.artsy.net/api/v1/artwork/abc123\n" | ||
) | ||
}) | ||
}) | ||
|
||
describe("when the artist exists", () => { | ||
test | ||
.nock("https://stagingapi.artsy.net", api => | ||
api | ||
.get("/api/v1/artist/abc123") | ||
.reply(200) | ||
.get("/api/v1/artwork/abc123") | ||
.reply(404) | ||
.get("/api/v1/partner/abc123") | ||
.reply(404) | ||
) | ||
.stdout() | ||
.command(["identify", "abc123"]) | ||
.it("displays a found artist message", ctx => { | ||
expect(ctx.stdout).to.equal( | ||
"Artist https://stagingapi.artsy.net/api/v1/artist/abc123\n" | ||
) | ||
}) | ||
}) | ||
|
||
describe("when the partner exists", () => { | ||
test | ||
.nock("https://stagingapi.artsy.net", api => | ||
api | ||
.get("/api/v1/artist/abc123") | ||
.reply(404) | ||
.get("/api/v1/artwork/abc123") | ||
.reply(404) | ||
.get("/api/v1/partner/abc123") | ||
.reply(200) | ||
) | ||
.stdout() | ||
.command(["identify", "abc123"]) | ||
.it("displays a found partner message", ctx => { | ||
expect(ctx.stdout).to.equal( | ||
"Partner https://stagingapi.artsy.net/api/v1/partner/abc123\n" | ||
) | ||
}) | ||
}) | ||
|
||
describe("when nothing is found", () => { | ||
test | ||
.nock("https://stagingapi.artsy.net", api => | ||
api | ||
.get("/api/v1/artwork/abc123") | ||
.reply(404) | ||
.get("/api/v1/artist/abc123") | ||
.reply(404) | ||
.get("/api/v1/partner/abc123") | ||
.reply(404) | ||
) | ||
.stdout() | ||
.command(["identify", "abc123"]) | ||
.it("displays a not-found message", ctx => { | ||
expect(ctx.stdout).to.equal( | ||
"Nothing found in: Artist, Artwork, Partner\n" | ||
) | ||
}) | ||
}) | ||
}) |