Skip to content

Commit

Permalink
Add request without signature
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Feb 4, 2012
1 parent 3734375 commit df96536
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/vacuum/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('coffee-script');
require('underscore');

module.exports = require('./request');
65 changes: 64 additions & 1 deletion lib/vacuum/request.coffee
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
2 changes: 1 addition & 1 deletion lib/vacuum/response.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Response
constructor: ->
constructor: (data, statusCode) ->

module.exports = Response
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"url":"git://github.com/hakanensari/vacuum.git"
},
"dependencies":{
"coffee-script":"*"
"coffee-script":">= 1.0",
"underscore":">= 1.0"
},
"devDependencies":{
"jasmine-node":"*"
Expand Down
6 changes: 0 additions & 6 deletions spec/vacuum-spec.coffee

This file was deleted.

72 changes: 72 additions & 0 deletions spec/vacuum/request-spec.coffee
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/

0 comments on commit df96536

Please sign in to comment.