Commit 9c87843a authored by Oskar Kalbag's avatar Oskar Kalbag

Added the app.net example. Show highlights now displays content that will not...

Added the app.net example. Show highlights now displays content that will not be in the final template at 50% opacity.
parent 7d94a541
express = require 'express'
tally = require './lib/tally.coffee'
superagent = require 'superagent'
#
# Set up Express with Tally as the templating engine.
#
app = express()
app.engine 'html', tally.__express
app.set 'views', __dirname + '/views'
#
# Simple template example (with static data)
#
# Sample data
data =
title: 'Tally sample'
......@@ -16,13 +29,6 @@ data =
{name: 'Natalie', skills: 'HTML, CSS'}
]
#
# Set up Express with Tally as the templating engine.
#
app = express()
app.engine 'html', tally.__express
app.set 'views', __dirname + '/views'
# Pure Tally call.
app.get '/', (request, response) ->
data.hybrid = no
......@@ -33,6 +39,24 @@ app.get '/hybrid', (request, response) ->
data.hybrid = yes
response.render 'hybrid.html', data
#
# App.net global timeline example.
#
app.get '/posts', (request, response) ->
superagent.get('https://alpha-api.app.net/stream/0/posts/stream/global')
.end (globalTimelineResponse) ->
if globalTimelineResponse.body
# Attach a custom function to the data to count the number of posts
globalTimelineResponse.body.numberOfPosts = ->
return this.data.length
response.render 'posts.html', globalTimelineResponse.body
else
response.render 'posts.html', {error: 'Bad response from Twitter'}
app.listen 3000
console.log 'The Tally sample is listening on port 3000…'
......
support
test
examples
*.sock
lib-cov
coverage.html
0.12.4 / 2013-02-11
==================
* fix get content-type even if can't get other headers in firefox. fixes #181
0.12.3 / 2013-02-11
==================
* add quick "progress" event support
0.12.2 / 2013-02-04
==================
* add test to check if response acts as a readable stream
* add ReadableStream in the Response prototype.
* add test to assert correct redirection when the host changes in the location header.
* add default Accept-Encoding. Closes #155
* fix req.pipe() return value of original stream for node parity. Closes #171
* remove the host header when cleaning headers to properly follow the redirection.
0.12.1 / 2013-01-10
==================
* add x-domain error handling
0.12.0 / 2013-01-04
==================
* add header persistence on redirects
0.11.0 / 2013-01-02
==================
* add .error Error object. Closes #156
* add forcing of res.text removal for FF HEAD responses. Closes #162
* add reduce component usage. Closes #90
* move better-assert dep to development deps
0.10.0 / 2012-11-14
==================
* add req.timeout(ms) support for the client
0.9.10 / 2012-11-14
==================
* fix client-side .query(str) support
0.9.9 / 2012-11-14
==================
* add .parse(fn) support
* fix socket hangup with dates in querystring. Closes #146
* fix socket hangup "error" event when a callback of arity 2 is provided
0.9.8 / 2012-11-03
==================
* add emission of error from `Request#callback()`
* add a better fix for nodes weird socket hang up error
* add PUT/POST/PATCH data support to client short-hand functions
* add .license property to component.json
* change client portion to build using component(1)
* fix GET body support [guille]
0.9.7 / 2012-10-19
==================
* fix `.buffer()` `res.text` when no parser matches
0.9.6 / 2012-10-17
==================
* change: use `this` when `window` is undefined
* update to new component spec [juliangruber]
* fix emission of "data" events for compressed responses without encoding. Closes #125
0.9.5 / 2012-10-01
==================
* add field name to .attach()
* add text "parser"
* refactor isObject()
* remove wtf isFunction() helper
0.9.4 / 2012-09-20
==================
* fix `Buffer` responses [TooTallNate]
* fix `res.type` when a "type" param is present [TooTallNate]
0.9.3 / 2012-09-18
==================
* remove __GET__ `.send()` == `.query()` special-case (__API__ change !!!)
0.9.2 / 2012-09-17
==================
* add `.aborted` prop
* add `.abort()`. Closes #115
0.9.1 / 2012-09-07
==================
* add `.forbidden` response property
* add component.json
* change emitter-component to 0.0.5
* fix client-side tests
0.9.0 / 2012-08-28
==================
* add `.timeout(ms)`. Closes #17
0.8.2 / 2012-08-28
==================
* fix pathname relative redirects. Closes #112
0.8.1 / 2012-08-21
==================
* fix redirects when schema is specified
0.8.0 / 2012-08-19
==================
* add `res.buffered` flag
* add buffering of text/*, json and forms only by default. Closes #61
* add `.buffer(false)` cancellation
* add cookie jar support [hunterloftis]
* add agent functionality [hunterloftis]
0.7.0 / 2012-08-03
==================
* allow `query()` to be called after the internal `req` has been created [tootallnate]
0.6.0 / 2012-07-17
==================
* add `res.send('foo=bar')` default of "application/x-www-form-urlencoded"
0.5.1 / 2012-07-16
==================
* add "methods" dep
* add `.end()` arity check to node callbacks
* fix unzip support due to weird node internals
0.5.0 / 2012-06-16
==================
* Added "Link" response header field parsing, exposing `res.links`
0.4.3 / 2012-06-15
==================
* Added 303, 305 and 307 as redirect status codes [slaskis]
* Fixed passing an object as the url
0.4.2 / 2012-06-02
==================
* Added component support
* Fixed redirect data
0.4.1 / 2012-04-13
==================
* Added HTTP PATCH support
* Fixed: GET / HEAD when following redirects. Closes #86
* Fixed Content-Length detection for multibyte chars
0.4.0 / 2012-03-04
==================
* Added `.head()` method [browser]. Closes #78
* Added `make test-cov` support
* Added multipart request support. Closes #11
* Added all methods that node supports. Closes #71
* Added "response" event providing a Response object. Closes #28
* Added `.query(obj)`. Closes #59
* Added `res.type` (browser). Closes #54
* Changed: default `res.body` and `res.files` to {}
* Fixed: port existing query-string fix (browser). Closes #57
0.3.0 / 2012-01-24
==================
* Added deflate/gzip support [guillermo]
* Added `res.type` (Content-Type void of params)
* Added `res.statusCode` to mirror node
* Added `res.headers` to mirror node
* Changed: parsers take callbacks
* Fixed optional schema support. Closes #49
0.2.0 / 2012-01-05
==================
* Added url auth support
* Added `.auth(username, password)`
* Added basic auth support [node]. Closes #41
* Added `make test-docs`
* Added guillermo's EventEmitter. Closes #16
* Removed `Request#data()` for SS, renamed to `send()`
* Removed `Request#data()` from client, renamed to `send()`
* Fixed array support. [browser]
* Fixed array support. Closes #35 [node]
* Fixed `EventEmitter#emit()`
0.1.3 / 2011-10-25
==================
* Added error to callback
* Bumped node dep for 0.5.x
0.1.2 / 2011-09-24
==================
* Added markdown documentation
* Added `request(url[, fn])` support to the client
* Added `qs` dependency to package.json
* Added options for `Request#pipe()`
* Added support for `request(url, callback)`
* Added `request(url)` as shortcut for `request.get(url)`
* Added `Request#pipe(stream)`
* Added inherit from `Stream`
* Added multipart support
* Added ssl support (node)
* Removed Content-Length field from client
* Fixed buffering, `setEncoding()` to utf8 [reported by stagas]
* Fixed "end" event when piping
0.1.1 / 2011-08-20
==================
* Added `res.redirect` flag (node)
* Added redirect support (node)
* Added `Request#redirects(n)` (node)
* Added `.set(object)` header field support
* Fixed `Content-Length` support
0.1.0 / 2011-08-09
==================
* Added support for multiple calls to `.data()`
* Added support for `.get(uri, obj)`
* Added GET `.data()` querystring support
* Added IE{6,7,8} support [alexyoung]
0.0.1 / 2011-08-05
==================
* Initial commit
TESTS = test/node/*.js
REPORTER = dot
all: superagent.js
test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--reporter $(REPORTER) \
--timeout 200 \
--growl \
$(TESTS)
test-cov: lib-cov
SUPERAGENT_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
lib-cov:
jscoverage lib lib-cov
superagent.js: components
component build --standalone superagent
components:
component install
superagent.min.js: superagent.js
uglifyjs --no-mangle $< > $@
test-server:
@node test/server
docs: test-docs
test-docs:
make test REPORTER=doc \
| cat docs/head.html - docs/tail.html \
> docs/test.html
clean:
rm -fr superagent.js components
.PHONY: test-cov test docs test-docs clean
# SuperAgent
SuperAgent is a small progressive __client-side__ HTTP request library, and __Node.js__ module with the same API, sporting many high-level HTTP client features. View the [docs](http://visionmedia.github.com/superagent/).
![super agent](http://f.cl.ly/items/3d282n3A0h0Z0K2w0q2a/Screenshot.png)
## Motivation
This library spawned from my frustration with jQuery's weak & inconsistent Ajax support. jQuery's API while having recently added some promise-like support, is largely static, forcing you to build up big objects containing all the header fields and options, not to mention most of the options are awkwardly named "type" instead of "method", etc. Onto examples!
The following is what you might typically do for a simple __GET__ with jQuery:
```js
$.get('/user/1', function(data, textStatus, xhr){
});
```
great, it's ok, but it's kinda lame having 3 arguments just to access something on the `xhr`. Our equivalent would be:
```js
request.get('/user/1', function(res){
});
```
the response object is an instanceof `request.Response`, encapsulating all of this information instead of throwing a bunch of arguments at you. For example we can check `res.status`, `res.header` for header fields, `res.text`, `res.body` etc.
An example of a JSON POST with jQuery typically might use `$.post()`, however once you need to start defining header fields you have to then re-write it using `$.ajax()`... so that might look like:
```js
$.ajax({
url: '/api/pet',
type: 'POST',
data: { name: 'Manny', species: 'cat' },
headers: { 'X-API-Key': 'foobar' }
}).success(function(res){
}).error(function(){
});
```
Not only is it ugly it's pretty opinionated, jQuery likes to special-case {4,5}xx, for example you cannot (easily at least) receive a parsed JSON response for say "400 Bad Request". This same request would look like this:
```js
request
.post('/api/pet')
.send({ name: 'Manny', species: 'cat' })
.set('X-API-Key', 'foobar')
.set('Accept', 'application/json')
.end(function(res){
});
```
building on the existing API internally we also provide something similar to `$.post()` for those times in life where your interactions are very basic:
```js
request.post('/api/pet', cat, function(res){
});
```
## Running node tests
Install dependencies:
$ npm install
Run em!
$ make test
## Running browser tests
Install the test server deps (nodejs / express):
$ npm install
Start the test server:
$ make test-server
Visit `localhost:3000/` in the browser.
## Browser build
The browser build of superagent is located in the `./build` directory.
## Examples:
- [agency tests](superagent/blob/master/test/node/agency.js)
- [express demo app](https://github.com/hunterloftis/component-test/blob/master/lib/users/test/controller.test.js)
## Wiki
For superagent extensions such as couchdb and oauth visit the [wiki](https://github.com/visionmedia/superagent/wiki).
## License
MIT
This diff is collapsed.
{
"name": "superagent",
"repo": "visionmedia/superagent",
"description": "awesome http requests",
"version": "0.12.4",
"keywords": [
"http",
"ajax",
"request",
"agent"
],
"scripts": [
"lib/client.js"
],
"main": "lib/client.js",
"dependencies": {
"component/emitter": "*",
"RedVentures/reduce": "*"
},
"license": "MIT"
}
{
"name": "emitter",
"description": "Event emitter",
"keywords": [
"emitter",
"events"
],
"version": "0.0.6",
"scripts": [
"index.js"
],
"repo": "https://raw.github.com/component/emitter"
}
\ No newline at end of file
/**
* Expose `Emitter`.
*/
module.exports = Emitter;
/**
* Initialize a new `Emitter`.
*
* @api public
*/
function Emitter(obj) {
if (obj) return mixin(obj);
};
/**
* Mixin the emitter properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function mixin(obj) {
for (var key in Emitter.prototype) {
obj[key] = Emitter.prototype[key];
}
return obj;
}
/**
* Listen on the given `event` with `fn`.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.on = function(event, fn){
this._callbacks = this._callbacks || {};
(this._callbacks[event] = this._callbacks[event] || [])
.push(fn);
return this;
};
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.once = function(event, fn){
var self = this;
this._callbacks = this._callbacks || {};
function on() {
self.off(event, on);
fn.apply(this, arguments);
}
fn._off = on;
this.on(event, on);
return this;
};
/**
* Remove the given callback for `event` or all
* registered callbacks.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners = function(event, fn){
this._callbacks = this._callbacks || {};
var callbacks = this._callbacks[event];
if (!callbacks) return this;
// remove all handlers
if (1 == arguments.length) {
delete this._callbacks[event];
return this;
}
// remove specific handler