Sha256: e5828f04a1f2624b1f06d51542ecb5a9c3c90c3507b3c791236a4820e0441cab

Contents?: true

Size: 847 Bytes

Versions: 6

Compression:

Stored size: 847 Bytes

Contents

module GrapeSlate
  class SampleGenerator
    attr_reader :resource, :root

    delegate :unique_params, to: :resource

    def initialize(resource)
      @resource = resource
      @root     = resource.key.singularize
    end

    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

    def request
      hash = sample

      return unless hash.present?

      JSON.pretty_generate(hash)
    end

    def response(list = false)
      hash = sample(true)

      return unless hash.present?

      hash = [hash] if list

      JSON.pretty_generate(hash)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
grape-slate-0.0.6 lib/grape-slate/sample_generator.rb
grape-slate-0.0.5 lib/grape-slate/sample_generator.rb
grape-slate-0.0.4 lib/grape-slate/sample_generator.rb
grape-slate-0.0.3 lib/grape-slate/sample_generator.rb
grape-slate-0.0.2 lib/grape-slate/sample_generator.rb
grape-slate-0.0.1 lib/grape-slate/sample_generator.rb