Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name":"chucknorris", | ||
"version":"0.0.1", | ||
"dependencies":{ | ||
"coffee-script":"1.1.1" | ||
} | ||
} |