From f68c39db2c8b6702215d8c880c38950c3e75d907 Mon Sep 17 00:00:00 2001 From: Verdi Ergun Date: Fri, 6 Sep 2013 22:34:25 -0700 Subject: [PATCH 1/3] Initial commit. --- README.md | 63 ++++++++++++++++++++++++++++++++++-------------- package.json | 15 ++++++------ test/register.js | 14 ----------- 3 files changed, 53 insertions(+), 39 deletions(-) delete mode 100644 test/register.js diff --git a/README.md b/README.md index 7c6e466..74d3ccd 100755 --- a/README.md +++ b/README.md @@ -1,30 +1,57 @@ ![image_squidhome@2x.png](http://i.imgur.com/RIvu9.png) -# BoilerplateAdapter +# YelpAdapter -This template exists to make it easier for you to get started writing an official adapter for Sails.js. +This adapter extends the node-yelp module to Sails.js. (https://github.com/olalonde/node-yelp). +## Installation -## Getting started -It's usually pretty easy to add your own adapters for integrating with proprietary systems or existing open APIs. For most things, it's as easy as `require('some-module')` and mapping the appropriate methods to match waterline semantics. To get started: +This isn't released as an npm module so you have to download YelpAdapter.js and place it in your `api/adapters` directory. -1. Fork this repository -2. Set up your README and package.json file. Sails.js adapter module names are of the form sails-*, where * is the name of the datastore or service you're integrating with. -3. Build your adapter. +## Setup -## How to test your adapter -1. Run `npm link` in this adapter's directory -2. Clone the sails.js core and modify the tests to use your new adapter. -3. Run `npm link sails-boilerplate` -4. From the sails.js core directory, run `npm test`. +Add your yelp credentials to config/application.yml -## Submitting your adapter -1. Do a pull request to this repository (make sure you attribute yourself as the author set the license in the package.json to "MIT") Please let us know about any special instructions for usage/testing. -2. We'll run the tests one last time. If there are any issues, we'll let you know. -3. When it's ready, we'll update the documentation with information about your new adapter -4. Then we'll tweet and post about it on our blog, adoring you with lavish praises. -5. Mike will send you jelly beans. +``` +development: + yelp: + consumer_key: "your_consumer_key" + consumer_secret: "your_consumer_secret" + token: "your_token" + token_secret: "your_token_secret" +``` +Then require them in config/application.js +``` +require('js-yaml'); +global.NODE_ENV = process.env.ENV || 'development' +global.appConfig = require('./local.yml')[NODE_ENV] +``` + +## Usage + +Create a YelpBusiness model hooked up to the yelp adapter: + +// api/models/YelpBusiness.js + +``` +module.exports = { + adapter: 'yelp' +}; +``` + +Then you can use it: +``` +YelpBusiness.business("yelp-san-francisco", function(error, data) { + console.log(error); + console.log(data); +}); + +YelpBusiness.search("Tacos", "San Francisco, CA", function(error, data) { + console.log(error); + console.log(data); +}); +``` ## About Sails.js and Waterline http://SailsJs.com diff --git a/package.json b/package.json index a9f0670..c21faf5 100755 --- a/package.json +++ b/package.json @@ -1,26 +1,27 @@ { - "name": "sails-adapter-boilerplate", + "name": "sails-yelp", "version": "0.0.1", - "description": "Boilerplate adapter for Sails.js", - "main": "BoilerplateAdapter.js", + "description": "Adapter for the Yelp API", + "main": "YelpAdapter.js", "scripts": { "test": "echo \"Adapter should be tested using Sails.js core.\" && exit 1" }, "repository": { "type": "git", - "url": "https://github.com/balderdashy/sails-adapter-boilerplate.git" + "url": "https://github.com/vergun/sails-yelp.git" }, "keywords": [ "orm", "waterline", "sails", "sailsjs", - "sails.js" + "sails.js", + "yelp" ], - "author": "Your name here", + "author": "Verdi Ergun", "license": "MIT", "readmeFilename": "README.md", "dependencies": { - "async": "0.1.22" + "yelp": "~0.1.1" } } diff --git a/test/register.js b/test/register.js deleted file mode 100644 index b4fcbb7..0000000 --- a/test/register.js +++ /dev/null @@ -1,14 +0,0 @@ -describe('registerCollection', function () { - - it('should not hang or encounter any errors', function (cb) { - var adapter = require('../index.js'); - adapter.registerCollection({ - identity: 'foo' - }, cb); - }); - - // e.g. - // it('should create a mysql connection pool', function () {}) - // it('should create an HTTP connection pool', function () {}) - // ... and so on. -}); \ No newline at end of file From 2a82ea6199b16ed2f9791488c82b88bf2e8b515a Mon Sep 17 00:00:00 2001 From: Verdi Ergun Date: Fri, 6 Sep 2013 22:35:16 -0700 Subject: [PATCH 2/3] YelpAdapter --- YelpAdapter.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 YelpAdapter.js diff --git a/YelpAdapter.js b/YelpAdapter.js new file mode 100644 index 0000000..359995f --- /dev/null +++ b/YelpAdapter.js @@ -0,0 +1,87 @@ +/*--------------------------------------------------------------- + :: sails-yelp + -> adapter +---------------------------------------------------------------*/ + +// Module dependencies and credentials assignment +var yelp = require('yelp').createClient({ + consumer_key: appConfig.yelp.consumer_key || '', + consumer_secret: appConfig.yelp.consumer_secret || '', + token: appConfig.yelp.token || '', + token_secret: appConfig.yelp.token_secret || '' +}) + +/*--------------------------------------------------------------- +// Set up global.appConfig variable for yelp api keys +// in config/local.yml with format such as: + +development: + yelp: + consumer_key: "your_consumer_key" + consumer_secret: "your_consumer_secret" + token: "your_token" + token_secret: "your_token_secret" + +// config/application.js.... + +// require('js-yaml'); +// global.NODE_ENV = process.env.ENV || 'development' +// global.appConfig = require('./local.yml')[NODE_ENV] + +---------------------------------------------------------------*/ + + +// Define the adapter +var adapter = { + + // Search the Yelp Search API for a search term and/or location + // e.g. "term: tacos, location: San Francisco, CA" + // See http://www.yelp.com/developers/documentation/v2/search_api + search: function(collectionName, term, location, callback) { + + // If location is not a function transpose location and callback + if (location && typeof(location) === "function") callback = location + + // If term is not a function transpose term and callback + if (term && typeof(term) === "function") callback = term + + // Error states for not providing term or location is provided + // by the state when location is undefined as both are overwritten + var err = [{ err: { message: "Must provide a search term or a location."}}] + if (typeof(location) === "undefined") return callback(err) + + // Search, return errors and data from search + yelp.search({term: term, location: location}, function(error, data) { + + //Callback is present return errors and data + if (callback && typeof(callback) === "function") return callback(error, data); + + }); + + }, + + // Search the Yelp Business API for a specific business name + // e.g. ('Fred's tacos') + // See http://www.yelp.com/developers/documentation/v2/business + business: function(collectionName, term, callback) { + + // If term is a function then a business search term name + // wasn't supplied and respond back with an error + if (term && typeof(term) === "function") { + var err = [{ err: { message: "Must provide a business yelp ID."}}] + return callback(err) + } + + // Otherwise, search for the business name through the business API + yelp.business(term, function(error, data) { + + //Callback is present return errors and data + if (callback && typeof(callback) === "function") return callback(error, data); + + }); + + } + +}; + +module.exports = adapter; From 247a8485439de3962664991ddf84f2f248b22abd Mon Sep 17 00:00:00 2001 From: Verdi Ergun Date: Fri, 6 Sep 2013 22:36:03 -0700 Subject: [PATCH 3/3] Removed index --- index.js | 223 ------------------------------------------------------- 1 file changed, 223 deletions(-) delete mode 100644 index.js diff --git a/index.js b/index.js deleted file mode 100644 index 32ed3d4..0000000 --- a/index.js +++ /dev/null @@ -1,223 +0,0 @@ -/*--------------------------------------------------------------- - :: sails-boilerplate - -> adapter ----------------------------------------------------------------*/ - -var async = require('async'); - -var adapter = module.exports = { - - // Set to true if this adapter supports (or requires) things like data types, validations, keys, etc. - // If true, the schema for models using this adapter will be automatically synced when the server starts. - // Not terribly relevant if not using a non-SQL / non-schema-ed data store - syncable: false, - - // Including a commitLog config enables transactions in this adapter - // Please note that these are not ACID-compliant transactions: - // They guarantee *ISOLATION*, and use a configurable persistent store, so they are *DURABLE* in the face of server crashes. - // However there is no scheduled task that rebuild state from a mid-step commit log at server start, so they're not CONSISTENT yet. - // and there is still lots of work to do as far as making them ATOMIC (they're not undoable right now) - // - // However, for the immediate future, they do a great job of preventing race conditions, and are - // better than a naive solution. They add the most value in findOrCreate() and createEach(). - // - // commitLog: { - // identity: '__default_mongo_transaction', - // adapter: 'sails-mongo' - // }, - - // Default configuration for collections - // (same effect as if these properties were included at the top level of the model definitions) - defaults: { - - // For example: - // port: 3306, - // host: 'localhost' - - // If setting syncable, you should consider the migrate option, - // which allows you to set how the sync will be performed. - // It can be overridden globally in an app (config/adapters.js) and on a per-model basis. - // - // drop => Drop schema and data, then recreate it - // alter => Drop/add columns as necessary, but try - // safe => Don't change anything (good for production DBs) - migrate: 'alter' - }, - - // This method runs when a model is initially registered at server start time - registerCollection: function(collection, cb) { - - cb(); - }, - - - // The following methods are optional - //////////////////////////////////////////////////////////// - - // Optional hook fired when a model is unregistered, typically at server halt - // useful for tearing down remaining open connections, etc. - teardown: function(cb) { - cb(); - }, - - - // REQUIRED method if integrating with a schemaful database - define: function(collectionName, definition, cb) { - - // Define a new "table" or "collection" schema in the data store - cb(); - }, - // REQUIRED method if integrating with a schemaful database - describe: function(collectionName, cb) { - - // Respond with the schema (attributes) for a collection or table in the data store - var attributes = {}; - cb(null, attributes); - }, - // REQUIRED method if integrating with a schemaful database - drop: function(collectionName, cb) { - // Drop a "table" or "collection" schema from the data store - cb(); - }, - - // Optional override of built-in alter logic - // Can be simulated with describe(), define(), and drop(), - // but will probably be made much more efficient by an override here - // alter: function (collectionName, attributes, cb) { - // Modify the schema of a table or collection in the data store - // cb(); - // }, - - - // REQUIRED method if users expect to call Model.create() or any methods - create: function(collectionName, values, cb) { - // Create a single new model specified by values - - // Respond with error or newly created model instance - cb(null, values); - }, - - // REQUIRED method if users expect to call Model.find(), Model.findAll() or related methods - // You're actually supporting find(), findAll(), and other methods here - // but the core will take care of supporting all the different usages. - // (e.g. if this is a find(), not a findAll(), it will only send back a single model) - find: function(collectionName, options, cb) { - - // ** Filter by criteria in options to generate result set - - // Respond with an error or a *list* of models in result set - cb(null, []); - }, - - // REQUIRED method if users expect to call Model.update() - update: function(collectionName, options, values, cb) { - - // ** Filter by criteria in options to generate result set - - // Then update all model(s) in the result set - - // Respond with error or a *list* of models that were updated - cb(); - }, - - // REQUIRED method if users expect to call Model.destroy() - destroy: function(collectionName, options, cb) { - - // ** Filter by criteria in options to generate result set - - // Destroy all model(s) in the result set - - // Return an error or nothing at all - cb(); - }, - - - - // REQUIRED method if users expect to call Model.stream() - stream: function(collectionName, options, stream) { - // options is a standard criteria/options object (like in find) - - // stream.write() and stream.end() should be called. - // for an example, check out: - // https://github.com/balderdashy/sails-dirty/blob/master/DirtyAdapter.js#L247 - - } - - - - /* - ********************************************** - * Optional overrides - ********************************************** - - // Optional override of built-in batch create logic for increased efficiency - // otherwise, uses create() - createEach: function (collectionName, cb) { cb(); }, - - // Optional override of built-in findOrCreate logic for increased efficiency - // otherwise, uses find() and create() - findOrCreate: function (collectionName, cb) { cb(); }, - - // Optional override of built-in batch findOrCreate logic for increased efficiency - // otherwise, uses findOrCreate() - findOrCreateEach: function (collectionName, cb) { cb(); } - */ - - - /* - ********************************************** - * Custom methods - ********************************************** - - //////////////////////////////////////////////////////////////////////////////////////////////////// - // - // > NOTE: There are a few gotchas here you should be aware of. - // - // + The collectionName argument is always prepended as the first argument. - // This is so you can know which model is requesting the adapter. - // - // + All adapter functions are asynchronous, even the completely custom ones, - // and they must always include a callback as the final argument. - // The first argument of callbacks is always an error object. - // For some core methods, Sails.js will add support for .done()/promise usage. - // - // + - // - //////////////////////////////////////////////////////////////////////////////////////////////////// - - - // Any other methods you include will be available on your models - foo: function (collectionName, cb) { - cb(null,"ok"); - }, - bar: function (collectionName, baz, watson, cb) { - cb("Failure!"); - } - - - // Example success usage: - - Model.foo(function (err, result) { - if (err) console.error(err); - else console.log(result); - - // outputs: ok - }) - - // Example error usage: - - Model.bar(235, {test: 'yes'}, function (err, result){ - if (err) console.error(err); - else console.log(result); - - // outputs: Failure! - }) - - */ - - -}; - -////////////// ////////////////////////////////////////// -////////////// Private Methods ////////////////////////////////////////// -////////////// ////////////////////////////////////////// \ No newline at end of file