Class: RatPackSwagger::SwaggerSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwaggerSpec

Returns a new instance of SwaggerSpec.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/swagger_spec.rb', line 46

def initialize
  @spec = {
    paths: {}
  }
  @spec_cache_invalid = true
  @spec_cache = {
    api_spec: nil,       # the data returned byt /v2/swagger.json
    api_spec_json: nil,  # api_spec as json string
    resolved_spec: nil,  # a hash where all json pointers are resolved 
                         # for request/response validation, because json-schema sucks at pointers
  }
  @swagger_schema = ::JSON.parse(File.read(File.join(File.dirname(__FILE__), 'swagger_json_schema.json')))
  @this_route = SwaggerOperation.new
end

Instance Attribute Details

#specObject

Returns the value of attribute spec.



44
45
46
# File 'lib/swagger_spec.rb', line 44

def spec
  @spec
end

#swagger_schemaObject

Returns the value of attribute swagger_schema.



44
45
46
# File 'lib/swagger_spec.rb', line 44

def swagger_schema
  @swagger_schema
end

#this_routeObject

Returns the value of attribute this_route.



44
45
46
# File 'lib/swagger_spec.rb', line 44

def this_route
  @this_route
end

Instance Method Details

#add_definitions(*constants) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/swagger_spec.rb', line 77

def add_definitions(*constants)
  @spec[:definitions] ||= {}
  constants.each do |constant|
    if constant.is_a?(Module)
      constant.constants.each do |c|
        klass = constant.const_get(c)
        if klass.is_a?(Class) && klass.ancestors.include?(Definition)
          @spec[:definitions][c] = make_deep_copy(klass.definition)
        end
      end
    else
      if constant.is_a?(Class) && constant.ancestors.include?(Definition)
        @spec[:definitions][constant.to_s.rpartition('::').last] = make_deep_copy(constant.definition)
      end
    end
  end
  @spec_cache_invalid = true
end

#api_specObject



100
101
102
103
# File 'lib/swagger_spec.rb', line 100

def api_spec 
  update_spec_cache if @spec_cache_invalid
  @spec_cache[:api_spec]
end

#api_spec_jsonObject



104
105
106
107
# File 'lib/swagger_spec.rb', line 104

def api_spec_json
  update_spec_cache if @spec_cache_invalid
  @spec_cache[:api_spec_json]
end

#register_this_route(path, verb) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/swagger_spec.rb', line 68

def register_this_route(path, verb)
  verb.downcase!
  paths = @spec[:paths]
  paths[path] ||= {}
  paths[path][verb] = @this_route.to_h
  @this_route = SwaggerOperation.new
  @spec_cache_invalid = true
end

#resolved_specObject



96
97
98
99
# File 'lib/swagger_spec.rb', line 96

def resolved_spec
  update_spec_cache if @spec_cache_invalid
  @spec_cache[:resolved_spec]
end

#route_consumes?(path, verb, mime) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/swagger_spec.rb', line 61

def route_consumes?(path, verb, mime)
  route = @spec[:paths][path][verb]
  return true if route[:consumes] && route[:consumes].include?(mime)
  return true if @spec[:consumes] && @spec[:consumes].include?(mime)
  return false
end