Skip to content
This repository has been archived by the owner on May 29, 2018. It is now read-only.

Commit

Permalink
Cache implementation. Badge returns an anonymous prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
hiendv committed Aug 19, 2016
1 parent 4d09b36 commit 7575593
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
APP_PORT=1406
APP_URL=
APP_CACHE=1000
GITHUB_TOKEN=PersonalAccessToken
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"koa": "^1.2.1",
"koa-route": "^2.4.2",
"koa-static": "^2.0.0",
"memoizee": "^0.4.1",
"octokat": "^0.4.18"
},
"devDependencies": {
Expand Down
54 changes: 33 additions & 21 deletions src/Badge.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
export default {
draw (src) {
if (this.$env.APP_URL) {
return this.$env.APP_URL + '/' + src
}
return '/' + src
},
show (id, repo) {
return this.$app.github.users(id)
.fetch()
.then(user => {
if (user.hireable) {
return 'hireable-yes.svg'
}
return 'hireable-no.svg'
})
.catch(e => {
return 'hireable-error.svg'
// console.log(e.json ? e.json.message : e.message, e.status)
})
.then(src => this.draw(src))
import Cache from 'memoizee'
let Badge = function (app, env) {
this.$app = app
this.$env = env
if (this.$env.APP_CACHE) {
this.show = Cache(this._show, { maxAge: this.$env.APP_CACHE, promise: true })
} else {
this.show = this._show
}
}

Badge.prototype.draw = function (src) {
if (this.$env.APP_URL) {
return this.$env.APP_URL + '/' + src
}
return '/' + src
}

Badge.prototype._show = function (id, repo) {
return this.$app.github.users(id)
.fetch()
.then(user => {
if (user.hireable) {
return 'hireable-yes.svg'
}
return 'hireable-no.svg'
})
.catch(e => {
return 'hireable-error.svg'
// console.log(e.json ? e.json.message : e.message, e.status)
})
.then(src => this.draw(src))
}

export default Badge
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ app.context.github = new Octokat({
token: process.env.GITHUB_TOKEN
})

Badge.$app = app.context
Badge.$env = process.env
const badge = new Badge(app.context, process.env)

app.use(Serve(path.join(__dirname, '../public'), {
maxage: 31536000000
Expand All @@ -32,7 +31,7 @@ app.use(Route.get('/p/:user', function * (user) {
}))

app.use(Route.get('/:user/:repo?', function * show (id, repo) {
yield Badge.show(id, repo).then(src => {
yield badge.show(id, repo).then(src => {
this.redirect(src)
})
}))
Expand Down

0 comments on commit 7575593

Please sign in to comment.