Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d81786
commit 4493de4
Showing
10 changed files
with
306 additions
and
118 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require('coffee-script'); | ||
(function() { | ||
|
||
module.exports = require('./request'); | ||
module.exports = require('./request'); | ||
|
||
}).call(this); |
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,115 @@ | ||
(function() { | ||
var Request, Response, crypto, http; | ||
|
||
crypto = require('crypto'); | ||
|
||
http = require('http'); | ||
|
||
Response = require('./response'); | ||
|
||
Request = (function() { | ||
var CURRENT_API_VERSION, HOSTS; | ||
|
||
CURRENT_API_VERSION = '2011-08-01'; | ||
|
||
HOSTS = { | ||
ca: 'ecs.amazonaws.ca', | ||
cn: 'webservices.amazon.cn', | ||
de: 'ecs.amazonaws.de', | ||
es: 'webservices.amazon.es', | ||
fr: 'ecs.amazonaws.fr', | ||
it: 'webservices.amazon.it', | ||
jp: 'ecs.amazonaws.jp', | ||
uk: 'ecs.amazonaws.co.uk', | ||
us: 'ecs.amazonaws.com' | ||
}; | ||
|
||
function Request(options) { | ||
var locale; | ||
locale = options.locale || 'us'; | ||
this._key = options.key || (function() { | ||
throw 'Missing key'; | ||
})(); | ||
this._secret = options.secret || (function() { | ||
throw 'Missing secret'; | ||
})(); | ||
this._tag = options.tag || (function() { | ||
throw 'Missing associate tag'; | ||
})(); | ||
this._host = HOSTS[locale] || (function() { | ||
throw 'Bad locale'; | ||
})(); | ||
this.reset(); | ||
} | ||
|
||
Request.prototype.add = function(params) { | ||
var key, val; | ||
for (key in params) { | ||
val = params[key]; | ||
if (val.constructor === Array) val = val.join(','); | ||
key = key[0].toUpperCase() + key.slice(1); | ||
this._params[key] = val; | ||
} | ||
return this; | ||
}; | ||
|
||
Request.prototype.get = function(callback, errback) { | ||
var options; | ||
if (errback == null) errback = function() {}; | ||
options = { | ||
host: this._host, | ||
path: "/onca/xml?" + (this._query()) | ||
}; | ||
return http.get(options, function(res) { | ||
var data; | ||
data = ''; | ||
return res.on('data', function(chunk) { | ||
return data += chunk; | ||
}).on('end', function() { | ||
return callback(new Response(data, res.statusCode)); | ||
}).on('error', function(e) { | ||
return errback(e); | ||
}); | ||
}); | ||
}; | ||
|
||
Request.prototype.reset = function() { | ||
this._params = { | ||
AWSAccessKeyId: this._key, | ||
AssociateTag: this._tag, | ||
Service: 'AWSECommerceService', | ||
Timestamp: new Date().toISOString(), | ||
Version: CURRENT_API_VERSION | ||
}; | ||
return this; | ||
}; | ||
|
||
Request.prototype._query = function() { | ||
var hmac, key, query, signature, val; | ||
query = ((function() { | ||
var _ref, _results; | ||
_ref = this._params; | ||
_results = []; | ||
for (key in _ref) { | ||
val = _ref[key]; | ||
_results.push([key, val]); | ||
} | ||
return _results; | ||
}).call(this)).sort(function(self, other) { | ||
return self[0] > other[0]; | ||
}).map(function(tuple) { | ||
return "" + tuple[0] + "=" + (encodeURIComponent(tuple[1])); | ||
}).join('&'); | ||
hmac = crypto.createHmac('sha256', this._secret); | ||
hmac.update(['GET', this._host, '/onca/xml', query].join("\n")); | ||
signature = hmac.digest('base64'); | ||
return "" + query + "&Signature=" + (encodeURIComponent(signature)); | ||
}; | ||
|
||
return Request; | ||
|
||
})(); | ||
|
||
module.exports = Request; | ||
|
||
}).call(this); |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,38 +1,34 @@ | ||
fs = require 'fs' | ||
libxmljs = require 'libxmljs' | ||
Response = require '../../lib/vacuum/response' | ||
Response = require '../../src/vacuum/response' | ||
|
||
describe 'Response', -> | ||
beforeEach -> | ||
data = fs.readFileSync("#{__dirname}/../fixtures/response", 'utf8') | ||
@res = new Response data, 200 | ||
|
||
describe '#find', -> | ||
it 'returns an array of matches', -> | ||
expect(@res.find('Item').constructor).toBe Array | ||
expect(@res.find('Item')[0]['ASIN'].constructor).toBe String | ||
|
||
describe '#_xmlToObject', -> | ||
beforeEach -> | ||
data = '<?xml version="1.0" ?>' + | ||
'<Item>' + | ||
'<Title>A Title</Title>' + | ||
'<Author>An Author</Author>' + | ||
'<Author>Another Author</Author>' + | ||
'<Creator Role="Translator">A Translator</Creator>' + | ||
'</Item>' | ||
doc = libxmljs.parseXmlString(data) | ||
@obj = @res._xmlToObject doc.root() | ||
|
||
it 'returns an object', -> | ||
expect(@obj.constructor).toBe Object | ||
|
||
it 'handles children with no siblings', -> | ||
expect(@obj['Title']).toBe 'A Title' | ||
|
||
it 'handles children with siblings', -> | ||
expect(@obj['Author'].constructor).toBe Array | ||
|
||
it 'handles attributes', -> | ||
expect(@obj['Creator']['Role']).toBe 'Translator' | ||
expect(@obj['Creator']['__content__']).toBe 'A Translator' | ||
@items = @res.find('Item') | ||
|
||
it 'returns matches to a given query', -> | ||
expect(@items.length).toBeGreaterThan 0 | ||
|
||
it 'returns an empty array if there are no matches', -> | ||
expect(@res.find('foo-bar-baz').length).toBe 0 | ||
|
||
it 'parses nodes with no siblings', -> | ||
expect(@items[0]['ASIN']).toBeDefined() | ||
|
||
it 'parses nodes with siblings', -> | ||
itemLinks = @items[0]['ItemLinks']['ItemLink'] | ||
expect(itemLinks.length).toBeGreaterThan 0 | ||
|
||
it 'parses nodes with attributes', -> | ||
creator = @items[0]['ItemAttributes']['Creator'] | ||
expect(creator['Role']).toBeDefined() | ||
expect(creator['__content']).toBeDefined() | ||
|
||
describe '#toObject', -> | ||
it 'returns entire response as an object', -> | ||
expect(@res.toObject().constructor).toBe Object |
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 @@ | ||
module.exports = require './request' |
Oops, something went wrong.