Skip to content

Commit

Permalink
Chuck Norris FTW
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Jun 28, 2011
1 parent cc0cf6c commit 3fdfb03
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README
@@ -0,0 +1,2 @@
Chuck is a node.js module for accessing the icndb Jokes API.
Run the demo to see what it can do.
11 changes: 11 additions & 0 deletions example.coffee
@@ -0,0 +1,11 @@
chuck = require './index'

# New chuck instance. First and last names are optional.
jokes = new chuck 'Chuck', 'Norris'

# Here's some spiffy examples.
jokes.id 1, (err, joke) -> console.log joke
jokes.random (err, joke) -> console.log joke
jokes.random 5, (err, jokes) -> console.log jokes
jokes.count (err, count) -> console.log count
jokes.categories (err, cats) -> console.log cats
27 changes: 27 additions & 0 deletions index.coffee
@@ -0,0 +1,27 @@
http = require 'http'

module.exports = class ChuckNorris
constructor: (@fname='Chuck', @lname='Norris') ->

# Joke interface.
id: (id, cb) -> @_joke 'jokes/'+id, cb
random: (c, cb) -> if not cb then @_joke 'jokes/random', c else @_jokes 'jokes/random/'+c, cb

# Utility interface
count: (cb) -> @_value 'jokes/count', cb
categories: (cb) -> @_value 'categories', cb

# Make requests of varying data depth.
_joke: (f, cb) -> @_value f, (err, data) -> if err then cb err else cb null, data.joke
_jokes: (f, cb) -> @_value f, (err, data) -> if err then cb err else cb null, data.map (val) -> val.joke
_value: (f, cb) -> @_req f, (err, data) -> if err then cb err else cb null, data.value
_req: (f, cb) ->
opts =
path:'/'+f+'?firstName='+@fname+'&lastName='+@lname
host: 'api.icndb.com'
port: 80
http.get opts, (res) ->
data = ''
res.on 'data', (chunk) => data += chunk
res.on 'end', =>
try cb null, JSON.parse data catch err then cb err
7 changes: 7 additions & 0 deletions package.json
@@ -0,0 +1,7 @@
{
"name":"chucknorris",
"version":"0.0.1",
"dependencies":{
"coffee-script":"1.1.1"
}
}

0 comments on commit 3fdfb03

Please sign in to comment.