Module: OpenapiRspec::SchemaLoader

Defined in:
lib/openapi_rspec/schema_loader.rb

Class Method Summary collapse

Class Method Details

.call(path, app: OpenapiRspec.config.app) ⇒ Object



5
6
7
8
# File 'lib/openapi_rspec/schema_loader.rb', line 5

def self.call(path, app: OpenapiRspec.config.app)
  response = request(path, app)
  parse(response)
end

.parse(schema) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/openapi_rspec/schema_loader.rb', line 10

def self.parse(schema)
  begin
    JSON.parse(schema)
  rescue JSON::ParserError
    YAML.safe_load(schema)
  end
rescue => e
  raise "Unable to parse OpenAPI schema. #{e}"
end

.request(path, app) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openapi_rspec/schema_loader.rb', line 20

def self.request(path, app)
  session = Rack::Test::Session.new(app)
  response = session.get(path)

  raise "Response code: #{response.status}" unless response.successful?
  raise "Empty body" if response.body.empty?

  response.body
rescue => e
  raise "Unable to perform GET request for the OpenAPI schema '#{path}'. #{e}"
end