Class: ApiSketch::ExamplesServer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ ExamplesServer

Returns a new instance of ExamplesServer.



6
7
8
# File 'lib/api_sketch/examples_server.rb', line 6

def initialize(env)
  @request = Rack::Request.new(env)
end

Class Method Details

.call(env) ⇒ Object



2
3
4
# File 'lib/api_sketch/examples_server.rb', line 2

def self.call(env)
  new(env).response.finish
end

Instance Method Details

#responseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_sketch/examples_server.rb', line 10

def response
  if api_resource
    api_response = if api_response_context
      api_resource.responses.find { |rsp| rsp.name == api_response_context }
    else
      api_resource.responses.first
    end

    if api_response
      Rack::Response.new do |response|
        api_response.headers.each do |header|
          response[header.name] = header.value
        end

        response['Content-Type'] = 'application/json'

        response.status = Rack::Utils.status_code(api_response.http_status)

        params_array = (api_response.parameters.body_container_type.to_s == "array") ? [api_response.parameters.wrapped_body] : api_response.parameters.body

        response.write(ApiSketch::ResponseRenderer.new(params_array, api_response.parameters.body_container_type, get_elements_count).to_json)
      end
    else
      api_sketch_message("No any responses defined for this resource and context", 404)
    end
  else
    api_sketch_message("Resource is not Found", 404)
  end
end