Module: Sinatra::SwaggerExposer

Defined in:
lib/sinatra/swagger-exposer/swagger-exposer.rb,
lib/sinatra/swagger-exposer/version.rb,
lib/sinatra/swagger-exposer/swagger-info.rb,
lib/sinatra/swagger-exposer/swagger-type.rb,
lib/sinatra/swagger-exposer/swagger-endpoint.rb,
lib/sinatra/swagger-exposer/swagger-utilities.rb,
lib/sinatra/swagger-exposer/swagger-type-property.rb,
lib/sinatra/swagger-exposer/swagger-content-creator.rb,
lib/sinatra/swagger-exposer/swagger-parameter-helper.rb,
lib/sinatra/swagger-exposer/swagger-endpoint-response.rb,
lib/sinatra/swagger-exposer/swagger-invalid-exception.rb,
lib/sinatra/swagger-exposer/swagger-endpoint-parameter.rb,
lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb,
lib/sinatra/swagger-exposer/swagger-parameter-preprocessor.rb

Overview

Expose swagger API from your Sinatra app

Defined Under Namespace

Modules: SwaggerParameterHelper, SwaggerUtilities Classes: SwaggerContentCreator, SwaggerEndpoint, SwaggerEndpointParameter, SwaggerEndpointResponse, SwaggerInfo, SwaggerInvalidException, SwaggerParameterPreprocessor, SwaggerRequestPreprocessor, SwaggerType, SwaggerTypeProperty

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.declare_swagger_endpoints(app) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 27

def self.declare_swagger_endpoints(app)
  app.endpoint_summary 'The swagger endpoint'
  app.endpoint_tags 'swagger'
  app.get '/swagger_doc.json' do
    swagger_content = ::Sinatra::SwaggerExposer::SwaggerContentCreator.new(
        settings.respond_to?(:swagger_info) ? settings.swagger_info : nil,
        settings.swagger_types,
        settings.swagger_endpoints
    ).to_swagger
    content_type :json
    swagger_content.to_json
  end

  app.endpoint_summary 'Option method for the swagger endpoint, useful for some CORS stuff'
  app.endpoint_tags 'swagger'
  app.options '/swagger_doc.json' do
    200
  end
end

.registered(app) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 18

def self.registered(app)
  app.set :swagger_endpoints, []
  app.set :swagger_current_endpoint_info, {}
  app.set :swagger_current_endpoint_parameters, {}
  app.set :swagger_current_endpoint_responses, {}
  app.set :swagger_types, {}
  declare_swagger_endpoints(app)
end

Instance Method Details

#endpoint(params) ⇒ Object

Define fluent endpoint dispatcher

Parameters:

  • params (Hash)

    the parameters



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
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 88

def endpoint(params)
  params.each_pair do |param_name, param_value|
    case param_name
      when :summary
        endpoint_summary param_value
      when :description
        endpoint_description param_value
      when :tags
        endpoint_tags *param_value
      when :produces
        endpoint_produces *param_value
      when :path
        endpoint_path param_value
      when :parameters
        param_value.each do |param, args_param|
          endpoint_parameter param, *args_param
        end
      when :responses
        param_value.each do |code, args_response|
          endpoint_response code, *args_response
        end
      else
        raise SwaggerInvalidException.new("Invalid endpoint parameter [#{param_name}]")
    end
  end
end

#endpoint_description(description) ⇒ Object

Provide a description for the endpoint



58
59
60
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 58

def endpoint_description(description)
  set_if_type_and_not_exist(description, :description, String)
end

#endpoint_parameter(name, description, how_to_pass, required, type, params = {}) ⇒ Object

Define parameter for the endpoint



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 73

def endpoint_parameter(name, description, how_to_pass, required, type, params = {})
  parameters = settings.swagger_current_endpoint_parameters
  check_if_not_duplicate(name, parameters, 'Parameter')
  parameters[name] = SwaggerEndpointParameter.new(
      name,
      description,
      how_to_pass,
      required,
      type,
      params,
      settings.swagger_types.keys)
end

#endpoint_path(path) ⇒ Object

Provide a path



53
54
55
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 53

def endpoint_path(path)
  set_if_type_and_not_exist(path, :path, String)
end

#endpoint_produces(*produces) ⇒ Object

Provide produces params for the endpoint



68
69
70
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 68

def endpoint_produces(*produces)
  set_if_not_exist(produces, :produces)
end

#endpoint_response(code, type = nil, description = nil) ⇒ Object

Declare a response



130
131
132
133
134
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 130

def endpoint_response(code, type = nil, description = nil)
  responses = settings.swagger_current_endpoint_responses
  check_if_not_duplicate(code, responses, 'Response')
  responses[code] = SwaggerEndpointResponse.new(type, description, settings.swagger_types.keys)
end

#endpoint_summary(summary) ⇒ Object

Provide a summary for the endpoint



48
49
50
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 48

def endpoint_summary(summary)
  set_if_type_and_not_exist(summary, :summary, String)
end

#endpoint_tags(*tags) ⇒ Object

Provide tags for the endpoint



63
64
65
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 63

def endpoint_tags(*tags)
  set_if_not_exist(tags, :tags)
end

#general_info(params) ⇒ Object

General information



116
117
118
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 116

def general_info(params)
  set :swagger_info, SwaggerInfo.new(params)
end

#route(verb, path, options = {}, &block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 136

def route(verb, path, options = {}, &block)
  if verb == 'HEAD'
    super(verb, path, options, &block)
  else
    request_preprocessor = process_endpoint(verb.downcase, path, options)
    super(verb, path, options) do |*params|
      request_preprocessor.run(self, params, &block)
    end
  end
end

#type(name, params) ⇒ Object

Declare a type



121
122
123
124
125
126
127
# File 'lib/sinatra/swagger-exposer/swagger-exposer.rb', line 121

def type(name, params)
  types = settings.swagger_types
  if types.key? name
    raise SwaggerInvalidException.new("Type [#{name}] already exist with value #{types[name]}")
  end
  types[name] = SwaggerType.new(name, params, settings.swagger_types.keys)
end