Lines of Code | 71 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 27.78% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | 1 | const ParserInterface = require('../ParserInterface'), |
|
2 | _ = require('lodash') |
||
3 | |||
4 | /** |
||
5 | * Response parser |
||
6 | * @class ResponseParser |
||
7 | * |
||
8 | * @property {Array.<Swagger20ResponseObject>} originalData Original responses |
||
9 | * @property {Array.<Swagger20ResponseObject>} data Parsed responses |
||
10 | * @param {String} modelPath Path to model |
||
11 | */ |
||
12 | class ResponseParser extends ParserInterface { |
||
13 | |||
14 | /** |
||
15 | * ResponseParser constructor |
||
16 | * |
||
17 | * @param {Array.<Swagger20ResponseObject>} responses Original responses |
||
18 | * @param {String} modelPath Path to model |
||
19 | */ |
||
20 | constructor (responses, modelPath) { |
||
21 | super() |
||
22 | |||
23 | this.originalData = responses |
||
24 | this.modelPath = modelPath |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Load references name from property |
||
29 | * |
||
30 | * @return {undefined|String} |
||
31 | * |
||
32 | * @private |
||
33 | */ |
||
34 | _loadReference (ref) { |
||
35 | 2 | if (_.isString(ref)) { |
|
36 | let segments = ref.split('/') |
||
37 | return segments[segments.length - 1] |
||
38 | } |
||
39 | |||
40 | return undefined |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Parse response |
||
45 | * |
||
46 | * @return {Array.<Swagger20ResponseObject>} |
||
47 | */ |
||
48 | parse () { |
||
49 | let _self = this |
||
50 | this.data = {} |
||
51 | |||
52 | _.each(this.originalData, (config, code) => { |
||
53 | 4 | if (config.schema && config.schema.$ref) { |
|
54 | let ref = _self._loadReference(config.schema.$ref) |
||
55 | 2 | config.schema = _.isString(ref) ? { |
|
56 | ref: ref |
||
57 | } : {} |
||
58 | config.modelPath = _self.modelPath |
||
59 | } |
||
60 | _self.data[code] = config |
||
61 | }) |
||
62 | |||
63 | return this.data |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @ignore module.exports |
||
69 | * @ignore exports |
||
70 | */ |
||
71 | module.exports = ResponseParser |