// --------------------------------------------------------------------------------------------------------------------
//
// sqs.js - class for AWS SQS
//
// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
// Written by Andrew Chilton <chilts@appsattic.com>
//
// License: http://opensource.org/licenses/MIT
//
// --------------------------------------------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------------------------------------
// requires

// built-ins
var util = require('util');

// dependencies
var _ = require('underscore');

// our own
var awssum = require('../awssum');
var amazon = require('./amazon');
var operations = require('./sqs-config');

// --------------------------------------------------------------------------------------------------------------------
// package variables

var MARK = 'sqs: ';

// From: http://docs.amazonwebservices.com/general/latest/gr/rande.html
var endPoint = {};
endPoint[amazon.US_EAST_1]      = "sqs.us-east-1.amazonaws.com";
endPoint[amazon.US_WEST_1]      = "sqs.us-west-1.amazonaws.com";
endPoint[amazon.US_WEST_2]      = "sqs.us-west-2.amazonaws.com";
endPoint[amazon.EU_WEST_1]      = "sqs.eu-west-1.amazonaws.com";
endPoint[amazon.AP_SOUTHEAST_1] = "sqs.ap-southeast-1.amazonaws.com";
endPoint[amazon.AP_SOUTHEAST_2] = "sqs.ap-southeast-2.amazonaws.com";
endPoint[amazon.AP_NORTHEAST_1] = "sqs.ap-northeast-1.amazonaws.com";
endPoint[amazon.SA_EAST_1]      = "sqs.sa-east-1.amazonaws.com";
endPoint[amazon.US_GOV_WEST_1]  = "sqs.us-gov-west-1.amazonaws.com";

var version = '2011-10-01';

// --------------------------------------------------------------------------------------------------------------------
// constructor

var Sqs = function(opts) {
    var self = this;

    // call the superclass for initialisation
    Sqs.super_.call(this, opts);

    // check the region is valid
    if ( ! endPoint[opts.region] ) {
        throw MARK + "invalid region '" + opts.region + "'";
    }

    // check we have an awsAccountId
    if ( ! opts.awsAccountId ) {
        throw MARK + "provide a 'awsAccountId'";
    }
    self.setAwsAccountId(opts.awsAccountId);

    return self;
};

// inherit from Amazon
util.inherits(Sqs, amazon.Amazon);

// --------------------------------------------------------------------------------------------------------------------
// methods we need to implement from awssum.js/amazon.js

Sqs.prototype.host = function() {
    return endPoint[this.region()];
};

Sqs.prototype.version = function() {
    return version;
};

// --------------------------------------------------------------------------------------------------------------------
// operations on the service

_.each(operations, function(operation, operationName) {
    Sqs.prototype[operationName] = awssum.makeOperation(operation);
});

// --------------------------------------------------------------------------------------------------------------------
// exports

exports.Sqs = Sqs;

// --------------------------------------------------------------------------------------------------------------------

@@ -783,16 +783,68 @@
 onfig');
+%0Avar awsSignatureV4 = require('./aws-signature-v4');
 %0A%0A// ---
@@ -2692,16 +2692,651 @@
 on;%0A%7D;%0A%0A
+// --------------------------------------------------------------------------------------------------------------------%0A// AWS Signature v4%0A%0ASqs.prototype.scope = function() %7B%0A    return 'sqs';%0A%7D%0A%0ASqs.prototype.needsTarget = function() %7B%0A    return false;%0A%7D%0A%0A// This service uses the AWS Signature v4.%0ASqs.prototype.strToSign        = awsSignatureV4.strToSign;%0ASqs.prototype.signature        = awsSignatureV4.signature;%0ASqs.prototype.addSignature     = awsSignatureV4.addSignature;%0ASqs.prototype.addCommonOptions = awsSignatureV4.addCommonOptions;%0ASqs.prototype.contentType      = function() %7B return 'application/x-amz-json-1.0'; %7D;%0A%0A
 // -----

Add support for AWS Signature Version 4 for SQS