<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd" https="false">
<meta>
    <author>Sam Pullara (Assisted by TableSaw)</author>
    <description/>
    <documentationURL>http://help.simplegeo.com/faqs/api-documentation/endpoints</documentationURL>
    <sampleQuery>select * from {table} where ck=@ck and secret=@secret and layer='com.simplegeo.global.twitter' and id='6467771902';</sampleQuery>
</meta>
<execute><![CDATA[
var document = {getElementsByTagName:function(value) { return null; }};
// Include the OAuth libraries from oauth.net
y.include("http://oauth.googlecode.com/svn/code/javascript/oauth.js");
y.include("http://oauth.googlecode.com/svn/code/javascript/sha1.js");

// Collect all the parameters
var encodedurl = request.url;
var accessor = { consumerSecret:secret, tokenSecret:""};
]]></execute>
<bindings>
    <select produces="JSON">
        <urls>
            <url>http://api.simplegeo.com/{version}/records/{layer}/{id}/history.json</url>
        </urls>
        <inputs>
            <key id="version" type="xs:string" paramType="path" default='0.1'/>
            <key id="layer" type="xs:string" paramType="path" required='true'/>
            <key id="id" type="xs:string" paramType="path" required='true'/>
            <key id="ck" type="xs:string" paramType="variable" required="true"/>
            <key id="secret" type="xs:string" paramType="variable" required="true"/>
			<key id="history" type="xs:string" paramType="variable" required="true"/>
        </inputs>
        <execute>
<![CDATA[
var message = { action: encodedurl, method: "GET", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
OAuth.setTimestampAndNonce(message);

// Sign the request
OAuth.SignatureMethod.sign(message, accessor);

try {
   // PUT the content to the service along with the OAuth header
   response.object = request
       .header("Authorization", OAuth.getAuthorizationHeader("api.simplegeo.com", message.parameters))
       .get().response;
} catch(err) {
   response.object = {'result':'failure', 'error': err};
}
            ]]>
        </execute>
    </select>
	    <select produces="JSON">
	        <urls>
	            <url>http://api.simplegeo.com/{version}/records/{layer}/{id}.json</url>
	        </urls>
	        <inputs>
	            <key id="version" type="xs:string" paramType="path" default='0.1'/>
	            <key id="layer" type="xs:string" paramType="path" required='true'/>
	            <key id="id" type="xs:string" paramType="path" required='true'/>
	            <key id="ck" type="xs:string" paramType="variable" required="true"/>
	            <key id="secret" type="xs:string" paramType="variable" required="true"/>
	        </inputs>
	        <execute>
	<![CDATA[
	var message = { action: encodedurl, method: "GET", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
	OAuth.setTimestampAndNonce(message);

	// Sign the request
	OAuth.SignatureMethod.sign(message, accessor);

	try {
	   // PUT the content to the service along with the OAuth header
	   response.object = request
	       .header("Authorization", OAuth.getAuthorizationHeader("api.simplegeo.com", message.parameters))
	       .get().response;
	} catch(err) {
	   response.object = {'result':'failure', 'error': err};
	}
	            ]]>
	        </execute>
	    </select>
    <insert produces="JSON">
        <urls>
            <url>http://api.simplegeo.com/{version}/records/{layer}.json</url>
        </urls>
        <inputs>
            <key id="version" type="xs:string" paramType="path" default='0.1'/>
            <key id="layer" type="xs:string" paramType="path" required='true'/>
            <key id="id" type="xs:string" paramType="path" required='true'/>
            <key id="ck" type="xs:string" paramType="variable" required="true"/>
            <key id="secret" type="xs:string" paramType="variable" required="true"/>
            <key id="lat" paramType="variable" required="true"/>
            <key id="lon" paramType="variable" required="true"/>
        </inputs>
        <execute>
<![CDATA[
var message = { action: encodedurl, method: "PUT", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
OAuth.setTimestampAndNonce(message);

// Sign the request
OAuth.SignatureMethod.sign(message, accessor);

try {
   // PUT the content to the service along with the OAuth header
   var content = 
   '{"features": [{"id":"' + id + 
   '","expiration":0,"created":' + new Date().getTime() + 
   ',"layer":"' + layer + '","properties":{},"type":"Feature","geometry":{"type":"Point","coordinates":[' + lat + ',' + lon + 
   ']}}], "type":"FeatureCollection"}';
   var auth = OAuth.getAuthorizationHeader("api.simplegeo.com", message.parameters);
   response.object = request
       .contentType("application/json")
       .header("Authorization", auth)
       .put(content).response;
} catch(err) {
   response.object = {'result':'failure', 'error': err};
}
            ]]>
        </execute>
    </insert>
	    <update produces="JSON">
	        <urls>
	            <url>http://api.simplegeo.com/{version}/records/{layer}/{id}.json</url>
	        </urls>
	        <inputs>
	            <key id="version" type="xs:string" paramType="path" default='0.1'/>
	            <key id="layer" type="xs:string" paramType="path" required='true'/>
	            <key id="id" type="xs:string" paramType="path" required='true'/>
	            <key id="ck" type="xs:string" paramType="variable" required="true"/>
	            <key id="secret" type="xs:string" paramType="variable" required="true"/>
	            <value id="lat" paramType="variable" required="true"/>
	            <value id="lon" paramType="variable" required="true"/>
	        </inputs>
	        <execute>
	<![CDATA[
	var message = { action: encodedurl, method: "PUT", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
	OAuth.setTimestampAndNonce(message);

	// Sign the request
	OAuth.SignatureMethod.sign(message, accessor);

	try {
	   // PUT the content to the service along with the OAuth header
	   var content = 
	   '{"id":"' + id + 
	   '","layer":"' + layer + '","properties":{},"type":"Feature","geometry":{"type":"Point","coordinates":[' + lat + ',' + lon + 
	   ']}}';
	y.log(content);
	   var auth = OAuth.getAuthorizationHeader("api.simplegeo.com", message.parameters);
	   response.object = request
	       .contentType("application/json")
	       .header("Authorization", auth)
	       .put(content).response;
	} catch(err) {
	   response.object = {'result':'failure', 'error': err};
	}
	            ]]>
	        </execute>
	    </update>
		    <delete produces="JSON">
		        <urls>
		            <url>http://api.simplegeo.com/{version}/records/{layer}/{id}.json</url>
		        </urls>
		        <inputs>
		            <key id="version" type="xs:string" paramType="path" default='0.1'/>
		            <key id="layer" type="xs:string" paramType="path" required='true'/>
		            <key id="id" type="xs:string" paramType="path" required='true'/>
		            <key id="ck" type="xs:string" paramType="variable" required="true"/>
		            <key id="secret" type="xs:string" paramType="variable" required="true"/>
		        </inputs>
		        <execute>
		<![CDATA[
		var message = { action: encodedurl, method: "DELETE", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
		OAuth.setTimestampAndNonce(message);

		// Sign the request
		OAuth.SignatureMethod.sign(message, accessor);

		try {
		   var auth = OAuth.getAuthorizationHeader("api.simplegeo.com", message.parameters);
		   response.object = request
		       .header("Authorization", auth)
		       .del().response;
		} catch(err) {
		   response.object = {'result':'failure', 'error': err};
		}
		            ]]>
		        </execute>
		    </delete>
</bindings>
</table>
