Module: Webspicy::Web::Openapi::Utils

Included in:
Reporter
Defined in:
lib/webspicy/web/openapi/utils.rb

Instance Method Summary collapse

Instance Method Details

#actual_body_schema(service) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/webspicy/web/openapi/utils.rb', line 172

def actual_body_schema(service)
  schema = actual_input_schema(service)

  a_schema = schema
  a_schema = schema.target if schema.is_a?(Finitio::AliasType)
  return schema unless a_schema.is_a?(Finitio::HashBasedType)

  in_url = service.specification.url_placeholders.map(&:to_sym)
  return schema if in_url.empty?

  a_schema.allbut(in_url)
end

#actual_input_schema(service) ⇒ Object



168
169
170
# File 'lib/webspicy/web/openapi/utils.rb', line 168

def actual_input_schema(service)
  service.input_schema['Main']
end

#actual_output_schema(test_case, counterexample) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/webspicy/web/openapi/utils.rb', line 160

def actual_output_schema(test_case, counterexample)
  if counterexample
    test_case.service.error_schema['Main']
  else
    test_case.service.output_schema['Main']
  end
end

#base_path_for(specification) ⇒ Object



33
34
35
36
37
# File 'lib/webspicy/web/openapi/utils.rb', line 33

def base_path_for(specification)
  into_specification_path(specification, {
    summary: specification.name.to_s || 'API Specification'
  })
end

#base_request_body_for(service) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/webspicy/web/openapi/utils.rb', line 71

def base_request_body_for(service)
  schema, content_type = extract_request_body_info_for(service)
  return {} unless schema

  into_service_request_body(service, {
    required: true,
    content: {
      content_type => {
        schema: schema.to_json_schema,
      }.compact,
    },
  })
end

#base_request_example_for(test_case) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/webspicy/web/openapi/utils.rb', line 85

def base_request_example_for(test_case)
  schema, content_type = extract_request_body_info_for(service)
  return {} unless schema

  value = test_case.params
  in_url = service.specification.url_placeholders
  value = value.reject{|k,v| in_url.include?(k) } if value.is_a?(Hash)

  example = {
    description: test_case.description,
    value: value,
  }

  into_service_request_body(service, {
    required: true,
    content: {
      content_type => {
        examples: {
          test_case.description => example
        }
      }.compact,
    },
  })
end

#base_request_response_for(invocation) ⇒ Object



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
# File 'lib/webspicy/web/openapi/utils.rb', line 110

def base_request_response_for(invocation)
  test_case = invocation.test_case
  service = invocation.service
  content_type = content_type_for(service)
  verb = downcase_verb(service)

  content = nil
  unless invocation.is_empty_response?
    schema = actual_output_schema(test_case, false)
    example = invocation.loaded_output
    content = {
      content_type => {
        schema: schema&.to_json_schema,
        example: example,
      }.compact
    }
  end

  into_service_responses(service, {
    invocation.response_code.to_s => {
      description: '',
      content: content,
    }.compact
  })
end

#base_verb_for(service) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webspicy/web/openapi/utils.rb', line 39

def base_verb_for(service)
  verb_defn = {
    summary: service.name,
    description: service.description,
    tags: tags_for(service.specification).map{|s| s[:name] },
    parameters: parameters_for(service),
  }

  verb_defn = service.conditions.inject(verb_defn) do |memo, p|
    if p.respond_to?(:contribute_to_openapi_verb)
      p.contribute_to_openapi_verb(memo)
    else
      memo
    end
  end

  into_service_verb(service, verb_defn)
end

#content_type_for(service) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/webspicy/web/openapi/utils.rb', line 194

def content_type_for(service)
  if service.method.downcase == 'post_form'
    'application/x-www-form-urlencoded'
  else
    'application/json'
  end
end

#downcase_verb(service) ⇒ Object



190
191
192
# File 'lib/webspicy/web/openapi/utils.rb', line 190

def downcase_verb(service)
  service.method.downcase.gsub(/_form$/, '')
end

#empty_schema?(schema) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
# File 'lib/webspicy/web/openapi/utils.rb', line 202

def empty_schema?(schema)
  schema = schema.target if schema.is_a?(Finitio::AliasType)
  schema.is_a?(Finitio::HashBasedType) && schema.heading.empty?
end

#extract_request_body_info_for(service) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webspicy/web/openapi/utils.rb', line 58

def extract_request_body_info_for(service)
  verb = downcase_verb(service)
  return nil if ['get', 'options', 'head'].include?(verb)

  schema = actual_body_schema(service)
  return nil if empty_schema?(schema)
  puts schema.inspect

  content_type = content_type_for(service)

  [schema, content_type]
end

#into_service_request_body(service, x) ⇒ Object



19
20
21
22
23
# File 'lib/webspicy/web/openapi/utils.rb', line 19

def into_service_request_body(service, x)
  into_service_verb(service, {
    requestBody: x.compact,
  })
end

#into_service_responses(service, x) ⇒ Object



25
26
27
28
29
# File 'lib/webspicy/web/openapi/utils.rb', line 25

def into_service_responses(service, x)
  into_service_verb(service, {
    responses: x.compact,
  })
end

#into_service_verb(service, x) ⇒ Object



12
13
14
15
16
17
# File 'lib/webspicy/web/openapi/utils.rb', line 12

def into_service_verb(service, x)
  verb = downcase_verb(service)
  into_specification_path(service.specification, {
    verb => x.compact,
  })
end

#into_specification_path(specification, x) ⇒ Object



6
7
8
9
10
# File 'lib/webspicy/web/openapi/utils.rb', line 6

def into_specification_path(specification, x)
  {
    standardize(specification.url) => x.compact,
  }
end

#parameters_for(service) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/webspicy/web/openapi/utils.rb', line 147

def parameters_for(service)
  schema = actual_input_schema(service)
  params = service.specification.url_placeholders.map{|p|
    {
      in: 'path',
      name: p,
      schema: { type: 'string' },
      required: true
    }
  }
  params.empty? ? nil : params
end

#standardize(url) ⇒ Object



185
186
187
188
# File 'lib/webspicy/web/openapi/utils.rb', line 185

def standardize(url)
  url = url.gsub(/\/$/, '') if url =~ /\/$/
  url
end

#tags_for(specification) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/webspicy/web/openapi/utils.rb', line 136

def tags_for(specification)
  tag = specification&.openapi&.[](:tag) || specification.name

  return [] unless tag.is_a?(String)
  return [] if tag.empty?

  [{
    name: tag.gsub(/\n/, ' ').strip
  }]
end