Class: Sinatra::SwaggerExposer::Configuration::SwaggerEndpoint

Inherits:
Object
  • Object
show all
Includes:
SwaggerConfigurationUtilities
Defined in:
lib/sinatra/swagger-exposer/configuration/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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwaggerConfigurationUtilities

#check_name, #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.

Parameters:



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

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

  @parameters = parameters
  @responses = responses
  @produces = produces

  @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

#parametersObject (readonly)

Returns the value of attribute parameters.



16
17
18
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 16

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 16

def path
  @path
end

#producesObject (readonly)

Returns the value of attribute produces.



16
17
18
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 16

def produces
  @produces
end

#responsesObject (readonly)

Returns the value of attribute responses.



16
17
18
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 16

def responses
  @responses
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 16

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



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

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



88
89
90
91
92
93
94
95
96
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 88

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

#to_swaggerHash

Return a swagger version

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sinatra/swagger-exposer/configuration/swagger-endpoint.rb', line 52

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