Class: AuctionFunCore::Operations::UserContext::AuthenticationOperation

Inherits:
Base
  • Object
show all
Defined in:
lib/auction_fun_core/operations/user_context/authentication_operation.rb

Overview

Operation class for authenticate users.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(attributes, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/auction_fun_core/operations/user_context/authentication_operation.rb', line 12

def self.call(attributes, &block)
  operation = new.call(attributes)

  return operation unless block

  Dry::Matcher::ResultMatcher.call(operation, &block)
end

Instance Method Details

#call(attributes) {|publish_user_authentication(user.id)| ... } ⇒ Object

TODO:

Add custom doc

Yields:



21
22
23
24
25
26
27
# File 'lib/auction_fun_core/operations/user_context/authentication_operation.rb', line 21

def call(attributes)
  user = yield validate_contract(attributes)

  yield publish_user_authentication(user.id)

  Success(user)
end

#publish_user_authentication(user_id, time = Time.current) ⇒ Dry::Monads::Result::Success

Triggers the publication of event users.registration.

Parameters:

  • user_id (Integer)

    User ID

Returns:

  • (Dry::Monads::Result::Success)


44
45
46
47
48
# File 'lib/auction_fun_core/operations/user_context/authentication_operation.rb', line 44

def publish_user_authentication(user_id, time = Time.current)
  Success(
    Application[:event].publish("users.authentication", {user_id: user_id, time: time})
  )
end

#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure

Calls the authentication contract class to perform the validation and authentication of the informed attributes.

Parameters:

  • attrs (Hash)

    user attributes

Returns:

  • (Dry::Monads::Result::Success, Dry::Monads::Result::Failure)


33
34
35
36
37
38
39
# File 'lib/auction_fun_core/operations/user_context/authentication_operation.rb', line 33

def validate_contract(attrs)
  contract = authentication_contract.call(attrs)

  return Failure(contract.errors.to_h) if contract.failure?

  Success(contract.context[:user])
end