Class: Sinatra::SwaggerExposer::Processing::SwaggerRequestProcessor

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

Overview

A processor for a request, apply the parameters processors then execute the query code

Constant Summary collapse

JSON_CONTENT_TYPE =
MIME::Types['application/json'].first

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(produces = nil) ⇒ SwaggerRequestProcessor

Returns a new instance of SwaggerRequestProcessor.

Parameters:

  • produces (Array<String>) (defaults to: nil)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 27

def initialize(produces = nil)
  @processors_dispatchers = []
  @response_processors = {}
  @produces = produces
  if produces
    @produces_types = produces.collect do |produce|
      if produce == '*'
        [Sinatra::SwaggerExposer::Processing::AllMimesTypes.new]
      else
        MIME::Types[produce]
      end
    end.flatten
  end
end

Instance Attribute Details

#processors_dispatchersObject (readonly)

Returns the value of attribute processors_dispatchers.



24
25
26
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 24

def processors_dispatchers
  @processors_dispatchers
end

#producesObject (readonly)

Returns the value of attribute produces.



24
25
26
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 24

def produces
  @produces
end

#response_processorsObject (readonly)

Returns the value of attribute response_processors.



24
25
26
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 24

def response_processors
  @response_processors
end

Instance Method Details

#add_dispatcher(dispatcher) ⇒ Object



43
44
45
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 43

def add_dispatcher(dispatcher)
  @processors_dispatchers << dispatcher
end

#add_response_processor(code, response_processor) ⇒ Object



48
49
50
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 48

def add_response_processor(code, response_processor)
  @response_processors[code] = response_processor
end

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

Run the processor the call the route content

Parameters:

  • app

    the sinatra app being run

  • block_params (Array)

    the block parameters

  • block

    the block containing the route content



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 58

def run(app, block_params, &block)
  parsed_body = {}
  if JSON_CONTENT_TYPE.like?(app.env['CONTENT_TYPE'])
    body = app.request.body.read
    unless body.empty?
      begin
        parsed_body = JSON.parse(body)
      rescue JSON::ParserError => e
        return [400, {:code => 400, :message => e.message}.to_json]
      end
    end
  end
  app.params['parsed_body'] = parsed_body
  unless @processors_dispatchers.empty?
    @processors_dispatchers.each do |processor_dispatcher|
      begin
        processor_dispatcher.process(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

#validate_response(response_body, content_type, response_status) ⇒ Object

Validate the response

Parameters:

  • response_body (String)

    the body

  • content_type (String)

    the content type

  • response_status (Integer)

    the status



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 93

def validate_response(response_body, content_type, response_status)
  validate_response_content_type(content_type, response_status)
  if @response_processors.key?(response_status)
    response_processor = response_processors[response_status]
    if JSON_CONTENT_TYPE.like?(content_type) && response_processor
      response_processor.validate_response(response_body)
    end
  else
    raise SwaggerInvalidException.new("Status with unknown response status [#{response_status}], known statuses are [#{@response_processors.keys.join(', ')}] response value is #{response_body}")
  end
end

#validate_response_content_type(content_type, response_status) ⇒ Object

Validate a response content type

Parameters:

  • content_type (String)

    the content type to validate

  • response_status (Integer)

    the status



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sinatra/swagger-exposer/processing/swagger-request-processor.rb', line 108

def validate_response_content_type(content_type, response_status)
  if content_type.nil? && (response_status == 204)
    # No content and no content type => everything is OK
  elsif @produces
    unless @produces_types.any? { |produce| produce.like?(content_type) }
      raise SwaggerInvalidException.new("Undeclared content type [#{content_type}], declared content type are [#{@produces.join(', ')}]")
    end
  elsif !JSON_CONTENT_TYPE.like?(content_type)
    raise SwaggerInvalidException.new("Undeclared content type [#{content_type}], if no declaration for the endpoint you should only return json")
  end
end