module Oath
  module Services
    # Sign up service. Signs the user up
    # @since 0.0.15
    class SignUp
      # Initialize service
      #
      # @param user_params [Hash] A hash of user credentials. Should contain the lookup and token fields
      def initialize user_params
        digested_token = token_digest(user_params)
        @user_params = user_params.
          except(token_field).
          merge(token_store_field.to_sym => digested_token)
      end

      # Performs the service
      # @see Oath::Configuration.default_creation_method
      def perform
        Oath.config.creation_method.call(user_params)
      end

      private

      attr_reader :user_params

      def token_digest(user_params)
        undigested_token = user_params[token_field]
        unless undigested_token.blank?
          Oath.hash_token(undigested_token)
        end
      end

      def token_store_field
        Oath.config.user_token_store_field
      end

      def token_field
        Oath.config.user_token_field
      end
    end
  end
end
