class GrapeApiary::SampleGenerator

Attributes

resource[R]
root[R]

Public Class Methods

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

Public Instance Methods

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

  return unless hash.present?

  # format json spaces for blueprint markdown
  JSON
    .pretty_generate(hash)
    .gsub('{', (' ' * 14) + '{')
    .gsub('}', (' ' * 14) + '}')
    .gsub(/\ {2}\"/, (' ' * 16) + '"')
end
response(list = false) click to toggle source
# File lib/grape-apiary/sample_generator.rb, line 40
def response(list = false)
  return unless (hash = sample(true)).present?

  pretty_response_for(list ? [hash] : hash)
end
sample(id = false) click to toggle source
# File lib/grape-apiary/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: Config.generate_id) if id
  hash = { root => hash } if Config.include_root

  hash
end

Private Instance Methods

pretty_response_for(hash) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/grape-apiary/sample_generator.rb, line 49
def pretty_response_for(hash)
  JSON
    .pretty_generate(hash)
    .gsub('[', (' ' * 12) + '[')
    .gsub(']', (' ' * 12) + ']')
    .gsub('{', (' ' * 14) + '{')
    .gsub('}', (' ' * 14) + '}')
    .gsub(/\ {2}\"/, (' ' * 16) + '"')
end