-
Notifications
You must be signed in to change notification settings - Fork 1
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
3734375
commit df96536
Showing
6 changed files
with
140 additions
and
9 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,4 @@ | ||
require('coffee-script'); | ||
require('underscore'); | ||
|
||
module.exports = require('./request'); |
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,4 +1,67 @@ | ||
_ = require 'underscore' | ||
http = require 'http' | ||
Response = require './response' | ||
|
||
class Request | ||
constructor: -> | ||
constructor: (options) -> | ||
@_key = options.key or throw 'Missing key' | ||
@_secret = options.secret or throw 'Missing secret' | ||
@_tag = options.tag or throw 'Missing associate tag' | ||
@_host = { | ||
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' | ||
}[options.locale or 'us'] or throw 'Bad locale' | ||
@reset() | ||
|
||
add: (properties) -> | ||
for key, value of properties | ||
value = value.join(',') if value.constructor == Array | ||
key = key[0].toUpperCase() + key.slice(1) | ||
|
||
@params[key] = value | ||
|
||
@ | ||
|
||
get: (callback) -> | ||
options = | ||
host: @_host | ||
path: "/onca/xml?#{@_query()}" | ||
|
||
http.get options, (res) -> | ||
data = '' | ||
res.on 'data', (chunk) -> | ||
data += chunk | ||
.on 'end', -> | ||
callback new Response(data, res.statusCode) | ||
.on 'error', (error) -> | ||
|
||
reset: -> | ||
@params = | ||
AWSAccessKeyId : @_key | ||
AssociateTag : @_tag | ||
Service : 'AWSECommerceService' | ||
Timestamp : new Date().toJSON() | ||
Version : '2011-08-01' | ||
|
||
@ | ||
|
||
_query: -> | ||
_ | ||
.chain(@params) | ||
.map (value, key) -> | ||
[key, value] | ||
.sortBy (tuple) -> | ||
tuple[0] | ||
.map (tuple) -> | ||
"#{tuple[0]}=#{encodeURIComponent(tuple[1])}" | ||
.value() | ||
.join('&') | ||
|
||
module.exports = Request |
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,4 +1,4 @@ | ||
class Response | ||
constructor: -> | ||
constructor: (data, statusCode) -> | ||
|
||
module.exports = Response |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
Request = require '../../lib/vacuum/request' | ||
|
||
describe 'Request', -> | ||
beforeEach -> | ||
@req = new Request | ||
key: process.env.AMAZON_KEY | ||
secret: process.env.AMAZON_SECRET | ||
tag: process.env.AMAZON_TAG | ||
|
||
describe 'constructor', -> | ||
it 'requires a key', -> | ||
expect -> | ||
new Request | ||
secret: 'foo' | ||
tag: ' bar' | ||
.toThrow 'Missing key' | ||
|
||
it 'requires a secret', -> | ||
expect -> | ||
new Request | ||
key: 'foo' | ||
tag: 'bar' | ||
.toThrow 'Missing secret' | ||
|
||
it 'requires an associate tag', -> | ||
expect -> | ||
new Request | ||
key: 'foo' | ||
secret: 'bar' | ||
.toThrow 'Missing associate tag' | ||
|
||
it 'requires a valid locale', -> | ||
expect -> | ||
new Request | ||
key: 'foo' | ||
secret: 'bar' | ||
tag: 'secret' | ||
locale: 'invalid' | ||
.toThrow 'Bad Locale' | ||
|
||
it 'defaults locale to the US', -> | ||
expect(@req._host).toMatch /\.com/ | ||
|
||
it 'sets up the parameters with default values', -> | ||
expect(@req.params.Timestamp).toBeDefined() | ||
|
||
describe '#add', -> | ||
it 'adds new parameters to the existing ones', -> | ||
expect(@req.add(foo: 'bar').params.Foo).toBe('bar') | ||
|
||
it 'casts values that are arrays to strings', -> | ||
expect(@req.add(foo: [1, 2]).params.Foo).toBe('1,2') | ||
|
||
describe '#get', -> | ||
it 'returns a response', -> | ||
@req.get (res) -> | ||
expect(res.constructor).toMatch /Response/ | ||
|
||
describe '#reset', -> | ||
it 'resets the parameters to default values', -> | ||
@req.params.foo = 1 | ||
expect(@req.reset.foo).toBeUndefined() | ||
|
||
describe '#_query', -> | ||
it 'sorts the parameters', -> | ||
expect(@req.add(a: 0, z: 1)._query()).toMatch /^A=0&.*Z=1$/ | ||
|
||
it 'canonicalizes the parameters', -> | ||
expect(@req._query()).toMatch /\w+=\w+&/ | ||
|
||
it 'URL-encodes values', -> | ||
expect(@req.add(foo: 'bar,baz')._query()).toMatch /bar%2Cbaz/ |