-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaxle.coffee
191 lines (136 loc) · 4.75 KB
/
axle.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
_ = require "lodash"
{ Client } = require "./lib/client"
class exports.AxleObject
constructor: ( @client, @id, @data ) ->
_.extend this, @data
# 'fancy' dates
for type in [ "createdAt", "updatedAt" ]
if seconds = this[type]
this[type] = new Date seconds
request: ( args... ) -> @client.request args...
save: ( cb ) ->
options =
method: "POST"
body: JSON.stringify( @data )
return @request @url(), options, ( err, meta, results ) ->
# repopulate the data for this item
@data = results
_.extend this, @data
return cb err if err
return cb null, meta, results
update: ( new_details, cb ) ->
options =
method: "PUT"
body: JSON.stringify( new_details )
@request @url(), options, ( err, meta, details ) =>
return cb err if err
new_thing = details.new
old_thing = details.old
# repopulate the data for this item
@data = new_thing
_.extend this, @data
return cb null, meta, details
stats: ( options, cb ) ->
return @request "#{ @url() }/stats", options, cb
delete: ( cb ) ->
options =
method: "DELETE"
body: {}
return @request @url(), options, cb
class KeyHolder extends exports.AxleObject
linkKey: ( key_id, cb ) ->
options =
method: "PUT"
body: {}
@request "#{ @url() }/linkkey/#{ key_id }", options, ( err, meta, res ) =>
return cb err if err
return cb null, meta, @client.newKey( key_id, res )
unlinkKey: ( key_id, cb ) ->
options =
method: "PUT"
body: {}
@request "#{ @url() }/unlinkkey/#{ key_id }", options, ( err, meta, res ) =>
return cb err if err
return cb null, meta, @client.newKey( key_id, res )
keys: ( options, cb ) ->
options = @client.getRangeOptions options
@request "#{ @url() }/keys", options, ( err, meta, results ) =>
return cb err if err
instanciated = for id, details of results
@client.newKey id, details
return cb null, meta, instanciated
class exports.Key extends exports.AxleObject
url: -> "/key/#{ @id }"
linkedApis: ( options, cb ) ->
default_options =
query_params:
resolve: true
options = _.merge default_options, options
@request "#{ @url() }/apis", options, ( err, meta, results ) =>
return cb err if err
instanciated = for id, details of results
@client.newApi id, details
return cb null, meta, instanciated
charts: ( options, cb ) ->
@request "#{ @url() }/apicharts", options, cb
class exports.Api extends KeyHolder
url: -> "/api/#{ @id }"
charts: ( options, cb ) ->
@request "#{ @url() }/keycharts", options, cb
statsTimers: ( options, cb ) ->
return @request "#{ @url() }/stats/timers", options, cb
class exports.Keyring extends KeyHolder
url: -> "/keyring/#{ @id }"
class exports.V1 extends Client
constructor: ( args... ) ->
# quick access to these things without having to initialise a new
# client e.g. newApi( "blah", {} )
for type in [ "Api", "Key", "Keyring" ]
do( type ) =>
this["new#{ type }"] = ( id, data ) =>
return new exports[type]( this, id, data )
super args...
getRangeOptions: ( options ) ->
default_options =
query_params:
resolve: true
from: 0
to: 20
return _.merge default_options, { query_params: options }
rawRequest: ( path, options, cb ) ->
super "/v1#{ path }", options, cb
ping: ( cb ) ->
@rawRequest "/ping", {}, cb
apis: ( options, cb ) ->
options = @getRangeOptions options
@request "/apis", options, ( err, meta, results ) =>
return cb err if err
instanciated = for id, details of results
@newApi id, details
return cb null, meta, instanciated
keys: ( options, cb ) ->
options = @getRangeOptions options
@request "/keys", options, ( err, meta, results ) =>
return cb err if err
instanciated = for id, details of results
@newKey id, details
return cb null, meta, instanciated
keyrings: ( options, cb ) ->
options = @getRangeOptions options
@request "/keyrings", options, ( err, meta, results ) =>
return cb err if err
instanciated = for id, details of results
@newKey id, details
return cb null, meta, instanciated
modelDocs: ( cb ) ->
@request "/docs/models", {}, cb
controllerDocs: ( cb ) ->
@request "/docs/controllers", {}, cb
findKey: ( name, cb ) ->
@request "/key/#{ name }", {}, ( err, meta, details ) =>
return cb err, meta if err
return cb null, meta, @newKey( name, details )
findApi: ( name, cb ) ->
@request "/api/#{ name }", {}, ( err, meta, details ) =>
return cb err, meta if err
return cb null, meta, @newApi( name, details )