File: resource.ejs

package info (click to toggle)
ruby-asana 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,020 kB
  • sloc: ruby: 3,314; javascript: 8; makefile: 3
file content (225 lines) | stat: -rw-r--r-- 9,150 bytes parent folder | download | duplicates (5)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<%
var singularName  = resource.name,
    pluralName    = plural(singularName),
    mixins        = {
      task: ["AttachmentUploading", "EventSubscription"],
      project: ["EventSubscription"]
    },
    skip          = { attachment: ["createOnTask"] },
    formatComment = function formatComment(text, indentation) {
        var indent = Array(indentation + 1).join(" ")
        return text.trim().split("\n").map(function(line) {
            return indent + (line.length > 0 ? "# " : "#") + line
        }).join("\n")
    }


function Action(action) {
    var that = this
    this.action = action
    this.collection = action.collection == true
    this.requiresData = action.method == "POST" || action.method == "PUT"
    this.isInstanceAction = (action.method == "PUT" || action.method == "DELETE")
    this.method = action.method
    this.methodName = snake(action.name)
    this.clientMethod = action.method.toLowerCase()
    this.returnsUpdatedRecord = action.method == 'PUT' || action.comment.match(/Returns[a-z\W]+updated/) !== null
    this.returnsNothing = action.comment.match(/Returns an empty/) !== null

    // Params and idParams
    var params = action.params || []
    this.idParams = _.filter(params, function(p) { return p.type == "Id" })

    // If it looks like an instance action but it's not, make it one
    if (!this.isInstanceAction) {
        var mainIdParam = _.find(this.idParams, function(p) { return p.name == singularName })
        if (mainIdParam !== undefined && !action.name.match(/Id/)) {
            this.isInstanceAction = true
            this.mainIdParam = mainIdParam
        }
    }

    if (this.idParams.length == 1 &&
        action.path.match(/%d/) &&
        (action.name.match(/Id/) || (this.isInstanceAction && this.mainIdParam == undefined))) {
            var mainIdParam = this.idParams[0]
            this.mainIdParam = mainIdParam
            this.inferredReturnType = this.isInstanceAction ? 'self.class' : 'self'
    }

    if (mainIdParam !== undefined) {
        this.params = _.reject(params, function(p) { return p.name == mainIdParam.name })
    } else {
        this.params = params
    }

    if (!this.inferredReturnType) {
        // Infer return type
        var name = action.path.match(/\/([a-zA-Z]+)$/)
        if (name !== null) {
            name = name[1]

            // Desugarize 'addProject' to 'project'
            var camelCaseTail = name.match(/.*([A-Z][a-z]+)$/)
            if (camelCaseTail !== null) { name = decap(camelCaseTail[1]) }

            name = single(name)

            var explicit = _.find(resources, function(p) { return p == name })

            if (name == singularName || name == 'parent' || name == 'children' || name.match(/^sub/) !== null) {
                this.inferredReturnType = this.isInstanceAction ? 'self.class' : 'self'
            } else if (explicit !== undefined) {
                this.inferredReturnType = cap(explicit)
            } else {
                this.inferredReturnType = 'Resource'
            }
        } else {
            this.inferredReturnType = 'Resource'
        }
    }

    // Endpoint path
    this.path = _.reduce(this.idParams, function(acc, id) {
        var localName = that.mainIdParam == id ? "id" : id.name
        return acc.replace("\%d", "#{" + localName + "}")
    }, action.path)

    // Extra params (not in the URL) to be passed in the body of the call
    this.extraParams = _.reject(this.params, function(p) {
        return that.path.match(new RegExp("#{" + p.name + "}"))
    })

    // Params processing
    var paramsLocal = "data"
    if (this.collection) { this.extraParams.push({ name: "per_page", apiParamName: "limit" }) }
    if (this.extraParams.length > 0) {
        var paramNames = _.map(this.extraParams, function(p) { return (p.apiParamName || p.name) + ": " + p.name })
        if (this.requiresData) {
            var paramsProcessing = "with_params = data.merge(" + paramNames.join(", ") + ")"
            paramsLocal = "with_params"
        } else {
            var paramsProcessing = "params = { " + paramNames.join(", ") + " }"
            paramsLocal = "params"
        }
        paramsProcessing += ".reject { |_,v| v.nil? || Array(v).empty? }"
    }

    this.paramsProcessing = paramsProcessing
    this.paramsLocal      = paramsLocal

    // Method argument names
    var argumentNames = Array()
    if (!this.isInstanceAction) { argumentNames.push("client") }
    if (this.mainIdParam !== undefined && !this.isInstanceAction) { argumentNames.push("id") }
    _.forEach(this.params, function(param) {
        argumentNames.push(param.name + ":" + (param.required ? " required(\"" + param.name + "\")" : " nil"))
    })
    if (this.collection) { argumentNames.push("per_page: 20") }
    if (this.method != 'DELETE')  { argumentNames.push("options: {}") }
    if (this.requiresData)          { argumentNames.push("**data") }
    this.argumentNames = argumentNames

    // API request params
    var requestParams = Array()
    requestParams.push('"' + this.path + '"')
    if (this.paramsProcessing || this.argumentNames.indexOf("**data") != -1) {
        var argument = this.requiresData ? "body" : "params"
        requestParams.push(argument + ": " + paramsLocal)
    }
    if (this.method != 'DELETE') { requestParams.push("options: options") }
    this.requestParams = requestParams
    this.documentation = this.renderDocumentation()

    // Constructor
    this.constructor = function(body) {
        var pre = '', post = ''
        var wrapWithParsing = function(body) {
            var pre = '', post = ''
            if (!that.returnsNothing) {
                pre = 'parse('
                post = ')' + (that.collection ? '' : '.first')
            }
            return pre + body + post
        }

        if (!that.returnsNothing) {
            if (that.isInstanceAction && that.returnsUpdatedRecord) {
                pre = "refresh_with("
                post = ')'
            } else {
                pre = that.collection ? "Collection.new(" : that.inferredReturnType + ".new("
                post = (that.collection ? ', type: ' + that.inferredReturnType : '') + ', client: client)'
            }
        } else { post = ' && true' }
        return pre + wrapWithParsing(body) + post
    }

    this.request = this.constructor("client." + this.clientMethod + "(" + this.requestParams.join(", ") + ")")
}

Action.prototype.renderDocumentation = function () {
    var formatParamNotes = function(params) {
        var trimmed = _.flatten(_.map(params, function(p) {
            return _.map(p.notes, function(note) { return note.trim() })
        }))
        return (trimmed.length > 0 ? "\nNotes:\n\n" + trimmed.join("\n\n") : "")
    }

    var formatParam = function(p, name) {
        return (name !== undefined ? name : p.name) + " - [" + p.type + "] " + p.comment
    }
    var lines = _.map(this.params, function(p) { return formatParam(p) })
    if (this.mainIdParam !== undefined && !this.isInstanceAction) { lines.unshift(formatParam(this.mainIdParam, "id")) }
    if (this.collection) { lines.push("per_page - [Integer] the number of records to fetch per page.") }
    if (this.method != 'DELETE') { lines.push("options - [Hash] the request I/O options.") }
    if (this.requiresData) { lines.push("data - [Hash] the attributes to post.") }
    return this.action.comment + "\n" + lines.join("\n") + formatParamNotes(this.params)
}

var actionsToSkip   = skip[resource.name] || []
var actionsToGen    = _.reject(resource.actions, function(action) {
  return actionsToSkip.indexOf(action.name) != -1
})

var allActions      = _.map(actionsToGen, function(action) { return new Action(action) }),
    instanceActions = _.filter(allActions, function(action) { return action.isInstanceAction }),
    classActions    = _.reject(allActions, function(action) { return action.isInstanceAction })

var mixinsToInclude = mixins[resource.name] || []

%>### WARNING: This file is auto-generated by the asana-api-meta repo. Do not
### edit it manually.

module Asana
  module Resources
<%= formatComment(resource.comment, 4) %>
    class <%= cap(singularName) %> < Resource
<% _.forEach(mixinsToInclude, function(mixin) { %>
      include <%= mixin %>
<% }) %>
<% _.forEach(resource.properties, function(property) { %>
      attr_reader :<%= property.name %>
<% }) %>
      class << self
        # Returns the plural name of the resource.
        def plural_name
          '<%= pluralName %>'
        end
<% _.forEach(classActions, function(action) { %>
<%= formatComment(action.documentation, 8) %>
        def <%= action.methodName %>(<%= action.argumentNames.join(", ") %>)
<% if (action.paramsProcessing) { %>          <%= action.paramsProcessing %><% } %>
          <%= action.request %>
        end
<% }) %>      end
<% _.forEach(instanceActions, function(action) { %>
<%= formatComment(action.documentation, 6) %>
      def <%= action.methodName %>(<%= action.argumentNames.join(", ") %>)
<% if (action.paramsProcessing) { %>        <%= action.paramsProcessing %><% } %>
        <%= action.request %>
      end
<% }) %>
    end
  end
end