Created
October 5, 2011 22:31
-
-
Save dyoder/1265943 to your computer and use it in GitHub Desktop.
REST Tutorial
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.url, | |
| parameters: { | |
| channel: "foo" | |
| }, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].MessageList, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.equals(response.content.data[0].message, | |
| "Hello, Foo!") | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.channels.url, | |
| parameters: { | |
| events: "create,delete" | |
| }, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].ChannelEvent, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.ok(response.content.data.name); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.channels.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].ChannelEvent, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.ok(response.content.data.name); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: description.resources.session.resources.channels.url, | |
| headers: { | |
| accept: description.schema["1.0"].channel.mediaType, | |
| content_type: description.schema["1.0"].channel.mediaType, | |
| authorization: "Capability " + | |
| description.resources.session.resources.channels.capability | |
| }, | |
| content: { | |
| name: "Foo", | |
| description: "The Foo channel, yo." | |
| }, | |
| on: { | |
| 201: function(response) { | |
| description.resources.channels = {}; | |
| description.resources.channels.Foo = response.content.data; | |
| assert.ok(description.resources.channels.Foo.url); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: description.resources.sessions.url, | |
| headers: { | |
| accept: description.schema["1.0"].session.mediaType, | |
| content_type: description.schema["1.0"].account.mediaType | |
| }, | |
| content: { | |
| key: description.resources.account.key | |
| }, | |
| on: { | |
| 201: function(response) { | |
| description.resources.session = response.content.data; | |
| assert.ok(description.resources.session); | |
| assert.ok(description.resources.session.url); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: description.resources.accounts.url, | |
| headers: { | |
| accept: description.schema["1.0"].session.mediaType, | |
| content_type: description.schema["1.0"].account.mediaType | |
| }, | |
| content: { | |
| email: "[email protected]", | |
| password: "password" | |
| }, | |
| on: { | |
| 201: function(response) { | |
| description.resources.session = response.content.data; | |
| description.resources.account = | |
| description.resources.session.resources.account | |
| assert.ok(description.resources.account); | |
| assert.ok(description.resources.account.url); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: "http://api.spire.io/", | |
| headers: { | |
| accept: "application/json" | |
| }, | |
| on: { | |
| response: function(response) { | |
| description = response.content.data; | |
| assert.ok(description.resources.sessions.url); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.subscribe.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].MessageList, | |
| }, | |
| content: { | |
| filter: { | |
| to: "Bob" | |
| } | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.equals(response.content.data[0].message.to, | |
| "Bob") | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.channels.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].ChannelList, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| resources.channels = response.content.data; | |
| assert.ok(resource.channels["foo"]); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| var Shred = require("shred") | |
| , shred = new Shred() | |
| , description | |
| , key = "1234567890"; | |
| ; |
This file contains hidden or 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
| shred.get({ | |
| url: description.resources.subscription.url, | |
| headers: { | |
| accept: description.schema["1.0"].events.mediaType, | |
| authorization: "Capability " + | |
| description.resources.subscription.capability | |
| }, | |
| parameters: { | |
| "last-message": description.lastMessageTimestamp | |
| }, | |
| on: { | |
| 200: function(response) { | |
| var events = response.content.data; | |
| assert.equal(events.messages.length, 0); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: description.resources.subscription.url, | |
| headers: { | |
| accept: description.schema["1.0"].events.mediaType, | |
| authorization: "Capability " + | |
| description.resources.subscription.capability | |
| }, | |
| parameters: { | |
| "last-message": description.lastMessageTimestamp, | |
| timeout: 30 // seconds | |
| }, | |
| on: { | |
| 200: function(response) { | |
| var events = response.content.data; | |
| assert.equal(events.messages[0].content, "Hello, World!"); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: description.resources.subscription.url, | |
| headers: { | |
| accept: description.schema["1.0"].events.mediaType, | |
| authorization: "Capability " + | |
| description.resources.subscription.capability | |
| }, | |
| on: { | |
| 200: function(response) { | |
| var events = response.content.data; | |
| description.lastMessageTimestamp = events.messages[0].timestamp; | |
| assert.equal(events.messages[0].content, "Hello, World!"); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: resources.notifications.url, | |
| parameters: { | |
| channel: "foo.bar" | |
| }, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| content_type: resources.schema["1.0"].Message | |
| accept: resources.schema["1.0"].Message, | |
| }, | |
| content: { | |
| message: "Hello, Foo.Bar!" | |
| }, | |
| on: { | |
| 201: function(response) { | |
| assert.equals(response.content.data.message.url, | |
| response.getHeader("Location")); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: description.resources.channels.Foo.url, | |
| headers: { | |
| content_type: description.schema["1.0"].message.mediaType, | |
| accept: description.schema["1.0"].message.mediaType, | |
| authorization: "Capability " + | |
| description.resources.channels.Foo.capability | |
| }, | |
| content: { content: "Hello, World!" }, | |
| on: { | |
| 201: function(response) { | |
| // we get back the message we publish | |
| assert.equal(response.content.data.content,"Hello, World!"); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: resources.notifications.publish.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| content_type: resources.schema["1.0"].Message | |
| accept: resources.schema["1.0"].Message, | |
| }, | |
| content: { | |
| message: { | |
| from: "Alice", | |
| to: "Bob", | |
| text: "Hello, Bob!" | |
| } | |
| }, | |
| on: { | |
| 201: function(response) { | |
| assert.equals(response.content.data.message.url, | |
| response.getHeader("Location")); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: resources.channels["foo"].publish.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| content_type: resources.schema["1.0"].Message | |
| accept: resources.schema["1.0"].Message, | |
| }, | |
| content: { | |
| message: "Hello, Foo!" | |
| }, | |
| on: { | |
| 201: function(response) { | |
| assert.equals(response.content.data.message.url, | |
| response.getHeader("Location")); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.channels["foo"].subscribe.url, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].MessageList, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.equals(response.content.data[0].message, | |
| "Hello, Foo!") | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.get({ | |
| url: resources.notifications.url, | |
| parameters: { | |
| channel: "foo" | |
| events: "create,delete" | |
| }, | |
| headers: { | |
| origin: "http://yourdomain.com", | |
| accept: resources.schema["1.0"].ChannelEvent, | |
| }, | |
| on: { | |
| 200: function(response) { | |
| assert.ok(response.content.data.name); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| shred.post({ | |
| url: description.resources.session.resources.subscriptions.url, | |
| headers: { | |
| accept: description.schema["1.0"].subscription.mediaType, | |
| content_type: description.schema["1.0"].subscription.mediaType, | |
| authorization: "Capability " + | |
| description.resources.session.resources.subscriptions.capability | |
| }, | |
| content: { | |
| events: [ "messages" ], | |
| channels: [ description.resources.channels.Foo.url ] | |
| }, | |
| on: { | |
| 201: function(response) { | |
| description.resources.subscription = response.content.data; | |
| assert.ok(description.resources.subscription.url); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment