Class: Synthra::Parser::AST::ScenarioNode

Inherits:
Node
  • Object
show all
Defined in:
lib/synthra/parser/ast.rb

Overview

Scenario node

Represents a response scenario within an API definition. Scenarios define possible HTTP responses with status codes, probabilities, and response body shapes.

Examples:

DSL

scenario Success:
  @status 200
  @probability 80%
  users: schema(:User).array

AST

ScenarioNode.new(
  name: "Success",
  directives: [StatusDirective.new(status_code: 200), ...],
  response_fields: [ResponseFieldNode.new(...)]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, directives: [], response_fields: [], line: nil) ⇒ ScenarioNode

Create a new ScenarioNode

Parameters:

  • name (String)

    scenario name

  • directives (Array<DirectiveNode>) (defaults to: [])

    HTTP directives

  • response_fields (Array<ResponseFieldNode>) (defaults to: [])

    response fields

  • line (Integer, nil) (defaults to: nil)

    source line number



951
952
953
954
955
956
# File 'lib/synthra/parser/ast.rb', line 951

def initialize(name:, directives: [], response_fields: [], line: nil)
  super(line: line)
  @name = name
  @directives = directives
  @response_fields = response_fields
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



935
936
937
# File 'lib/synthra/parser/ast.rb', line 935

def directives
  @directives
end

#nameObject (readonly)

Returns the value of attribute name.



929
930
931
# File 'lib/synthra/parser/ast.rb', line 929

def name
  @name
end

#response_fieldsObject (readonly)

Returns the value of attribute response_fields.



941
942
943
# File 'lib/synthra/parser/ast.rb', line 941

def response_fields
  @response_fields
end

Instance Method Details

#probabilityInteger?

Get the probability from directives

Returns:

  • (Integer, nil)

    probability percentage or nil



973
974
975
976
# File 'lib/synthra/parser/ast.rb', line 973

def probability
  prob_directive = directives.find { |d| d.is_a?(ProbabilityDirective) }
  prob_directive&.percentage
end

#status_codeInteger?

Get the status code from directives

Returns:

  • (Integer, nil)

    HTTP status code or nil



963
964
965
966
# File 'lib/synthra/parser/ast.rb', line 963

def status_code
  status_directive = directives.find { |d| d.is_a?(StatusDirective) }
  status_directive&.status_code
end