Class: Sinatra::SwaggerExposer::SwaggerEndpoint

Inherits:
Object
  • Object
show all
Includes:
SwaggerUtilities
Defined in:
lib/sinatra/swagger-exposer/swagger-endpoint.rb

Overview

An endpoint

Constant Summary collapse

REGEX_PATH_PARAM_MIDDLE =
/\A(.*\/)\:([a-z]+)\/(.+)\z/
REGEX_PATH_PARAM_END =
/\A(.*)\/:([a-z]+)\z/

Constants included from SwaggerParameterHelper

Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_HEADER, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_PATH, Sinatra::SwaggerExposer::SwaggerParameterHelper::HOW_TO_PASS_QUERY, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_DEFAULT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXAMPLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_EXCLUSIVE_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_FORMAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_LIST, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAXIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MAX_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MINIMUM, Sinatra::SwaggerExposer::SwaggerParameterHelper::PARAMS_MIN_LENGTH, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES, Sinatra::SwaggerExposer::SwaggerParameterHelper::PRIMITIVE_TYPES_FOR_NON_BODY, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BOOLEAN, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_BYTE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DATE_TIME, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_DOUBLE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FILE, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_FLOAT, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_INTEGER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_LONG, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_NUMBER, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_PASSWORD, Sinatra::SwaggerExposer::SwaggerParameterHelper::TYPE_STRING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwaggerUtilities

#get_type, #hash_to_swagger, #list_or_none, #ref_to_type, #type_to_s, #white_list_params

Constructor Details

#initialize(type, sinatra_path, parameters, responses, summary, description, tags, explicit_path, produces) ⇒ SwaggerEndpoint

Returns a new instance of SwaggerEndpoint.



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

def initialize(type, sinatra_path, parameters, responses, summary, description, tags, explicit_path, produces)
  @type = type
  @path = swagger_path(sinatra_path, explicit_path)
  @request_preprocessor = SwaggerRequestPreprocessor.new

  @parameters = parameters
  @parameters.each do |parameter|
    preprocessor = parameter.preprocessor
    if preprocessor.useful?
      @request_preprocessor.add_preprocessor preprocessor
    end
  end

  @responses = responses

  @attributes = {}
  if summary
    @attributes[:summary] = summary
  end
  if description
    @attributes[:description] = description
  end
  if tags
    @attributes[:tags] = tags
  end
  if produces
    @attributes[:produces] = produces
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 13

def path
  @path
end

#request_preprocessorObject (readonly)

Returns the value of attribute request_preprocessor.



13
14
15
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 13

def request_preprocessor
  @request_preprocessor
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 13

def type
  @type
end

Instance Method Details

#swagger_path(sinatra_path, explicit_path) ⇒ Object

Get the endpoint swagger path

Parameters:

  • sinatra_path

    the path declared in the sinatra app

  • explicit_path

    an explicit path the user can specify



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 65

def swagger_path(sinatra_path, explicit_path)
  if explicit_path
    explicit_path
  elsif sinatra_path.is_a? String
    while (m = REGEX_PATH_PARAM_MIDDLE.match(sinatra_path))
      sinatra_path = "#{m[1]}{#{m[2]}}/#{m[3]}"
    end
    if (m = REGEX_PATH_PARAM_END.match(sinatra_path))
      sinatra_path = "#{m[1]}/{#{m[2]}}"
    end
    sinatra_path
  else
    raise SwaggerInvalidException.new("You need to specify a path when using a non-string path [#{sinatra_path}]")
  end
end

#to_sObject



81
82
83
84
85
86
87
88
89
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 81

def to_s
  {
      :type => @type,
      :path => @path,
      :attributes => @attributes,
      :parameters => @parameters,
      :responses => @responses,
  }.to_json
end

#to_swaggerObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sinatra/swagger-exposer/swagger-endpoint.rb', line 45

def to_swagger
  result = @attributes.clone

  unless @parameters.empty?
    result[:parameters] = @parameters.collect { |parameter| parameter.to_swagger }
  end

  unless @responses.empty?
    result[:responses] = hash_to_swagger(@responses)
  end

  result
end