From a951072182790bd28047319052ebc950b98a4082 Mon Sep 17 00:00:00 2001 From: Dan Zajdband Date: Wed, 16 May 2012 20:32:25 -0300 Subject: [PATCH] initial commit --- Makefile | 11 +++++ Readme.md | 0 example.js | 7 +++ lib/auth.js | 25 ++++++++++ lib/endpoints.json | 78 ++++++++++++++++++++++++++++++ lib/moviedb.js | 21 ++++++++ lib/request.js | 116 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 10 ++++ 8 files changed, 268 insertions(+) create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 example.js create mode 100644 lib/auth.js create mode 100644 lib/endpoints.json create mode 100644 lib/moviedb.js create mode 100644 lib/request.js create mode 100644 package.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a46c5b5 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +ALL_TESTS = $(shell find test/ -name '*.test.js') + +run-tests: + @./node_modules/.bin/mocha \ + $(TESTFLAGS) \ + $(TESTS) + +test: + @$(MAKE) NODE_PATH=lib TESTS="$(ALL_TESTS)" run-tests + +.PHONY: test diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/example.js b/example.js new file mode 100644 index 0000000..57ea314 --- /dev/null +++ b/example.js @@ -0,0 +1,7 @@ +var mdb = require('./lib/moviedb'); + +var m = new mdb('yout api key'); + +m.searchMovie({query: 'alien'}, function(err, res){ + console.log(res); +}); diff --git a/lib/auth.js b/lib/auth.js new file mode 100644 index 0000000..cc75de2 --- /dev/null +++ b/lib/auth.js @@ -0,0 +1,25 @@ + +/** + * Module dependencies + */ + +var request = require('superagent') + , endpoints = require('./endpoints.json') + , MovieDB = module.parent.exports; + + +MovieDB.prototype.requestToken = function(fn){ + var that = this; + + request + .get(endpoints.base_url + endpoints.authentication.requestToken) + .send({api_key: that.api_key}) + .set('Accept', 'application/json') + .end(function(res){ + if(res.ok) that.token = res.body; + else throw new Error('Invalid authentication'); + + fn(); + }); +}; + diff --git a/lib/endpoints.json b/lib/endpoints.json new file mode 100644 index 0000000..a0eb0e6 --- /dev/null +++ b/lib/endpoints.json @@ -0,0 +1,78 @@ +{ + "base_url": "http://api.themoviedb.org/3/" + , "configuration" : "configuration" + , "authentication" : { + "requestToken" : "authentication/token/new" + , "sessionId" : "authentication/session/new" + } + , "methods" : { + "search" : { + "get" : { + "Movie" : "search/movie" + , "Person" : "search/person" + } + , "post" : { + } + } + , "collection" : { + "get" : { + "Info" : "collection/:id" + } + , "post" : { + } + } + , "movie" : { + "get" : { + "Info" : "movie/:id" + , "AlternativeTitles" : "movie/:id/alternative_titles" + , "Casts" : "movie/:id/casts" + , "Images" : "movie/:id/images" + , "Keywords" : "movie/:id/keywords" + , "Releases" : "movie/:id/releases" + , "Trailers" : "movie/:id/trailers" + , "Translations" : "movie/:id/translations" + , "Similar" : "movie/:id/similar_movies" + } + , "post" : { + } + } + , "person" : { + "get" : { + "Info" : "person/:id" + , "Credits" : "person/:id/credits" + , "Images" : "person/:id/images" + } + , "post" : { + } + } + , "misc" : { + "get" : { + "LatestMovie" : "latest/movie" + , "NowPlaying" : "movie/now-playing" + , "PopularMovies" : "movie/popular" + , "TopRatedMovies" : "movie/top-rated" + } + , "post" : { + "AddMovieRating" : "movie/:id/rating" + } + } + , "company" : { + "get" : { + "Info" : "company/:id" + , "Movies" : "company/:id/movies" + } + , "post" : { + } + } + , "account" : { + "get" : { + "Info" : "account" + , "FavoriteMovies" : "account/:id/favorite_movies" + , "RatedMovies" : "account/:id/rated_movies" + } + , "post" : { + "AddFavorite" : "account/:id/favorite" + } + } + } +} diff --git a/lib/moviedb.js b/lib/moviedb.js new file mode 100644 index 0000000..03bae62 --- /dev/null +++ b/lib/moviedb.js @@ -0,0 +1,21 @@ + +/* + * Exports the constructor + */ + +module.exports = function MovieDB(api_key){ + if(api_key) this.api_key = api_key; + else throw new Error('Bad api key'); +}; + +/* + * API auth + */ + +require('./auth'); + +/* + * API request + */ + +require('./request'); diff --git a/lib/request.js b/lib/request.js new file mode 100644 index 0000000..a90e402 --- /dev/null +++ b/lib/request.js @@ -0,0 +1,116 @@ + +/** + * Module dependencies + */ + +var request = require('superagent') +var endpoints = require('./endpoints.json') + , methods = endpoints.methods + , base_url = endpoints.base_url + , MovieDB = module.exports = module.parent.exports; + +var get, post; + +Object.keys(methods).forEach(function(method){ + get = methods[method].get; + post = methods[method].post; + + // GET requests + + Object.keys(methods[method].get).forEach(function(g){ + + MovieDB.prototype[method + g] = function(params, fn){ + + var that = this; + + if(!this.token || Date.now() > +new Date(this.token.expires_at)) { + + this.requestToken(function(){ + execMethod.call(that, 'get', params, methods[method].get[g], fn); + }); + + } else { + + execMethod.call(this, 'get', methods[method].get[g], fn); + + } + + }; + + }); + + // POST requests + Object.keys(methods[method].post).forEach(function(p){ + + MovieDB.prototype[method + p] = function(params, fn){ + + var that = this; + + if(!this.token || Date.now() > +new Date(this.token.expires_at)) { + + this.requestToken(function(){ + execMethod.call(that, 'post', params, methods[method].post[p], fn); + }); + + } else { + + execMethod.call(this, 'post', params, methods[method].post[p], fn); + + } + + }; + + }); + +}); + +var execMethod = function(type, params, endpoint, fn){ + + params = params || {}; + endpoint = endpoint.replace(':id', params.id); + delete params.id; + + if(type === 'post'){ + + request + .post(base_url + endpoint) + .query({api_key : this.api_key}) + .send(params) + .set('Accept', 'application/json') + .end(function(res){ + if(res.ok){ + try{ + var body = JSON.parse(res.text); + fn(null, body); + } catch(e){ + fn(e, null); + } + + } else { + fn(res.error, null); + } + }); + + } else { + + request + .get(base_url + endpoint) + .send(params) + .set('Accept', 'application/json') + .end(function(res){ + if(res.ok){ + try{ + var body = JSON.parse(res.text); + fn(null, body); + } catch(e){ + fn(e, null); + } + + } else { + fn(res.error, null); + } + }); + + } + +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..cdd20bd --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name" : "moviedb" + , "version" : "0.0.1" + , "dependencies" : { + "superagent" : "0.4.x" + } + , "devDependencies" : { + "mocha" : "1.0.x" + } +}