-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add hook support to contract tests (#257)
- Loading branch information
Showing
5 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'ldclient-rb' | ||
|
||
class Hook | ||
include LaunchDarkly::Interfaces::Hooks::Hook | ||
|
||
# | ||
# @param name [String] | ||
# @param callback_uri [String] | ||
# @param data [Hash] | ||
# | ||
def initialize(name, callback_uri, data) | ||
@metadata = LaunchDarkly::Interfaces::Hooks::Metadata.new(name) | ||
@callback_uri = callback_uri | ||
@data = data | ||
@context_filter = LaunchDarkly::Impl::ContextFilter.new(false, []) | ||
end | ||
|
||
def metadata | ||
@metadata | ||
end | ||
|
||
# | ||
# @param evaluation_series_context [LaunchDarkly::Interfaces::Hooks::EvaluationSeriesContext] | ||
# @param data [Hash] | ||
# | ||
def before_evaluation(evaluation_series_context, data) | ||
payload = { | ||
evaluationSeriesContext: { | ||
flagKey: evaluation_series_context.key, | ||
context: @context_filter.filter(evaluation_series_context.context), | ||
defaultValue: evaluation_series_context.default_value, | ||
method: evaluation_series_context.method, | ||
}, | ||
evaluationSeriesData: data, | ||
stage: 'beforeEvaluation', | ||
} | ||
result = HTTP.post(@callback_uri, json: payload) | ||
|
||
(data || {}).merge(@data[:beforeEvaluation] || {}) | ||
end | ||
|
||
|
||
# | ||
# @param evaluation_series_context [LaunchDarkly::Interfaces::Hooks::EvaluationSeriesContext] | ||
# @param data [Hash] | ||
# @param detail [LaunchDarkly::EvaluationDetail] | ||
# | ||
def after_evaluation(evaluation_series_context, data, detail) | ||
payload = { | ||
evaluationSeriesContext: { | ||
flagKey: evaluation_series_context.key, | ||
context: @context_filter.filter(evaluation_series_context.context), | ||
defaultValue: evaluation_series_context.default_value, | ||
method: evaluation_series_context.method, | ||
}, | ||
evaluationSeriesData: data, | ||
evaluationDetail: { | ||
value: detail.value, | ||
variationIndex: detail.variation_index, | ||
reason: detail.reason, | ||
}, | ||
stage: 'afterEvaluation', | ||
} | ||
HTTP.post(@callback_uri, json: payload) | ||
|
||
(data || {}).merge(@data[:afterEvaluation] || {}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
'polling-gzip', | ||
'inline-context', | ||
'anonymous-redaction', | ||
'evaluation-hooks', | ||
], | ||
}.to_json | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters