Skip to content

Commit

Permalink
syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoung committed Jan 6, 2012
1 parent 04cd830 commit fbc63c8
Showing 1 changed file with 64 additions and 61 deletions.
125 changes: 64 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,71 +88,74 @@ JSON.stringify your object first. Otherwise, it will be sent as `application/x-w
Example usage
-------------

var sys = require('util'),
rest = require('./restler');

rest.get('http://google.com').on('complete', function(data) {
sys.puts(data);
});

rest.get('http://twaud.io/api/v1/users/danwrong.json').on('complete', function(data) {
sys.puts(data[0].message); // auto convert to object
});

rest.get('http://twaud.io/api/v1/users/danwrong.xml').on('complete', function(data) {
sys.puts(data[0].sounds[0].sound[0].message); // auto convert to object
});

rest.post('http://user:pass@service.com/action', {
data: { id: 334 },
}).on('complete', function(data, response) {
if (response.statusCode == 201) {
// you can get at the raw response like this...
}
});

// multipart request sending a file and using https
rest.post('https://twaud.io/api/v1/upload.json', {
multipart: true,
username: 'danwrong',
password: 'wouldntyouliketoknow',
data: {
'sound[message]': 'hello from restler!',
'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, null, null, 'audio/mpeg')
}
}).on('complete', function(data) {
sys.puts(data.audio_url);
});

// create a service constructor for very easy API wrappers a la HTTParty...
Twitter = rest.service(function(u, p) {
this.defaults.username = u;
this.defaults.password = p;
}, {
baseURL: 'http://twitter.com'
}, {
update: function(message) {
return this.post('/statuses/update.json', { data: { status: message } });
}
});

var client = new Twitter('danwrong', 'password');
client.update('Tweeting using a Restler service thingy').on('complete', function(data) {
sys.p(data);
});

// the JSON post
rest.post('http://example.com/action', {
data: JSON.stringify({ id: 334 }),
}).on('complete', function(data, response) {
// you can get at the raw response like this...
});

```javascript
var sys = require('util'),
rest = require('./restler');

rest.get('http://google.com').on('complete', function(data) {
sys.puts(data);
});

rest.get('http://twaud.io/api/v1/users/danwrong.json').on('complete', function(data) {
sys.puts(data[0].message); // auto convert to object
});

rest.get('http://twaud.io/api/v1/users/danwrong.xml').on('complete', function(data) {
sys.puts(data[0].sounds[0].sound[0].message); // auto convert to object
});

rest.post('http://user:pass@service.com/action', {
data: { id: 334 },
}).on('complete', function(data, response) {
if (response.statusCode == 201) {
// you can get at the raw response like this...
}
});

// multipart request sending a file and using https
rest.post('https://twaud.io/api/v1/upload.json', {
multipart: true,
username: 'danwrong',
password: 'wouldntyouliketoknow',
data: {
'sound[message]': 'hello from restler!',
'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, null, null, 'audio/mpeg')
}
}).on('complete', function(data) {
sys.puts(data.audio_url);
});

// create a service constructor for very easy API wrappers a la HTTParty...
Twitter = rest.service(function(u, p) {
this.defaults.username = u;
this.defaults.password = p;
}, {
baseURL: 'http://twitter.com'
}, {
update: function(message) {
return this.post('/statuses/update.json', { data: { status: message } });
}
});

var client = new Twitter('danwrong', 'password');
client.update('Tweeting using a Restler service thingy').on('complete', function(data) {
sys.p(data);
});

// the JSON post
rest.post('http://example.com/action', {
data: JSON.stringify({ id: 334 }),
}).on('complete', function(data, response) {
// you can get at the raw response like this...
});
```

Running the tests
-----------------

node test/restler.js
```javascript
node test/restler.js
```

TODO
----
Expand Down

0 comments on commit fbc63c8

Please sign in to comment.