Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

Commit

Permalink
add initial publish implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Oct 1, 2010
1 parent de5455f commit 8f017b3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/index.coffee
Expand Up @@ -13,6 +13,18 @@ class Subscription
@lease_seconds = parseInt(@lease_seconds) || 0
@bad_params = null

publish: (items, options, cb) ->
format = Subscription.formatters[options.format]
data = format items
data_len = data.length
client = ScopedClient.create(@callback).
headers(
"content-type": format.content_type
"content-length": data_len.toString()
)
client.post(data) (err, resp) =>
@check_response_for_success err, resp, cb

# Public: Checks verification of the Subscription by passing a challenge
# string and checking for the response.
#
Expand All @@ -26,10 +38,8 @@ class Subscription
client.get() (err, resp, body) =>
if body != client.options.query['hub.challenge']
cb {error: "bad challenge"}, resp
else if resp.statusCode.toString().match(/^2\d\d/)
cb null, resp
else
cb {error: "bad status"}, resp
@check_response_for_success err, resp, cb

# Public: Checks whether this Subscription is valid according to the PSHb
# spec. If the Subscription is invalid, check @bad_params for an Array of
Expand Down Expand Up @@ -98,6 +108,13 @@ class Subscription
else
@bad_params["hub.#{key}"] = true

check_response_for_success: (err, resp, cb) ->
if resp.statusCode.toString().match(/^2\d\d/)
cb null, resp
else
cb {error: "bad status"}, resp

Subscription.formatters = {}
Subscription.valid_proto = /^https?:$/
Subscription.valid_modes = ['subscribe', 'unsubscribe']
Subscription.required_keys = ['callback', 'mode', 'topic', 'verify']
Expand All @@ -106,6 +123,10 @@ Subscription.allowed_keys = [
'lease_seconds', 'secret', 'verify_token'
]

Subscription.formatters.json = (items) ->
JSON.stringify items
Subscription.formatters.json.content_type = 'application/json'

# Public: Points to a Subscription object. You can change this if you want
# to subclass Subscription with custom logic.
exports.Subscription = Subscription
Expand Down
52 changes: 52 additions & 0 deletions test/publish_test.coffee
@@ -0,0 +1,52 @@
assert = require 'assert'
http = require 'http'
url = require 'url'
query = require 'querystring'
nub = require '../src'

port = 9999
server = http.createServer (req, resp) ->
body = ''
req.on 'data', (chunk) -> body += chunk

req.on 'end', ->
req_url = url.parse req.url, true

switch req_url.query.testing
when 'json'
assert.equal "[{\"abc\":1}]", body
resp.writeHead 200
resp.end "ok"
when 'error'
resp.writeHead 500
resp.end()

req =
'hub.callback': 'http://localhost:9999?testing=json'
'hub.mode': 'subscribe'
'hub.topic': 'http://server.com/foo'
'hub.verify': 'sync'
'hub.lease_seconds': '1000'
sub = nub.subscribe(query.stringify(req))

server.listen(port)

calls = 2

# successful publishing
sub.publish [{abc: 1}], {format: 'json'}, (err, resp) ->
assert.equal null, err
done()

# errored
sub.callback = sub.callback.replace(/json/, 'error')
sub.publish [{abc: 1}], {format: 'json'}, (err, resp) ->
assert.equal 'bad status', err.error
done()

done = ->
calls -= 1
if calls == 0
server.close()

process.on 'exit', -> console.log 'done'

0 comments on commit 8f017b3

Please sign in to comment.