/
lhapp.coffee
92 lines (70 loc) · 2.59 KB
/
lhapp.coffee
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
request = require 'request'
_ = require 'underscore'
BASE_URL = "lighthouseapp.com"
getOptions = (options) ->
options = options or {}
options.format = options.format or 'json'
options.params = options.params or {}
return options
class Client
constructor: (@name, @token) ->
@request = request
getUrl: (path, options) ->
options = getOptions(options)
query = ("#{key}=#{value}" for key, value of options.params).join "&"
if query
query = "&#{query}"
return "http://#{@name}.#{BASE_URL}/#{path}.#{options.format}?_token=#{@token}#{query}"
getCallback: (options) ->
return (err, res, body) ->
if not err
body = JSON.parse(body)
options.callback(err, res, body)
get: (path, options) ->
options = getOptions(options)
url = @getUrl path, options
callback = @getCallback(options)
@request url, callback
getProjects: (callback) ->
@get 'projects', {callback: (err, res, data) ->
callback(_.map(data.projects, (p) -> p.project))
}
getProject: (id, callback) ->
@get "projects/#{id}", {callback: (err, res, data) ->
callback(data.project)
}
getChangesets: (id, callback) ->
@get "projects/#{id}/changesets", {callback: (err, res, data) ->
callback(_.map(data.changesets, (c) -> c.changeset))
}
getTickets: (id, callback, limit=30, page=1, query='') ->
params = {"limit": limit, "page": page}
if query
params.q = query
@get "projects/#{id}/tickets", {
callback: (err, res, data) ->
callback(_.map(data.tickets, (t) -> t.ticket))
"params": params
}
getTicket: (id, number, callback) ->
@get "projects/#{id}/tickets/#{number}", {callback: (err, res, data) ->
callback(data.ticket)
}
getMilestones: (id, callback) ->
@get "projects/#{id}/milestones", {callback: (err, res, data) ->
callback(_.map(data.milestones, (m) -> m.milestone))
}
getMilestone: (id, milestoneId, callback) ->
@get "projects/#{id}/milestones/#{milestoneId}", {callback: (err, res, data) ->
callback(data.milestone)
}
getProfile: (callback) ->
@get "profile", {callback: (err, res, data) ->
callback(data.user)
}
getUser: (id, callback) ->
@get "users/#{id}", {callback: (err, res, data) ->
callback(data.user)
}
exports.BASE_URL = BASE_URL
exports.Client = Client