Class: OpenApiParser::Specification::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/open_api_parser/specification/endpoint.rb

Constant Summary collapse

RESERVED_PARAMETER_KEYS =
[
  "name",
  "in",
  "description",
  "required"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method, raw) ⇒ Endpoint

Returns a new instance of Endpoint.



13
14
15
16
17
# File 'lib/open_api_parser/specification/endpoint.rb', line 13

def initialize(path, method, raw)
  @path = path
  @method = method
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



11
12
13
# File 'lib/open_api_parser/specification/endpoint.rb', line 11

def raw
  @raw
end

Instance Method Details

#body_schemaObject



19
20
21
22
23
24
# File 'lib/open_api_parser/specification/endpoint.rb', line 19

def body_schema
  body_param = parameters.detect { |param| param["in"] == "body" }
  return restrictive_schema if body_param.nil?

  body_param.fetch("schema", restrictive_schema)
end

#header_json(headers) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/open_api_parser/specification/endpoint.rb', line 66

def header_json(headers)
  schema = header_schema

  headers.reduce({}) do |json, (k, v)|
    json.merge(json_entry(schema, headerize(k), v))
  end
end

#header_schemaObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/open_api_parser/specification/endpoint.rb', line 26

def header_schema
  @header_schema ||= begin
   schema = parameter_schema(permissive_schema, "header")

   schema.tap do |schema|
     schema["properties"] = schema["properties"].reduce({}) do |props, (k, v)|
       props.merge(headerize(k) => v)
     end
   end
  end
end

#path_json(request_path) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/open_api_parser/specification/endpoint.rb', line 74

def path_json(request_path)
  pattern = Regexp.new(@path.gsub(/\{([^}]+)\}/, '(?<\1>[^/]+)'))
  match = pattern.match(request_path.gsub(/\..+\z/, ""))
  schema = path_schema

  match.names.reduce({}) do |json, name|
    json.merge(json_entry(schema, name, match[name]))
  end
end

#path_schemaObject



38
39
40
# File 'lib/open_api_parser/specification/endpoint.rb', line 38

def path_schema
  @path_schema ||= parameter_schema(restrictive_schema, "path")
end

#query_json(query_params) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/open_api_parser/specification/endpoint.rb', line 84

def query_json(query_params)
  schema = query_schema

  query_params.reduce({}) do |json, (k, v)|
    json.merge(json_entry(schema, k, v))
  end
end

#query_schemaObject



42
43
44
# File 'lib/open_api_parser/specification/endpoint.rb', line 42

def query_schema
  @query_schema ||= parameter_schema(restrictive_schema, "query")
end

#response_body_schema(status) ⇒ Object



46
47
48
49
50
51
# File 'lib/open_api_parser/specification/endpoint.rb', line 46

def response_body_schema(status)
  response = response_from_status(status)
  return restrictive_schema if response.nil?

  response.fetch("schema", restrictive_schema)
end

#response_header_schema(status) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/open_api_parser/specification/endpoint.rb', line 53

def response_header_schema(status)
  response = response_from_status(status)
  return permissive_schema if response.nil?

  header_properties = response.fetch("headers", {}).reduce({}) do |props, (k, v)|
    props.merge(headerize(k) => v)
  end

  permissive_schema.tap do |schema|
    schema["properties"] = header_properties
  end
end