Permalink
Browse files

Merge pull request #15 from joeandaverde/master

Added ability to persist users in memory and included tests
  • Loading branch information...
2 parents fb0fbe4 + 1f5c571 commit 2ed114631f1e97aca66fc76a34867ce5dd687555 @joeandaverde joeandaverde committed Mar 11, 2012
View
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.4
View
@@ -0,0 +1,11 @@
+TESTS = $(shell find test -name "*.spec.coffee")
+REPORTER = spec
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter $(REPORTER) \
+ --globals i \
+ $(TESTS)
+
+.PHONY: test
View
@@ -1,6 +1,8 @@
#Stalkqr
-An open source project by the NodeKC group.
+[![Build Status](https://secure.travis-ci.org/adunkman/stalkqr.png?branch=master)](http://travis-ci.org/adunkman/stalkqr)
+
+An open source project by the NodeKC group.
http://nodekc.org
@@ -12,7 +14,7 @@ This project will generate URL's that can be used by others to add your social c
###Set up your environment variables
-#### UNIX
+#### UNIX
export KEY=value
#### Windows
@@ -22,7 +24,7 @@ This project will generate URL's that can be used by others to add your social c
heroku config:add KEY=value
##Twitter API Key
-
+
TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET
@@ -39,5 +41,5 @@ https://github.com/account/applications Once again, it's important to set the ca
CALLBACK_BASE_URL
-You will need to set up your base callback url. This is the root url to where the site is hosted. Examples include: http://localhost:3000 or http://hazy-mist-1110.herokuapp.com
+You will need to set up your base callback url. This is the root url to where the site is hosted. Examples include: http://localhost:3000 or http://hazy-mist-1110.herokuapp.com
View
@@ -6,7 +6,7 @@ app = express.createServer()
port = process.env.PORT || 3000
sessionSecret = process.env.SESSION_SECRET || 'stalkqr'
-controllers =
+controllers =
authentication: require './controllers/authentication'
site: require './controllers/site'
code: require './controllers/code'
@@ -22,12 +22,12 @@ app.configure ->
app.set 'view engine', 'jade'
app.configure 'development', ->
- app.use stylus.middleware
- src: path.join __dirname, 'public'
-
+ app.use stylus.middleware
+ src: path.join __dirname, 'public'
+
app.configure 'production', ->
- app.use stylus.middleware
- src: path.join __dirname, 'public'
+ app.use stylus.middleware
+ src: path.join __dirname, 'public'
compress: true
controllers.authentication.use app
View
@@ -0,0 +1,14 @@
+StoredCollection = require '../data/storedCollection'
+
+class Events extends StoredCollection
+ constructor: () -> super
+
+ find: (criteria, callback) ->
+ super(Events._collectionName, criteria, callback)
+
+ findByUser: (user, callback) ->
+ userId = if user.attributes?._id? then user.attributes._id else user
+ @find({ user: userId }, callback)
+
+Events._collectionName = 'events'
+module.exports = Events
View
@@ -0,0 +1,10 @@
+StoredCollection = require '../data/storedCollection'
+
+class Tags extends StoredCollection
+ constructor: () -> super
+
+ find: (criteria, callback) ->
+ super(Tags._collectionName, criteria, callback)
+
+Tags._collectionName = 'tags'
+module.exports = Tags
View
@@ -0,0 +1,23 @@
+StoredCollection = require '../data/storedCollection'
+
+class Users extends StoredCollection
+ constructor: () -> super
+
+ find_by_service: (provider, id, callback) ->
+ @find { provider: provider, id: id }, callback
+
+ find: () ->
+ if arguments.length is 1
+ criteria = {}
+ [callback] = arguments
+
+ if arguments.length is 2
+ [criteria, callback] = arguments
+
+ super(Users._collectionName, criteria, callback)
+
+ all: (callback) ->
+ @find(callback)
+
+Users._collectionName = 'users'
+module.exports = Users
View
@@ -1,7 +1,7 @@
url = require 'url'
GitHubStrategy = require('passport-github').Strategy
TwitterStrategy = require('passport-twitter').Strategy
-user = require '../models/user'
+Users = require '../collections/users'
TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY || 'notvalidkey'
TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET || 'notvalidkey'
@@ -12,10 +12,14 @@ GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET || 'notvalidkey'
GITHUB_CALLBACK_URL = url.resolve process.env.CALLBACK_BASE_URL, '/auth/github/callback'
twitterDone = (accessToken, tokenSecret, profile, callback) ->
- user.find_by_service 'twitter', profile.id, callback
+ authDone 'twitter', profile, callback
githubDone = (accessToken, tokenSecret, profile, callback) ->
- user.find_by_service 'github', profile.id, callback
+ authDone 'github', profile, callback
+
+authDone = (provider, profile, callback) ->
+ users = new Users()
+ users.find_by_service provider, profile, callback
twitter = new TwitterStrategy
consumerKey: TWITTER_CONSUMER_KEY
@@ -31,4 +35,5 @@ github = new GitHubStrategy
module.exports =
twitter: twitter
- github: github
+ github: github
+
@@ -0,0 +1,40 @@
+class InMemoryDataAdapter
+ constructor: () ->
+ @items = {}
+
+ save: (collection, attributes, callback) =>
+ @items[collection] or= []
+ if attributes._id? then @update(collection, attributes, callback)
+ else @insert(collection, attributes, callback)
+
+ find: (collection, criteria, callback) =>
+ results = []
+
+ for item in @items[collection] || []
+ allTrue = true
+
+ for name, value of criteria
+ if item[name] isnt value
+ allTrue = false
+
+ if allTrue
+ results.push(item)
+
+ callback(null, results)
+
+ insert: (collection, attributes, callback) ->
+ attributes = copy(attributes)
+ attributes._id = @items[collection].length
+ @items[collection].push(attributes)
+ callback(null, attributes)
+
+ update: (collection, attributes, callback) ->
+ attributes = copy(attributes)
+ id = attributes._id
+ @items[collection][id] = attributes
+ callback(null, attributes)
+
+copy = (object) ->
+ JSON.parse(JSON.stringify(object))
+
+module.exports = InMemoryDataAdapter
@@ -0,0 +1,16 @@
+InMemoryDataAdapter = require './adapters/inMemory'
+
+class StoredCollection
+ constructor: () ->
+ @_adapter = new InMemoryDataAdapter()
+
+ find: () ->
+ if arguments.length is 2
+ criteria = {}
+ [collection, callback] = arguments
+
+ if arguments.length is 3
+ [collection, criteria, callback] = arguments
+ @_adapter.find(collection, criteria, callback)
+
+module.exports = StoredCollection
View
@@ -0,0 +1,10 @@
+InMemoryDataAdapter = require './adapters/inMemory'
+
+class StoredObject
+ constructor: () ->
+ @_adapter = new InMemoryDataAdapter()
+
+ save: (collection, attributes, callback) ->
+ @_adapter.save(collection, attributes, callback)
+
+module.exports = StoredObject
View
@@ -1,37 +1,14 @@
-event_store = {}
+Model = require './model'
+Events = require '../collections/events'
-findEventsByUserId = (user_id) ->
- event_store[user_id] or []
+class Event extends Model
+ constructor: (attributes = {}) ->
+ super
+ @attributes._id or= undefined
+ @attributes.name or= undefined
+ @attributes.codes or= []
-Event = (id, name) ->
- this.name = name
- this.id = id
- this.codes = []
- return
+ save: (callback) ->
+ super(Events._collectionName, callback)
-Event.prototype.addCodes = (new_codes, callback) ->
- this.codes = this.codes.concat new_codes
- callback null, this
-
-createEvent = (user_id, event_name, callback) ->
- events = findEventsByUserId user_id
- event = new Event events.length, event_name
- events.push event
- event_store[user_id] = events
- callback null, event
-
-findFirst = (user_id, filter) ->
- result = findEventsByUserId(user_id).filter filter
- result[0] || null
-
-findByName = (user_id, event_name, callback) ->
- callback null, findFirst user_id, (e) ->
- e.name == event_name
-
-list = (user_id, callback) ->
- callback null, findEventsByUserId user_id
-
-module.exports =
- list: list
- findByName: findByName
- create: createEvent
+module.exports = Event
View
@@ -0,0 +1,18 @@
+clone = require 'clone'
+StoredObject = require '../data/storedObject'
+
+class Model extends StoredObject
+ constructor: (attributes) ->
+ super
+ @attributes = clone(attributes) || {}
+
+ save: (collection, callback) ->
+ super(collection, @toJSON(), (err, attributes) =>
+ @attributes = attributes if not err?
+ callback(err, attributes) if callback?
+ )
+
+ toJSON: () ->
+ clone(@attributes)
+
+module.exports = Model
View
@@ -0,0 +1,17 @@
+Model = require './model'
+Code = require './codes'
+Tags = require '../collections/tags'
+
+class Tag extends Model
+ constructor: (attributes = {}) ->
+ super
+ @attributes._id or= undefined
+ @attributes.user or= undefined
+ @attributes.event or= undefined
+ @attributes.generatedOn or= new Date()
+ @attributes.code or= Code.generate()
+
+ save: (callback) ->
+ super(Tags._collectionName, callback)
+
+module.exports = Tag
View
@@ -1,6 +1,33 @@
-module.exports =
- find_by_id: (id, callback) ->
- callback null, id: id
+Model = require './model'
+Users = require '../collections/users'
- find_by_service: (service, service_id, callback) ->
- callback null, id: service_id
+defaults =
+ slugs: []
+ credentials: []
+ handles: []
+
+class User extends Model
+ constructor: (attributes = defaults) ->
+ super
+ @attributes._id or= undefined
+ @attributes.name or= undefined
+ @attributes.userSince or= undefined
+ @attributes.slugs or= []
+ @attributes.handles or= []
+ @attributes.credentials or= []
+
+ setCredential: (provider, id) ->
+ existingAttributes = @attributes.credentials.filter (c) ->
+ c.provider.toLowerCase().trim() == provider.toLowerCase().trim()
+
+ if existingAttributes.length > 0
+ existingAttributes[0].id = id
+ else
+ @attributes.credentials.push
+ provider: provider
+ id: id
+
+ save: (callback) ->
+ super(Users._collectionName, callback)
+
+module.exports = User
View
@@ -1,9 +1,10 @@
{
- "name": "node-kc",
+ "name": "node-kc-stalkqr",
"version": "0.0.1",
"dependencies": {
"express": "~v2.5.6",
"coffee-script": "~v1.2.0",
+ "clone": "v0.0.4",
"jade": "~v0.20.0",
"stylus": "~v0.22.5",
"passport": "~v0.1.6",
@@ -12,6 +13,11 @@
},
"devDependencies": {
- "vows": "~v0.6.1"
+ "should": "~v0.5.1",
+ "mocha": "v0.13.0"
+ },
+
+ "scripts": {
+ "test": "make test"
}
-}
+}
Oops, something went wrong.

0 comments on commit 2ed1146

Please sign in to comment.