Permalink
Browse files

Initial commit

  • Loading branch information...
meritt committed Jun 10, 2012
0 parents commit 3026e9227da0955a7971c67c44c0c161ac04b104
Showing with 356 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +13 −0 Cakefile
  3. +67 −0 README.md
  4. +43 −0 examples/get-access-token.coffee
  5. +11 −0 examples/token-auth.coffee
  6. +11 −0 examples/views/login.ejs
  7. +13 −0 examples/views/result.ejs
  8. +103 −0 lib/foursquare.js
  9. +25 −0 package.json
  10. +67 −0 src/foursquare.coffee
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules/*
+examples/_*.coffee
@@ -0,0 +1,13 @@
+{print} = require 'util'
+{spawn} = require 'child_process'
+
+build = (callback) ->
+ coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
+
+ coffee.stderr.on 'data', (data) ->
+ process.stderr.write data.toString()
+
+ coffee.stdout.on 'data', (data) ->
+ print data.toString()
+
+task 'build', 'Build lib/ from src/', -> build()
@@ -0,0 +1,67 @@
+# 4sq
+
+A Node.JS wrapper for [Foursquare API](https://developer.foursquare.com/overview/).
+
+How to use with JavaScript
+--------------------------
+
+```javascript
+var foursquare = require('4sq');
+
+var fsq = new foursquare({token: 'OAuth2 access_token'});
+
+fsq.checkins('self', {limit: 5}, function(error, data) {
+ if (error) {
+ throw new Error(error);
+ }
+
+ console.log(data);
+});
+```
+
+Or with CoffeeScript
+--------------------
+
+```coffeescript
+foursquare = require '4sq'
+
+fsq = new foursquare token: 'OAuth2 access_token'
+
+fsq.checkins 'self', limit: 5, (error, data) ->
+ throw new Error error if error
+ console.log data
+```
+
+Install with NPM
+----------------
+
+```
+npm install 4sq
+```
+
+How to retrieve Foursquare OAuth2 access_token
+------------------------------------------
+
+Grab latest source code and install all dev dependencies
+
+```
+npm link
+```
+
+Change your host, client id, client secret in [examples/get-access-token.coffee](http://github.com/meritt/node-4sq/blob/master/examples/get-access-token.coffee) and after that run [examples/get-access-token.coffee](http://github.com/meritt/node-4sq/blob/master/examples/get-access-token.coffee)
+
+```
+coffee examples/get-access-token.coffee
+```
+
+API
+---
+
+* user (*user id*, *params*, *callback*)
+* checkins (*user id*, *params*, *callback*)
+* badges (*user id*, *params*, *callback*)
+
+Author
+------
+
+* Alexey Simonenko, dwarfman@gmail.com
@@ -0,0 +1,43 @@
+express = require 'express'
+everyauth = require 'everyauth'
+
+everyauth.debug = true;
+
+options =
+ host: 'http://localhost:3000' # change this
+ id: '-- client id --' # change this
+ secret: '-- client secret --' # change this
+
+current = token = null
+
+everyauth.foursquare.myHostname options.host
+everyauth.foursquare.redirectPath '/'
+everyauth.foursquare.appId options.id
+everyauth.foursquare.appSecret options.secret
+everyauth.foursquare.findOrCreateUser (session, accessToken, accessTokenExtra, user) ->
+ [token, current] = [accessToken, user]
+
+app = express.createServer()
+
+app.use express.bodyParser()
+app.use express.cookieParser()
+app.use express.session secret: 'node-4sq'
+app.use everyauth.middleware()
+
+app.set 'views', __dirname + '/views'
+app.set 'view engine', 'ejs'
+app.set 'view options', layout: false
+
+app.get '/', (request, response) ->
+ if not request.loggedIn
+ response.render 'login'
+ else
+ response.render 'result'
+ id: current.id
+ name: current.firstName + ' ' + current.lastName
+ token: token
+
+everyauth.helpExpress app
+
+app.listen 3000
+console.log 'Go to ' + options.host
@@ -0,0 +1,11 @@
+###
+ Retrieve foursquare checkins with OAuth2 access_token
+###
+
+foursquare = require('./../lib/foursquare')
+
+fsq = new foursquare token: 'OAuth2 access_token'
+
+fsq.checkins 'self', limit: 5, (error, data) ->
+ throw new Error error if error
+ console.log data
@@ -0,0 +1,11 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Foursquare AccessToken</title>
+
+<body>
+
+ <section style="width:300px;margin:30px auto 0 auto;border-radius:10px;background-color:#ffc;height:120px;padding-top:90px;text-align:center">
+ <a href="/auth/foursquare">Login with Foursquare</a>
+ </section>
@@ -0,0 +1,13 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Foursquare AccessToken</title>
+
+<body>
+
+ <section style="width:600px;margin:30px auto 0 auto;border-radius:10px;background-color:#ffc;height:170px;padding-top:40px;text-align:center">
+ Hi, <%= name %>
+ <br><br>your access token is<br><b><%= token %></b>
+ <br><br>your id is<br><b><%= id %></b>
+ </section>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,25 @@
+{
+ "name": "4sq"
+ , "version": "0.0.1"
+ , "homepage": "http://github.com/meritt/node-4sq"
+ , "author": "Alexey Simonenko <alexey@simonenko.su>"
+ , "description": "Wrapper for the Foursquare API"
+ , "keywords": ["foursquare", "4sq", "api", "wrapper"]
+ , "repository": {
+ "type": "git"
+ , "url": "http://github.com/meritt/node-4sq.git"
+ }
+ , "main": "./lib/foursquare"
+ , "engines": {
+ "node": ">=0.6.0 <0.7.0"
+ }
+ , "dependencies": {
+ "request": ">=2.9.200"
+ }
+ , "devDependencies": {
+ "coffee-script": ">=1.3.0"
+ , "everyauth": ">=0.2.32"
+ , "express": ">=2.5.9"
+ , "ejs": ">=0.7.1"
+ }
+}
@@ -0,0 +1,67 @@
+###
+ node-4sq 0.0.1
+ (c) 2012 Alexey Simonenko, Serenity LLC.
+###
+
+# Constructor
+xhr = require 'request'
+qs = require 'querystring'
+
+class Foursquare
+ apihost = 'https://api.foursquare.com'
+ version = 'v2'
+
+ constructor: (@options = {}) ->
+ date = new Date()
+ month = date.getMonth() + 1
+
+ options.date = date.getFullYear()
+ options.date += (if month < 10 then '0' else '') + month
+ options.date += (if date.getDate() < 10 then '0' else '') + date.getDate()
+
+ user: (userid, params, fn) ->
+ userid = 'self' if not userid or parseInt(userid, 10) <= 0
+
+ if isFunction params
+ fn = params
+ params = {}
+
+ @scheme = "/users/#{userid}"
+ request @, params, fn
+
+ checkins: (userid, params, fn) ->
+ userid = 'self' if not userid or parseInt(userid, 10) <= 0
+
+ if isFunction params
+ fn = params
+ params = {}
+
+ @scheme = "/users/#{userid}/checkins"
+ request @, params, fn
+
+ badges: (userid, params, fn) ->
+ userid = 'self' if not userid or parseInt(userid, 10) <= 0
+
+ if isFunction params
+ fn = params
+ params = {}
+
+ @scheme = "/users/#{userid}/badges"
+ request @, params, fn
+
+ request = (self, query = {}, fn = ->) ->
+ query.v = self.options.date
+ query = qs.stringify query
+
+ params = url: "#{apihost}/#{version}#{self.scheme}?oauth_token=#{self.options.token}&#{query}"
+
+ xhr params, (error, request, body) ->
+ body = JSON.parse body
+ error = body.meta if body.meta.code isnt 200
+ data = if body.response? then body.response else null
+
+ fn.call self, error, data
+
+ isFunction = (object) -> '[object Function]' is toString.call object
+
+module.exports = Foursquare

0 comments on commit 3026e92

Please sign in to comment.