Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
npm-debug.log
node_modules/*
old/*
.nyc_output/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ marvel.events
})
})
```
## call as a promise
if you would like to use this library with promises, new in v1.2.0 is the ability to return a promise if you decide not to pass in a callback function to `get()`

example:
```js
marvel.events.name('civil war').get().then(function(results) {
console.log(results);
})
```


# documentation

Expand Down
36 changes: 9 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@

var Resource = require('./lib/resource.js')

var Marvel, merge, hasProp
var Marvel

hasProp = function(o, p) {
return Object.prototype.hasOwnProperty.call(o, p)
}

// merge
merge = function(a, b) {
var k

for (k in a) {
if (hasProp(a, k) && hasProp(b, k) === false) {
b[k] = a[k]
}
}
return b
}

Marvel = function(opts) {
Marvel = function(opts = {}) {
var defaults = {
apiDomain: 'https://gateway.marvel.com'
}
var resOpt

opts = merge(defaults, opts || {})
opts = { ...defaults, ...opts, API_VERSION: Marvel.API_VERSION }

if (opts.privateKey === undefined || opts.publicKey === undefined) {
throw new Error(
Expand All @@ -39,14 +23,12 @@ Marvel = function(opts) {
this.privateKey = opts.privateKey
this.apiDomain = opts.apiDomain

resOpt = merge(opts, { API_VERSION: Marvel.API_VERSION })

this.characters = new Resource('characters', resOpt)
this.comics = new Resource('comics', resOpt)
this.creators = new Resource('creators', resOpt)
this.events = new Resource('events', resOpt)
this.series = new Resource('series', resOpt)
this.stories = new Resource('stories', resOpt)
this.characters = new Resource('characters', opts)
this.comics = new Resource('comics', opts)
this.creators = new Resource('creators', opts)
this.events = new Resource('events', opts)
this.series = new Resource('series', opts)
this.stories = new Resource('stories', opts)
}

Marvel.API_VERSION = 'v1'
Expand Down
65 changes: 30 additions & 35 deletions lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,13 @@ var plural = require('plural').addRule(/series/i, function(w) {
})
var fields = require('./resource_fields')

var Resource, resources, res, lower, merge, hasProp

// lowercase helper
lower = function(s) {
var lower = function(s) {
return s.toLowerCase()
}

hasProp = function(o, p) {
return Object.prototype.hasOwnProperty.call(o, p)
}

// merge
merge = function(a, b) {
var k

for (k in a) {
if (hasProp(a, k) && hasProp(b, k) === false) {
b[k] = a[k]
}
}
return b
}
var Resource
var resources
var res

Resource = function(resource, opts) {
var resourceCalls = fields[resource]
Expand Down Expand Up @@ -62,37 +47,46 @@ Resource.prototype.hash = function(ts) {
.update(String(ts) + this.privateKey + this.publicKey)
.digest('hex')
}

Resource.prototype.get = function(cb) {
var self = this
if (!cb && typeof Promise !== 'undefined') {
return new Promise(function(resolve, reject) {
self._get(function(err, res) {
if (err) return reject(err)
return resolve(res)
})
})
} else {
self._get(cb)
}
}
Resource.prototype._get = function(cb) {
var uri
var qs
var ts = +new Date()

uri =
this.apiDomain + '/' + this.API_VERSION + '/public/' + lower(this.resource)

qs = merge(
{
apikey: this.publicKey,
ts: ts,
hash: this.hash(ts)
},
this.param
)
qs = {
apikey: this.publicKey,
ts: ts,
hash: this.hash(ts),
...this.param
}

var fullUrl = uri + '?' + querystring.stringify(qs)

http
.get(
merge(url.parse(fullUrl), {
{
...url.parse(fullUrl),
headers: {
'Accept-Encoding': 'gzip'
},
encoding: null
}),
},
function(res) {
var rawBuffer = []

res.on('data', function(chunk) {
rawBuffer.push(chunk)
})
Expand All @@ -109,13 +103,15 @@ Resource.prototype.get = function(cb) {
return cb(null, pBody.data.results, pBody)
})
})

/* istanbul ignore next */
res.on('error', function(e) {
return cb(e)
})
}
)
.on('error', function(err) {
console.log(err)
return cb(err)
})
this.param = {}
}
Expand All @@ -131,11 +127,10 @@ Resource.prototype.limit = function() {
}
return this
}

resources = ['Comic', 'Character', 'Creator', 'Event', 'Series', 'Story']

for (var i = 0; i < resources.length; i++) {
var fn = (function addResource(rsrc) {
;(function addResource(rsrc) {
rsrc = plural(lower(rsrc))
Resource.prototype[rsrc] = function(queryVal) {
if (this.resource !== rsrc) {
Expand Down
Loading