Class: Apivore::Swagger

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/apivore/swagger.rb

Constant Summary collapse

NONVERB_PATH_ITEMS =
%q(parameters)

Instance Method Summary collapse

Instance Method Details

#base_pathObject



23
24
25
# File 'lib/apivore/swagger.rb', line 23

def base_path
  self['basePath'] || ''
end

#each_response(&block) ⇒ Object



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

def each_response(&block)
  paths.each do |path, path_data|
    next if vendor_specific_tag? path
    path_data.each do |verb, method_data|
      next if NONVERB_PATH_ITEMS.include?(verb)
      next if vendor_specific_tag? verb
      if method_data.responses.nil?
        raise "No responses found in swagger for path '#{path}', " \
          "method #{verb}: #{method_data.inspect}"
      end
      method_data.responses.each do |response_code, response_data|
        schema_location = nil
        if response_data.schema
          schema_location = Fragment.new ['#', 'paths', path, verb, 'responses', response_code, 'schema']
        end
        block.call(path, verb, response_code, schema_location)
      end
    end
  end
end

#validateObject



9
10
11
12
13
14
15
16
17
# File 'lib/apivore/swagger.rb', line 9

def validate
  case version
  when '2.0'
    schema = File.read(File.expand_path("../../../data/swagger_2.0_schema.json", __FILE__))
  else
    raise "Unknown/unsupported Swagger version to validate against: #{version}"
  end
  JSON::Validator.fully_validate(schema, self)
end

#vendor_specific_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/apivore/swagger.rb', line 48

def vendor_specific_tag? tag
  tag =~ /\Ax-.*/
end

#versionObject



19
20
21
# File 'lib/apivore/swagger.rb', line 19

def version
  swagger
end