Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an identify command #FF #7

Merged
merged 5 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"@types/chai": "^4",
"@types/mocha": "^5",
"@types/node": "^10",
"@types/node-fetch": "^2.5.0",
"chai": "^4",
"globby": "^10",
"mocha": "^5",
"nock": "^10.0.6",
"nyc": "^13",
"ts-node": "^8",
"tslint": "^5",
Expand Down
49 changes: 49 additions & 0 deletions src/commands/identify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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: CollectionMapping[] = [
{ 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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strategy here is to build up all the requests for the various collections, fire em off all at once, and look for one that succeeded.


if (foundIndex >= 0) {
const foundCollection = Identify.collectionsToCheck[foundIndex]
const foundResource = `${foundCollection.endpoint}/${id}`
this.log(`${foundCollection.name} ${gravity.url(`api/v1/${foundResource}`)}`)
} else {
const collections = Identify.collectionsToCheck.map(c => c.name)
this.log(`Nothing found in: ${collections.join(", ")}`)
}
}
}

interface CollectionMapping {
/** Name of the Gravity resource */
name: string

/** The name of the collection as it appears in the GET endpoint for the resource, i.e. /api/v1/<endpoint>/:id */
endpoint: string
}
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)
Copy link
Member Author

@anandaroop anandaroop Sep 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second arg to .reply could be an object representing the JSON response. But here we mainly care whether the request 404'd or not.

)
.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"
)
})
})
})
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"trailing-comma": false,
"variable-name": [true, "ban-keywords", "allow-leading-underscore"],
"jsdoc-format": false,
"radix": false
"radix": false,
"no-single-line-block-comment": false
}
}
39 changes: 37 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ cardinal@^2.1.1:
ansicolors "~0.3.2"
redeyed "~2.1.0"

chai@^4:
chai@^4, chai@^4.1.2:
version "4.2.0"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
Expand Down Expand Up @@ -613,6 +613,11 @@ deep-eql@^3.0.1:
dependencies:
type-detect "^4.0.0"

deep-equal@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
integrity sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=

default-require-extensions@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/default-require-extensions/-/default-require-extensions-2.0.0.tgz#f5f8fbb18a7d6d50b21f641f649ebb522cfe24f7"
Expand Down Expand Up @@ -1181,6 +1186,11 @@ json-parse-better-errors@^1.0.1:
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down Expand Up @@ -1260,7 +1270,7 @@ lodash.templatesettings@^4.0.0:
dependencies:
lodash._reinterpolate "^3.0.0"

lodash@^4.17.11, lodash@^4.17.13:
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.5:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
Expand Down Expand Up @@ -1419,6 +1429,21 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

nock@^10.0.6:
version "10.0.6"
resolved "https://registry.yarnpkg.com/nock/-/nock-10.0.6.tgz#e6d90ee7a68b8cfc2ab7f6127e7d99aa7d13d111"
integrity sha512-b47OWj1qf/LqSQYnmokNWM8D88KvUl2y7jT0567NB3ZBAZFz2bWp2PC81Xn7u8F2/vJxzkzNZybnemeFa7AZ2w==
dependencies:
chai "^4.1.2"
debug "^4.1.0"
deep-equal "^1.0.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.5"
mkdirp "^0.5.0"
propagate "^1.0.0"
qs "^6.5.1"
semver "^5.5.0"

node-fetch@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
Expand Down Expand Up @@ -1653,6 +1678,11 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

propagate@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-1.0.0.tgz#00c2daeedda20e87e3782b344adba1cddd6ad709"
integrity sha1-AMLa7t2iDofjeCs0Stuhzd1q1wk=

pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
Expand Down Expand Up @@ -1685,6 +1715,11 @@ qqjs@^0.3.10:
tmp "^0.1.0"
write-json-file "^4.1.1"

qs@^6.5.1:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==

read-pkg-up@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-4.0.0.tgz#1b221c6088ba7799601c808f91161c66e58f8978"
Expand Down