class ApiSketch::ResponseRenderer

Attributes

container_type[R]
elements_count[R]
params[R]

Public Class Methods

new(params, container_type, elements_count) click to toggle source
# File lib/api_sketch/response_renderer.rb, line 5
def initialize(params, container_type, elements_count)
  @params = params || {}
  @container_type = container_type
  @elements_count = elements_count > 0 ? elements_count : 3
end

Public Instance Methods

to_h() click to toggle source
# File lib/api_sketch/response_renderer.rb, line 11
def to_h
  render_content(params, container_type)
end
to_json() click to toggle source
# File lib/api_sketch/response_renderer.rb, line 15
def to_json
  self.to_h.to_json
end

Private Instance Methods

render_content(items, placeholder_type) click to toggle source

TODO: Add this feature in future def to_xml

XML conversion code here

end

# File lib/api_sketch/response_renderer.rb, line 25
def render_content(items, placeholder_type)
  placeholder = case placeholder_type
  when :array then []
  when :document then {}
  end

  items.each do |param, index|
    value = if param.data_type == :array && param.content
      # Some crazy tricks to get 'elements_count' random elements
      elements_count.times.inject([]) { |a, _| a += render_content(param.content, param.data_type) }
    elsif param.data_type == :document && param.content
      render_content(param.content, param.data_type)
    else
      param.example_value(true)
    end

    case placeholder_type
    when :array
      if param.data_type == :array && param.content
        placeholder += value
      else
        placeholder << value
      end
    when :document
      placeholder[param.name] = value
    end
  end

  placeholder
end