Class: LanguageOperator::Dsl::WebhookDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/dsl/webhook_definition.rb

Overview

Webhook Definition

Defines webhook endpoints for reactive agents.

Examples:

Define a webhook with authentication

webhook "/github/pr-opened" do
  method :post
  authenticate do
    verify_signature header: "X-Hub-Signature-256",
                     secret: ENV['GITHUB_WEBHOOK_SECRET'],
                     algorithm: :sha256,
                     prefix: "sha256="
  end
  on_request do |context|
    # Handle the webhook
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WebhookDefinition

Initialize webhook definition

Parameters:

  • path (String)

    URL path for the webhook



30
31
32
33
34
35
36
# File 'lib/language_operator/dsl/webhook_definition.rb', line 30

def initialize(path)
  @path = path
  @http_method = :post
  @handler = nil
  @authentication = nil
  @validations = []
end

Instance Attribute Details

#authenticationObject (readonly)

Returns the value of attribute authentication.



25
26
27
# File 'lib/language_operator/dsl/webhook_definition.rb', line 25

def authentication
  @authentication
end

#handlerObject (readonly)

Returns the value of attribute handler.



25
26
27
# File 'lib/language_operator/dsl/webhook_definition.rb', line 25

def handler
  @handler
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



25
26
27
# File 'lib/language_operator/dsl/webhook_definition.rb', line 25

def http_method
  @http_method
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/language_operator/dsl/webhook_definition.rb', line 25

def path
  @path
end

#validationsObject (readonly)

Returns the value of attribute validations.



25
26
27
# File 'lib/language_operator/dsl/webhook_definition.rb', line 25

def validations
  @validations
end

Instance Method Details

#authenticate { ... } ⇒ void

This method returns an undefined value.

Define authentication for this webhook

Yields:

  • Authentication configuration block



50
51
52
53
# File 'lib/language_operator/dsl/webhook_definition.rb', line 50

def authenticate(&block)
  @authentication = WebhookAuthentication.new
  @authentication.instance_eval(&block) if block
end

#method(method_name) ⇒ void

This method returns an undefined value.

Set HTTP method

Parameters:

  • method_name (Symbol)

    HTTP method (:get, :post, :put, :delete, :patch)



42
43
44
# File 'lib/language_operator/dsl/webhook_definition.rb', line 42

def method(method_name)
  @http_method = method_name
end

#on_request {|context| ... } ⇒ void

This method returns an undefined value.

Define the request handler

Yields:

  • (context)

    Request handler block

Yield Parameters:

  • context (Hash)

    Request context with :path, :method, :headers, :params, :body



85
86
87
# File 'lib/language_operator/dsl/webhook_definition.rb', line 85

def on_request(&block)
  @handler = block
end

#register(web_server) ⇒ void

This method returns an undefined value.

Register this webhook with a web server

Parameters:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/language_operator/dsl/webhook_definition.rb', line 93

def register(web_server)
  return unless @handler

  web_server.register_route(
    @path,
    method: @http_method,
    authentication: @authentication,
    validations: @validations,
    &@handler
  )
end

#require_content_type(*content_types) ⇒ void

This method returns an undefined value.

Require specific content type

Parameters:

  • content_type (String, Array<String>)

    Allowed content type(s)



67
68
69
# File 'lib/language_operator/dsl/webhook_definition.rb', line 67

def require_content_type(*content_types)
  @validations << { type: :content_type, config: content_types.flatten }
end

#require_headers(headers) ⇒ void

This method returns an undefined value.

Require specific headers

Parameters:

  • headers (Hash)

    Required headers and their expected values (nil = just check presence)



59
60
61
# File 'lib/language_operator/dsl/webhook_definition.rb', line 59

def require_headers(headers)
  @validations << { type: :headers, config: headers }
end

#validate {|context| ... } ⇒ void

This method returns an undefined value.

Add custom validation

Yields:

  • (context)

    Validation block

Yield Returns:

  • (Boolean, String)

    true if valid, or error message string if invalid



76
77
78
# File 'lib/language_operator/dsl/webhook_definition.rb', line 76

def validate(&block)
  @validations << { type: :custom, config: block }
end