class GrapeMarkdown::SampleGenerator

Attributes

resource[R]
root[R]

Public Class Methods

new(resource) click to toggle source
# File lib/grape-markdown/sample_generator.rb, line 7
def initialize(resource)
  @resource = resource
  @root     = resource.key.singularize
end

Public Instance Methods

request(opts = {}) click to toggle source
# File lib/grape-markdown/sample_generator.rb, line 27
def request(opts = {})
  hash = sample

  return unless hash.present?

  json(hash, opts[:pretty])
end
response(opts = {}) click to toggle source
# File lib/grape-markdown/sample_generator.rb, line 35
def response(opts = {})
  hash = sample(true)

  return unless hash.present?

  hash = [hash] if opts[:list]

  json(hash, opts[:pretty])
end
sample(id = false) click to toggle source
# File lib/grape-markdown/sample_generator.rb, line 12
def sample(id = false)
  array = resource.unique_params.map do |param|
    next if param.name == root

    [param.name, param.example]
  end

  hash = Hash[array.compact]

  hash = hash.reverse_merge('id' => Configuration.generate_id) if id
  hash = { root => hash } if Configuration.include_root

  hash
end

Private Instance Methods

json(hash, pretty = true) click to toggle source
# File lib/grape-markdown/sample_generator.rb, line 47
def json(hash, pretty = true)
  if pretty
    JSON.pretty_generate(hash)
  else
    JSON.generate(hash)
  end
end