Class: Swaggable::Swagger2Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/swaggable/swagger_2_serializer.rb

Overview

Generates a Swagger 2 hash from an ApiDefinition.

Examples:

Basic usage

serializer = Swagger2Serializer.new
api_definition = ApiDefinition.new
serializer.serialize(api_definition)
# => {:swagger=>"2.0", :basePath=>nil, :info=>{:title=>nil, :description=>nil, :version=>nil}, :tags=>[], :paths=>{}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tag_serializerObject

Returns the value of attribute tag_serializer.



11
12
13
# File 'lib/swaggable/swagger_2_serializer.rb', line 11

def tag_serializer
  @tag_serializer
end

Instance Method Details

#serialize(api) ⇒ Object

Main method that given an ApiDefinition will return a hash to serialize



14
15
16
17
18
19
20
21
22
23
# File 'lib/swaggable/swagger_2_serializer.rb', line 14

def serialize api
  {
    swagger: '2.0',
    basePath: api.base_path,
    info: serialize_info(api),
    tags: api.tags.map{|t| serialize_tag t },
    paths: serialize_endpoints(api.endpoints),
    definitions: serialize_definitions(api),
  }
end

#serialize_definitions(api) ⇒ Object



118
119
120
121
122
123
# File 'lib/swaggable/swagger_2_serializer.rb', line 118

def serialize_definitions api
  api.used_schemas.inject({}) do |acc, schema|
    acc[schema.name] = serialize_parameter_schema schema
    acc
  end
end

#serialize_endpoint(endpoint) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swaggable/swagger_2_serializer.rb', line 52

def serialize_endpoint endpoint
  {
    tags: endpoint.tags.map(&:name),
    consumes: endpoint.consumes,
    produces: endpoint.produces,
    parameters: endpoint.parameters.map{|p| serialize_parameter p },
    responses: serialize_responses(endpoint.responses),
  }.
  tap do |e|
    e[:summary] = endpoint.summary if endpoint.summary
    e[:description] = endpoint.description if endpoint.description
  end
end

#serialize_endpoints(endpoints) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/swaggable/swagger_2_serializer.rb', line 44

def serialize_endpoints endpoints
  endpoints.inject({}) do |out, endpoint|
    out[endpoint.path] ||= {}
    out[endpoint.path][endpoint.verb] = serialize_endpoint(endpoint)
    out
  end
end

#serialize_info(api) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/swaggable/swagger_2_serializer.rb', line 25

def serialize_info api
  {
    title: api.title.to_s,
    version: (api.version || '0.0.0'),
  }.
  tap do |h|
    h[:description] = api.description if api.description
  end
end

#serialize_parameter(parameter) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/swaggable/swagger_2_serializer.rb', line 66

def serialize_parameter parameter
  p = {
    in: parameter.location.to_s,
    name: parameter.name,
    required: parameter.required?,
  }

  p[:type] = parameter.type || 'string' unless parameter.location == :body
  p[:description] = parameter.description if parameter.description

  unless parameter.schema.empty?
    p[:schema] = {:"$ref" => "#/definitions/#{parameter.schema.name}"}
  end

  p
end

#serialize_parameter_attribute(attribute) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/swaggable/swagger_2_serializer.rb', line 97

def serialize_parameter_attribute attribute
  {
    type: attribute.json_type,
  }.
  tap do |e|
    e[:description] = attribute.description if attribute.description
    e[:format] = attribute.json_format if attribute.json_format
  end
end

#serialize_parameter_schema(schema) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/swaggable/swagger_2_serializer.rb', line 83

def serialize_parameter_schema schema
  out = {type: 'object'}

  required_attrs = schema.attributes.select(&:required?)
  out[:required] = required_attrs.map(&:name) if required_attrs.any?

  out[:properties] = schema.attributes.inject({}) do |acc, attribute|
    acc[attribute.name] = serialize_parameter_attribute attribute
    acc
  end

  out
end

#serialize_responses(responses) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/swaggable/swagger_2_serializer.rb', line 107

def serialize_responses responses
  if responses.any?
    responses.inject({}) do |acc, r|
      acc[r.status] = {description: r.description}
      acc
    end
  else
    {200 => {description: 'Success'}}
  end
end

#serialize_tag(tag) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/swaggable/swagger_2_serializer.rb', line 35

def serialize_tag tag
  {
    name: tag.name,
  }.
  tap do |e|
    e[:description] = tag.description if tag.description
  end
end

#validate(api) ⇒ Object



129
130
131
# File 'lib/swaggable/swagger_2_serializer.rb', line 129

def validate api
  Swagger2Validator.validate serialize(api)
end

#validate!(api) ⇒ Object



125
126
127
# File 'lib/swaggable/swagger_2_serializer.rb', line 125

def validate! api
  Swagger2Validator.validate! serialize(api)
end