Class: Lutaml::Hal::EndpointParameter

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/hal/endpoint_parameter.rb

Overview

OpenAPI-inspired parameter definition for HAL endpoints

Defined Under Namespace

Classes: Schema

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, in:, required: nil, schema: {}, description: nil, example: nil) ⇒ EndpointParameter

Returns a new instance of EndpointParameter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 9

def initialize(name:, in:, required: nil, schema: {}, description: nil, example: nil)
  @name = name.to_s
  @location = validate_location(binding.local_variable_get(:in))
  @required = determine_required(required, @location)
  @schema = Schema.new(schema)
  @description = description
  @example = example

  validate!
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def description
  @description
end

#exampleObject (readonly)

Returns the value of attribute example.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def example
  @example
end

#locationObject (readonly)

Returns the value of attribute location.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def required
  @required
end

#schemaObject (readonly)

Returns the value of attribute schema.



7
8
9
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 7

def schema
  @schema
end

Class Method Details



33
34
35
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 33

def self.cookie(name, **options)
  new(name: name, in: :cookie, **options)
end

.header(name, **options) ⇒ Object



29
30
31
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 29

def self.header(name, **options)
  new(name: name, in: :header, **options)
end

.path(name, **options) ⇒ Object

Convenience constructors for different parameter types



21
22
23
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 21

def self.path(name, **options)
  new(name: name, in: :path, **options)
end

.query(name, **options) ⇒ Object



25
26
27
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 25

def self.query(name, **options)
  new(name: name, in: :query, **options)
end

Instance Method Details

Returns:

  • (Boolean)


50
51
52
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 50

def cookie_parameter?
  @location == :cookie
end

#default_valueObject

Get the default value for this parameter



60
61
62
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 60

def default_value
  @schema.default
end

#header_parameter?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 46

def header_parameter?
  @location == :header
end

#path_parameter?Boolean

Parameter type checks

Returns:

  • (Boolean)


38
39
40
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 38

def path_parameter?
  @location == :path
end

#query_parameter?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 42

def query_parameter?
  @location == :query
end

#validate!Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 64

def validate!
  # Path parameters must be required
  raise ArgumentError, 'Path parameters must be required' if path_parameter? && !@required

  @schema.validate_definition!
end

#validate_value(value) ⇒ Object

Validate a parameter value against this parameter’s schema



55
56
57
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 55

def validate_value(value)
  @schema.validate(value)
end