Skip to content

Instantly share code, notes, and snippets.

@dainmiller
Last active April 14, 2020 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainmiller/301de9778491bb23e1037139c4ed78e7 to your computer and use it in GitHub Desktop.
Save dainmiller/301de9778491bb23e1037139c4ed78e7 to your computer and use it in GitHub Desktop.
Ruby state manager API (Modeled from a famous state management algorithm & data structure)
class FeedConfig
def initialize(name=nil, url=nil)
end
end
class StateMachine
attr_accessor :feeds, :webhooks, :states
def initialize(name=nil, feeds=nil, webhooks=nil, states=nil)
@feeds = feeds
@webhooks = webhooks
@states = states
end
end
class Webhook
def initialize(match=nil, headers=nil)
end
end
class Gate
def initialize(name=nil, triggers=nil, next_states=nil, exit_condition=nil)
end
end
class Action
def initialize(name=nil, webhook=nil, next_states=nil)
end
end
class MetadataTrigger
def initialize(metadata_path=nil, klass)
end
end
class OnEntryTrigger
def initialize()
end
end
class ContextNextStates
def initialize(path=nil, destinations=nil, default=nil)
end
end
class ConstantNextState
def initialize(state=nil)
end
end
class ContextNextStatesOption
def initialize(state=nil,value=nil)
end
end
class OnExitCondition
end
class ExitConditionProgram
def initialize(query)
end
end
TEST_STATE_MACHINE = {
'test_machine': StateMachine.new(
name='test_machine',
feeds=[
FeedConfig.new(name='tests', url='feedurl')
],
webhooks=[
Webhook.new(
match='match_algo',
headers={
'x-api-key': 'kdkjfosdijfodh383892'
}
)
],
states = [
Gate.new(
name='start',
triggers=[
MetadataTrigger.new(metadata_path='path',
OnEntryTrigger.new()
)
],
next_states=ContextNextStates.new(
path='feeds.tests.should',
destinations=[
ContextNextStatesOption.new(
state='perform_action',
value=false
),
ContextNextStatesOption.new(
state='perform_alternate_action',
value=true
)
],
default='perform_action'
),
exit_condition=ExitConditionProgram.new(
'metadata.should_progress = true'
)
),
Action.new(
name='perform_action',
webhook='url',
next_states=ConstantNextState.new(state='end')
),
]
)
}
puts TEST_STATE_MACHINE[:test_machine].feeds # => <StateMachine>
puts TEST_STATE_MACHINE[:test_machine].webhooks # => <Webhook>
puts TEST_STATE_MACHINE[:test_machine].states # => <Gate> <Action>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment