Skip to content

Commit c66c2c3

Browse files
committed
resource.url and resource.name are now properties instead of functions, so they can be accessed in (swagger-ui) templates
1 parent 433e14a commit c66c2c3

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

lib/swagger-spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@
9999
return wordnik.ready != null;
100100
});
101101
});
102-
it("has a url()", function() {
102+
it("creates a url property", function() {
103103
return runs(function() {
104104
var resource;
105105
resource = wordnik.resources.wordList;
106-
return expect(resource.url()).toMatch(/\.json$/);
106+
return expect(resource.url).toMatch(/\.json$/);
107107
});
108108
});
109-
it("has a name() method which is inferred from its path", function() {
109+
it("has a name property which is inferred from its path", function() {
110110
return runs(function() {
111111
var resource;
112112
resource = wordnik.word;
113-
return expect(resource.name()).toEqual('word');
113+
return expect(resource.name).toEqual('word');
114114
});
115115
});
116116
it("creates a container object for its operations, with operation nicknames as keys", function() {

lib/swagger.js

Lines changed: 8 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/swagger-spec.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ describe 'SwaggerResource', ->
8383
waitsFor ->
8484
wordnik.ready?
8585

86-
it "has a url()", ->
86+
it "creates a url property", ->
8787
runs ->
8888
resource = wordnik.resources.wordList
89-
expect(resource.url()).toMatch(/\.json$/)
89+
expect(resource.url).toMatch(/\.json$/)
9090

91-
it "has a name() method which is inferred from its path", ->
91+
it "has a name property which is inferred from its path", ->
9292
runs ->
9393
resource = wordnik.word
94-
expect(resource.name()).toEqual('word')
94+
expect(resource.name).toEqual('word')
9595

9696
it "creates a container object for its operations, with operation nicknames as keys", ->
9797
runs ->

src/swagger.coffee

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class SwaggerApi
2+
3+
# Defaults
24
discoveryUrl: "http://api.wordnik.com/v4/resources.json"
35
debug: false
46
api_key: null
@@ -30,7 +32,7 @@ class SwaggerApi
3032
continue if resource.path is "/tracking.{format}" or resource.path is "/partner.{format}"
3133

3234
res = new SwaggerResource resource.path, resource.description, this
33-
@resources[res.name()] = res
35+
@resources[res.name] = res
3436

3537
this
3638

@@ -50,7 +52,15 @@ class SwaggerResource
5052
throw "SwaggerResources must have a path." unless @path?
5153
@operations = {}
5254

53-
$.getJSON @url(), (response) =>
55+
# e.g."http://api.wordnik.com/v4/word.json"
56+
@url = @api.basePath + @path.replace('{format}', 'json')
57+
58+
# Extract name from path
59+
# '/foo/dogs.format' -> 'dogs'
60+
parts = @path.split("/")
61+
@name = parts[parts.length - 1].replace('.{format}', '')
62+
63+
$.getJSON @url, (response) =>
5464
@basePath = response.basePath
5565

5666
# TODO: Take this out.. it's a wordnik API regression
@@ -65,24 +75,14 @@ class SwaggerResource
6575
@operations[op.nickname] = op
6676

6777
# Store a named reference to this resource on the parent object
68-
@api[this.name()] = this
78+
@api[this.name] = this
6979

7080
# Mark as ready
7181
@ready = true
7282

7383
# Now that this resource is loaded, tell the API to check in on itself
7484
@api.selfReflect()
7585

76-
# e.g."http://api.wordnik.com/v4/word.json"
77-
url: ->
78-
@api.basePath + @path.replace('{format}', 'json')
79-
80-
# Extract name from path
81-
# '/foo/dogs.format' -> 'dogs'
82-
name: ->
83-
parts = @path.split("/")
84-
parts[parts.length - 1].replace('.{format}', '')
85-
8686
class SwaggerOperation
8787

8888
constructor: (@nickname, @path, @httpMethod, @parameters, @summary, @resource) ->

0 commit comments

Comments
 (0)