forked from Apipie/apipie-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_description.rb
131 lines (101 loc) · 3.53 KB
/
response_description.rb
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
module Apipie
class ResponseDescription
class ResponseObject
include Apipie::DSL::Base
include Apipie::DSL::Param
attr_accessor :additional_properties, :typename
def initialize(method_description, scope, block, typename)
@method_description = method_description
@scope = scope
@param_group = {scope: scope}
@additional_properties = false
@typename = typename
self.instance_exec(&block) if block
prepare_hash_params
end
# this routine overrides Param#_default_param_group_scope and is called if Param#param_group is
# invoked during the instance_exec call in ResponseObject#initialize
def _default_param_group_scope
@scope
end
def name
"response #{@code} for #{@method_description.method}"
end
def params_ordered
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
options = args.find { |arg| arg.is_a? Hash }
options[:param_group] = @param_group
Apipie::ParamDescription.from_dsl_data(@method_description, args) unless options[:only_in] == :request
end.compact
end
def prepare_hash_params
@hash_params = params_ordered.reduce({}) do |h, param|
h.update(param.name.to_sym => param)
end
end
end
end
class ResponseDescription
include Apipie::DSL::Base
include Apipie::DSL::Param
attr_reader :code, :description, :scope, :type_ref, :hash_validator, :is_array_of
def self.from_dsl_data(method_description, code, args)
options, scope, block, adapter = args
Apipie::ResponseDescription.new(method_description,
code,
options,
scope,
block,
adapter)
end
def is_array?
@is_array_of != false
end
def typename
@response_object.typename
end
def initialize(method_description, code, options, scope, block, adapter)
@type_ref = options[:param_group]
@is_array_of = options[:array_of] || false
raise ReturnsMultipleDefinitionError, options if @is_array_of && @type_ref
@type_ref ||= @is_array_of
@method_description = method_description
if code.is_a? Symbol
@code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
else
@code = code
end
@description = options[:desc]
if @description.nil?
@description = Rack::Utils::HTTP_STATUS_CODES[@code]
raise "Cannot infer description from status code #{@code}" if @description.nil?
end
@scope = scope
if adapter
@response_object = adapter
else
@response_object = ResponseObject.new(method_description, scope, block, @type_ref)
end
@response_object.additional_properties ||= options[:additional_properties]
end
def param_description
nil
end
def params_ordered
@response_object.params_ordered
end
def additional_properties
!!@response_object.additional_properties
end
alias :allow_additional_properties :additional_properties
def to_json(lang=nil)
{
:code => code,
:description => description,
:is_array => is_array?,
:returns_object => params_ordered.map{ |param| param.to_json(lang).tap{|h| h.delete(:validations) }}.flatten,
:additional_properties => additional_properties,
}
end
end
end