lib/workato/connector/sdk/connector.rb in workato-connector-sdk-1.2.0 vs lib/workato/connector/sdk/connector.rb in workato-connector-sdk-1.3.0

- old
+ new

@@ -27,15 +27,10 @@ @settings = T.let(HashWithIndifferentAccess.wrap(settings), HashWithIndifferentAccess) @connection_source = T.let(HashWithIndifferentAccess.wrap(@source[:connection]), HashWithIndifferentAccess) @methods_source = T.let(HashWithIndifferentAccess.wrap(@source[:methods]), HashWithIndifferentAccess) end - sig { params(path: String, params: T::Hash[Symbol, T.untyped]).returns(T.untyped) } - def invoke(path, params = {}) - InvokePath.new(path: path, connector: self, params: params).call - end - sig { returns(T.nilable(String)) } def title @source[:title] end @@ -44,20 +39,22 @@ @actions = T.let(@actions, T.nilable(ActionsProxy)) @actions ||= ActionsProxy.new( actions: source[:actions].presence || {}, methods: methods_source, object_definitions: object_definitions, - connection: connection + connection: connection, + streams: streams ) end sig { returns(MethodsProxy) } def methods @methods = T.let(@methods, T.nilable(MethodsProxy)) @methods ||= MethodsProxy.new( methods: methods_source, - connection: connection + connection: connection, + streams: streams ) end sig { params(settings: T.nilable(SorbetTypes::SettingsHash)).returns(T.untyped) } def test(settings = nil) @@ -77,11 +74,12 @@ @triggers = T.let(@triggers, T.nilable(TriggersProxy)) @triggers ||= TriggersProxy.new( triggers: source[:triggers].presence || {}, methods: methods_source, connection: connection, - object_definitions: object_definitions + object_definitions: object_definitions, + streams: streams ) end sig { returns(ObjectDefinitions) } def object_definitions @@ -111,13 +109,39 @@ connection: connection_source, settings: settings ) end + sig { returns(Streams) } + def streams + @streams = T.let(@streams, T.nilable(Streams)) + @streams ||= Streams.new( + streams: streams_sources, + methods: methods_source, + connection: connection + ) + end + private sig { returns(HashWithIndifferentAccess) } + def streams_sources + @streams_sources = T.let(@streams_sources, T.nilable(HashWithIndifferentAccess)) + return @streams_sources if @streams_sources + + @streams_sources = HashWithIndifferentAccess.new + @streams_sources.merge!(source[:streams].presence || {}) + (source[:actions] || {}).values.map do |action| + @streams_sources.merge!(action[:streams] || {}) + end + (source[:trigger] || {}).values.map do |trigger| + @streams_sources.merge!(trigger[:streams] || {}) + end + @streams_sources + end + + sig { returns(HashWithIndifferentAccess) } attr_reader :methods_source sig { returns(HashWithIndifferentAccess) } attr_reader :connection_source @@ -131,22 +155,24 @@ sig do params( actions: HashWithIndifferentAccess, object_definitions: ObjectDefinitions, methods: HashWithIndifferentAccess, - connection: Connection + connection: Connection, + streams: Streams ).void end - def initialize(actions:, object_definitions:, methods:, connection:) + def initialize(actions:, object_definitions:, methods:, connection:, streams:) @methods = methods @connection = connection @object_definitions = object_definitions + @streams = streams @actions = T.let({}, T::Hash[T.any(Symbol, String), Action]) define_action_methods(actions) end - sig { params(action: T.any(Symbol, String)).returns(T.untyped) } + sig { params(action: T.any(Symbol, String)).returns(Action) } def [](action) public_send(action) end private @@ -155,10 +181,13 @@ attr_reader :methods sig { returns(Connection) } attr_reader :connection + sig { returns(Streams) } + attr_reader :streams + sig { returns(ObjectDefinitions) } attr_reader :object_definitions sig { params(actions_source: HashWithIndifferentAccess).void } def define_action_methods(actions_source) @@ -166,11 +195,12 @@ define_singleton_method(action) do |input_ = nil| @actions[action] ||= Action.new( action: definition, object_definitions: object_definitions, methods: methods, - connection: connection + connection: connection, + streams: streams ) return @actions[action] if input_.nil? T.must(@actions[action]).invoke(input_) end @@ -182,16 +212,18 @@ extend T::Sig sig do params( methods: HashWithIndifferentAccess, - connection: Connection + connection: Connection, + streams: Streams ).void end - def initialize(methods:, connection:) + def initialize(methods:, connection:, streams:) @methods = methods @connection = connection + @streams = streams @actions = T.let({}, T::Hash[T.any(Symbol, String), Action]) define_action_methods end private @@ -200,20 +232,24 @@ attr_reader :methods sig { returns(Connection) } attr_reader :connection + sig { returns(Streams) } + attr_reader :streams + sig { void } def define_action_methods methods.each do |method, _definition| define_singleton_method(method) do |*args| @actions[method] ||= Action.new( action: { execute: -> { T.unsafe(self).call(method, *args) } }, methods: methods, - connection: connection + connection: connection, + streams: streams ) T.must(@actions[method]).execute end end end @@ -276,29 +312,39 @@ sig do params( triggers: HashWithIndifferentAccess, object_definitions: ObjectDefinitions, methods: HashWithIndifferentAccess, - connection: Connection + connection: Connection, + streams: Streams ).void end - def initialize(triggers:, object_definitions:, methods:, connection:) + def initialize(triggers:, object_definitions:, methods:, connection:, streams:) @methods = methods @connection = connection @object_definitions = object_definitions + @streams = streams @triggers = T.let({}, T::Hash[T.any(Symbol, String), Trigger]) define_trigger_methods(triggers) end + sig { params(trigger: T.any(Symbol, String)).returns(Trigger) } + def [](trigger) + public_send(trigger) + end + private sig { returns(HashWithIndifferentAccess) } attr_reader :methods sig { returns(Connection) } attr_reader :connection + sig { returns(Streams) } + attr_reader :streams + sig { returns(ObjectDefinitions) } attr_reader :object_definitions sig { params(triggers_source: HashWithIndifferentAccess).void } def define_trigger_methods(triggers_source) @@ -306,77 +352,19 @@ define_singleton_method(trigger) do |input_ = nil, payload = {}, headers = {}, params = {}| @triggers[trigger] ||= Trigger.new( trigger: definition, object_definitions: object_definitions, methods: methods, - connection: connection + connection: connection, + streams: streams ) return @triggers[trigger] if input_.nil? T.must(@triggers[trigger]).invoke(input_, payload, headers, params) end end end end - - class InvokePath - extend T::Sig - - sig do - params( - path: String, - connector: Connector, - params: T::Hash[Symbol, T.untyped] - ).void - end - def initialize(path:, connector:, params:) - @path = T.let(path, String) - @connector = T.let(connector, Connector) - @params = T.let(params, T::Hash[Symbol, T.untyped]) - end - - sig { returns(T.untyped) } - def call - invoke_path - end - - private - - sig { returns(String) } - attr_reader :path - - sig { returns(Connector) } - attr_reader :connector - - sig { returns(T::Hash[Symbol, T.untyped]) } - attr_reader :params - - sig { returns(T.untyped) } - def invoke_path - methods = path.split('.') - method = methods.pop - raise ArgumentError, 'path is not found' unless method - - object = methods.inject(connector) { |obj, m| obj.public_send(m) } - output = invoke_method(object, method) - if output.respond_to?(:invoke) - invoke_method(output, :invoke) - else - output - end - end - - sig { params(object: T.untyped, method: T.any(Symbol, String)).returns(T.untyped) } - def invoke_method(object, method) - parameters = object.method(method).parameters.reject { |p| p[0] == :block }.map(&:second) - args = params.values_at(*parameters) - if parameters.last == :args - args = args.take(args.length - 1) + Array.wrap(args.last).flatten(1) - end - object.public_send(method, *args) - end - end - private_constant :InvokePath end end end