Class: GrapeSlate::SampleGenerator
- Inherits:
-
Object
- Object
- GrapeSlate::SampleGenerator
- Defined in:
- lib/grape-slate/sample_generator.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#initialize(resource) ⇒ SampleGenerator
constructor
A new instance of SampleGenerator.
- #request ⇒ Object
- #response(list = false) ⇒ Object
- #sample(id = false) ⇒ Object
Constructor Details
#initialize(resource) ⇒ SampleGenerator
Returns a new instance of SampleGenerator.
7 8 9 10 |
# File 'lib/grape-slate/sample_generator.rb', line 7 def initialize(resource) @resource = resource @root = resource.key.singularize end |
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
3 4 5 |
# File 'lib/grape-slate/sample_generator.rb', line 3 def resource @resource end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
3 4 5 |
# File 'lib/grape-slate/sample_generator.rb', line 3 def root @root end |
Instance Method Details
#request ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/grape-slate/sample_generator.rb', line 27 def request hash = sample return unless hash.present? JSON.pretty_generate(hash) end |
#response(list = false) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/grape-slate/sample_generator.rb', line 35 def response(list = false) hash = sample(true) return unless hash.present? hash = [hash] if list JSON.pretty_generate(hash) end |
#sample(id = false) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/grape-slate/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 |