Skip to content

Commit

Permalink
Add an identify command that checks artists, artworks and partners
Browse files Browse the repository at this point in the history
  • Loading branch information
anandaroop authored and jonallured committed Sep 6, 2019
1 parent 41892f9 commit e90f9b4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/commands/identify.ts
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(", ")}`)
}
}
}
83 changes: 83 additions & 0 deletions test/commands/identify.test.ts
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"
)
})
})
})

0 comments on commit e90f9b4

Please sign in to comment.