Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

YelpAdapter for node-yelp #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
87 changes: 87 additions & 0 deletions YelpAdapter.js
Original file line number Diff line number Diff line change
@@ -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;
223 changes: 0 additions & 223 deletions index.js

This file was deleted.

Loading