Class: Sinatra::SwaggerExposer::SwaggerRequestPreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb

Overview

A preprocessor for a request, apply the parameter preprocessor then execute the query code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwaggerRequestPreprocessor



14
15
16
# File 'lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb', line 14

def initialize
  @preprocessors = []
end

Instance Attribute Details

#preprocessorsObject (readonly)

Returns the value of attribute preprocessors.



12
13
14
# File 'lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb', line 12

def preprocessors
  @preprocessors
end

Instance Method Details

#add_preprocessor(preprocessor) ⇒ Object



18
19
20
# File 'lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb', line 18

def add_preprocessor(preprocessor)
  @preprocessors << preprocessor
end

#run(app, block_params, &block) ⇒ Object

Run the preprocessor the call the route content



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/swagger-exposer/swagger-request-preprocessor.rb', line 26

def run(app, block_params, &block)
  parsed_body = {}
  if app.env['CONTENT_TYPE'] == 'application/json'
    body = app.request.body.read
    unless body.empty?
      parsed_body = JSON.parse(body)
    end
  end
  app.params['parsed_body'] = parsed_body
  unless @preprocessors.empty?
    @preprocessors.each do |preprocessor|
      begin
        preprocessor.run(app, parsed_body)
      rescue SwaggerInvalidException => e
        app.content_type :json
        return [400, {:code => 400, :message => e.message}.to_json]
      end
    end
  end
  if block
    # Execute the block in the context of the app
    app.instance_exec(*block_params, &block)
  else
    ''
  end
end