Class: Rspec::Autoswagger::Parts::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/autoswagger/parts/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rspec_core_obj, example, response_name) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rspec/autoswagger/parts/path.rb', line 9

def initialize(rspec_core_obj, example, response_name)
  @response = rspec_core_obj.response
  @request = rspec_core_obj.request
  begin
    @description = rspec_core_obj.description
  rescue RSpec::Core::ExampleGroup::WrongScopeError
    @description = ''
  end
  @example = example
  @response_name = response_name
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def description
  @description
end

#exampleObject (readonly)

Returns the value of attribute example.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def example
  @example
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def path
  @path
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def response
  @response
end

#response_nameObject (readonly)

Returns the value of attribute response_name.



7
8
9
# File 'lib/rspec/autoswagger/parts/path.rb', line 7

def response_name
  @response_name
end

Instance Method Details

#generate_hashObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rspec/autoswagger/parts/path.rb', line 135

def generate_hash
  hash = {}
  path_sym = path
  hash[path] = {}
  hash[path][method] = {}

  hash[path][method] = {}
  hash[path][method]["tags"] = tags
  hash[path][method]["summary"] = ''
  hash[path][method]["description"] = description

  params, param_definitions = generate_parameters
  hash[path][method]["parameters"] = params

  hash[path][method]["produces"] = ['application/json']
  hash[path][method]["responses"] = {}
  hash[path][method]["responses"][status] = {}
  hash[path][method]["responses"][status]["description"] = response_description
  hash[path][method]["responses"][status]["schema"] = { "$ref" => "#/definitions/#{response_name}" }
  [hash, param_definitions]
end

#generate_parametersObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rspec/autoswagger/parts/path.rb', line 74

def generate_parameters
  schema = {}
  params_arr = []
  params.each do |name, value|
    type = convert_value_to_type(value)
    param_type = predict_param_type(name)
    if param_type == 'body'
      if type == 'array'
        value = value.map { |v| v.to_h } if value.first.class.to_s == 'ActiveSupport::HashWithIndifferentAccess'
        type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy")
        type_hash.delete('example')
        schema.merge!({ name => { 'type' => type, 'items' => type_hash } })
      else
        schema.merge!({ name => { 'type' => type } })
      end
    else
      if type == 'array'
        value = value.map { |v| v.to_h } if value.first.class.to_s == 'ActiveSupport::HashWithIndifferentAccess'
        type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy")
        type_hash.delete('example')
        param_hash = {
          'name' => name,
          'in' => param_type,
          'type' => type,
          'items' => type_hash
        }
      elsif type == 'hash'
        value.each do |n, v|
          type = convert_value_to_type(v)
          param_hash = {
            'name' => "#{name}\[#{n}\]",
            'in' => param_type,
            'type' => type
          }
          params_arr << param_hash
        end
      else
        param_hash = {
          'name' => name,
          'in' => param_type,
          'type' => type
        }
      end
      param_hash['required'] = true if param_type == 'path'
      params_arr << param_hash
    end
    params_arr = params_arr.uniq
  end
  param_definitions = {}
  unless schema.empty?
    params_arr << {
                     'name' => 'body',
                     'in' => 'body',
                     'schema' => { "$ref" => "#/definitions/#{param_model_name}" }
                   }
    param_definitions = { param_model_name => { 'type' => 'object', 'properties' => schema } }
  end

  [params_arr, param_definitions]
end

#get_converted_path(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/autoswagger/parts/path.rb', line 31

def get_converted_path(path)
  path.split("/").map do |path_element|
    if Util.detect_uuid(path_element)
      "{id}"
    elsif Util.detect_uuid(path_element)
      "{id}"
    else
      path_element
    end
  end.join("/")
end

#methodObject



54
55
56
# File 'lib/rspec/autoswagger/parts/path.rb', line 54

def method
  request.method.downcase
end

#operation_idObject



58
59
60
# File 'lib/rspec/autoswagger/parts/path.rb', line 58

def operation_id
  (method + path.gsub('/', '_').gsub(/{|}/, '')).camelize
end

#param_model_nameObject



62
63
64
# File 'lib/rspec/autoswagger/parts/path.rb', line 62

def param_model_name
  (response_name + '_request_parameter').camelize
end

#paramsObject



66
67
68
69
70
71
72
# File 'lib/rspec/autoswagger/parts/path.rb', line 66

def params
  return @params if @params.present?
  @params = request.parameters.dup
  @params.delete('controller')
  @params.delete('action')
  @params
end

#statusObject



50
51
52
# File 'lib/rspec/autoswagger/parts/path.rb', line 50

def status
  response.status
end

#tagsObject



43
44
45
46
47
48
# File 'lib/rspec/autoswagger/parts/path.rb', line 43

def tags
  tag = request.parameters['controller']
  base_path = Info.generate_hash['basePath']
  tag = ('/' + tag).gsub(base_path.to_s, '')
  [tag]
end