Class: Rspec::Swagger::SwaggerLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/swagger/swagger_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwaggerLoader

Returns a new instance of SwaggerLoader.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rspec/swagger/swagger_loader.rb', line 4

def initialize
  @root = JSON.parse(File.read("docs/swagger.json"))

  @resources = @root["apis"].map do |api|
    path = "#{api['path']}.json"

    if File.exists?("docs" + path)
      JSON.parse(File.read("docs" + path))
    else
      raise "file not found: docs#{path}"
    end
  end

  @root.freeze
  @resources.freeze
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



21
22
23
# File 'lib/rspec/swagger/swagger_loader.rb', line 21

def resources
  @resources
end

#rootObject (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/rspec/swagger/swagger_loader.rb', line 21

def root
  @root
end

Instance Method Details

#documented_pathsObject

documented_paths()

Get a list of routes that are documented. Returns an array of Strings.



26
27
28
29
30
# File 'lib/rspec/swagger/swagger_loader.rb', line 26

def documented_paths
  @documented_paths ||= begin
    @resources.inject([]) { |memo, r| memo += r["apis"].map { |api| api["path"] } }
  end
end

#operation(verb, path) ⇒ Object

operation()

Get an operation by HTTP verb and path.

Example: @loader.operation(:GET, “/foo”)



37
38
39
40
41
42
43
44
45
# File 'lib/rspec/swagger/swagger_loader.rb', line 37

def operation(verb, path)
  @resources.each do |resource|
    resource["apis"].each do |api|
      api["operations"].each do |op|
        return op if op["httpMethod"] == verb.to_s && path == api["path"]
      end
    end
  end
end