-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmoves.js
More file actions
107 lines (83 loc) · 3.56 KB
/
moves.js
File metadata and controls
107 lines (83 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var _ = require('underscore')
, qs = require('querystring')
, request = require('request')
, url = require('url')
var Moves = module.exports = function(config_obj) {
if(!(this instanceof Moves)) return new Moves(config_obj)
var config = {
oauth_base: 'https://api.moves-app.com/oauth/v1'
, api_base: 'https://api.moves-app.com/api/1.1'
, authorize_url: '/authorize'
}
this.config = _.extend(config, config_obj)
this.http = request
if(!this.config.client_id) throw new Error('Missing Client ID')
}
Moves.prototype.authorize = function(options, res) {
options = options || {}
if(typeof res === 'object'
&& typeof res.header !== 'function') throw new Error('authorize requires the first parameter to be a valid node response object')
if(!options.scope) throw new Error('Scope is required')
if(!_.isArray(options.scope)) throw new Error('Scope must be an array')
var query = {
client_id: this.config.client_id
, response_type: 'code'
, scope: options.scope.join(' ')
}
if(options.state) query.state = options.state
if(options.redirect_uri) query.redirect_uri = options.redirect_uri
var auth_url = this.config.oauth_base + this.config.authorize_url + '?' + qs.stringify(query)
if(!res) return auth_url
res.header('Content-Type', 'text/html')
res.statusCode = 302
res.header('Location', auth_url)
res.end('Redirecting...')
}
Moves.prototype.token = function(code, callback) {
if(!code) throw new Error('You must include a code')
if(!this.config.client_secret) throw new Error('Missing client secret')
if(typeof callback !== 'function') throw new Error('Invalid callback')
var query = {
grant_type: 'authorization_code'
, code: code
, client_id: this.config.client_id
, client_secret: this.config.client_secret
}
if(this.config.redirect_uri) query.redirect_uri = this.config.redirect_uri
this.http.post(this.config.oauth_base + '/access_token?' + qs.stringify(query), callback)
}
Moves.prototype.refresh_token = function(token, scope, callback) {
if(typeof scope === 'function' && !callback) {
callback = scope
scope = undefined
}
if(!token) throw new Error('You must include a token')
if(!this.config.client_secret) throw new Error('Missing client secret')
if(typeof callback !== 'function') throw new Error('Invalid callback')
var query = {
grant_type: 'refresh_token'
, refresh_token: token
, client_id: this.config.client_id
, client_secret: this.config.client_secret
}
if(scope) query.scope = scope
this.http.post(this.config.oauth_base + '/access_token?' + qs.stringify(query), callback)
}
Moves.prototype.token_info = function(token, callback) {
if(!token) throw new Error('You must include a token')
var query = {
access_token: token
}
this.http.get(this.config.oauth_base + '/tokeninfo?' + qs.stringify(query), callback)
}
Moves.prototype.get = function(call, access_token, callback) {
if(!call) throw new Error('call is required. Please refer to the Moves docs <https://dev.moves-app.com/docs/api>')
if(!access_token) throw new Error('Valid access token is required')
var get_url = url.parse(this.config.api_base, true)
, call_url = url.parse(call, true)
get_url.pathname += call_url.pathname
_.extend(get_url.query, call_url.query, {
access_token: access_token
})
this.http.get(url.format(get_url), callback)
}